• 万能适配器的一些问题


    不用万能适配器volley都能搞定
    如果用万能适配器则不能用volley(因为万能适配器里边有自己的settag)可以用NUtils在
    NUtils里边自定义三级缓存每次设图片时都去访问三级缓存

     1 package com.example.wannengadapter;
     2 
     3 import java.util.List;
     4 
     5 import android.content.Context;
     6 import android.view.LayoutInflater;
     7 import android.view.View;
     8 import android.view.ViewGroup;
     9 import android.widget.BaseAdapter;
    10 
    11 public abstract class CommonAdapter<T> extends BaseAdapter {
    12     protected Context mContext;
    13     protected List<T> mDatas;
    14     protected LayoutInflater mInflater;
    15     private int layoutId;
    16 
    17     public CommonAdapter(Context context, List<T> datas, int layoutId) {
    18         this.mContext = context;
    19         mInflater = LayoutInflater.from(context);
    20         this.mDatas = datas;
    21         this.layoutId = layoutId;
    22     }
    23 
    24     @Override
    25     public int getCount() {
    26         return mDatas.size();
    27     }
    28 
    29     @Override
    30     public T getItem(int position) {
    31         return mDatas.get(position);
    32     }
    33 
    34     @Override
    35     public long getItemId(int position) {
    36         return position;
    37     }
    38 
    39     @Override
    40     public View getView(int position, View convertView, ViewGroup parent) {
    41         ViewHolder holder = ViewHolder.get(mContext, convertView, parent,
    42                 layoutId, position);
    43         convert(holder, getItem(position));
    44         return holder.getConvertView();
    45     }
    46 
    47     public abstract void convert(ViewHolder holder, T t);
    48 
    49 }
    CommonAdapter
     1 package com.example.wannengadapter;
     2 
     3 import android.content.Context;
     4 import android.view.View;
     5 import android.view.ViewGroup;
     6 
     7 import java.util.List;
     8 
     9 public abstract class MultiItemCommonAdapter<T> extends CommonAdapter<T> {
    10 
    11     protected MultiItemTypeSupport<T> mMultiItemTypeSupport;
    12 
    13     public MultiItemCommonAdapter(Context context, List<T> datas,
    14             MultiItemTypeSupport<T> multiItemTypeSupport) {
    15         super(context, datas, -1);
    16         mMultiItemTypeSupport = multiItemTypeSupport;
    17     }
    18 
    19     @Override
    20     public int getViewTypeCount() {
    21         if (mMultiItemTypeSupport != null)
    22             return mMultiItemTypeSupport.getViewTypeCount();
    23         return super.getViewTypeCount();
    24     }
    25 
    26     @Override
    27     public int getItemViewType(int position) {
    28         if (mMultiItemTypeSupport != null)
    29             return mMultiItemTypeSupport.getItemViewType(position,
    30                     mDatas.get(position));
    31         return super.getItemViewType(position);
    32 
    33     }
    34 
    35     @Override
    36     public View getView(int position, View convertView, ViewGroup parent) {
    37         if (mMultiItemTypeSupport == null)
    38             return super.getView(position, convertView, parent);
    39 
    40         int layoutId = mMultiItemTypeSupport.getLayoutId(position,
    41                 getItem(position));
    42         ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent,
    43                 layoutId, position);
    44         convert(viewHolder, getItem(position));
    45         return viewHolder.getConvertView();
    46     }
    47 
    48 }
    MultiItemCommonAdapter
    1 package com.example.wannengadapter;
    2 
    3 public interface MultiItemTypeSupport<T> {
    4     int getLayoutId(int position, T t);
    5 
    6     int getViewTypeCount();
    7 
    8     int getItemViewType(int postion, T t);
    9 }
    MultiItemTypeSupport
      1 package com.example.wannengadapter;
      2 
      3 import android.annotation.SuppressLint;
      4 import android.content.Context;
      5 import android.graphics.Bitmap;
      6 import android.graphics.Paint;
      7 import android.graphics.Typeface;
      8 import android.graphics.drawable.Drawable;
      9 import android.os.Build;
     10 import android.text.util.Linkify;
     11 import android.util.SparseArray;
     12 import android.view.LayoutInflater;
     13 import android.view.View;
     14 import android.view.ViewGroup;
     15 import android.view.animation.AlphaAnimation;
     16 import android.widget.Checkable;
     17 import android.widget.ImageView;
     18 import android.widget.ProgressBar;
     19 import android.widget.RatingBar;
     20 import android.widget.TextView;
     21 
     22 public class ViewHolder {
     23     private SparseArray<View> mViews;
     24     private int mPosition;
     25     private View mConvertView;
     26     private Context mContext;
     27     private int mLayoutId;
     28 
     29     public ViewHolder(Context context, ViewGroup parent, int layoutId,
     30             int position) {
     31         mContext = context;
     32         mLayoutId = layoutId;
     33         this.mPosition = position;
     34         this.mViews = new SparseArray<View>();
     35         mConvertView = LayoutInflater.from(context).inflate(layoutId, parent,
     36                 false);
     37         mConvertView.setTag(this);
     38     }
     39 
     40     public static ViewHolder get(Context context, View convertView,
     41             ViewGroup parent, int layoutId, int position) {
     42         if (convertView == null) {
     43             return new ViewHolder(context, parent, layoutId, position);
     44         } else {
     45             ViewHolder holder = (ViewHolder) convertView.getTag();
     46             holder.mPosition = position;
     47             return holder;
     48         }
     49     }
     50 
     51     public int getPosition() {
     52         return mPosition;
     53     }
     54 
     55     public int getLayoutId() {
     56         return mLayoutId;
     57     }
     58 
     59     /**
     60      * 通过viewId获取控件
     61      * 
     62      * @param viewId
     63      * @return
     64      */
     65     public <T extends View> T getView(int viewId) {
     66         View view = mViews.get(viewId);
     67         if (view == null) {
     68             view = mConvertView.findViewById(viewId);
     69             mViews.put(viewId, view);
     70         }
     71         return (T) view;
     72     }
     73 
     74     public View getConvertView() {
     75         return mConvertView;
     76     }
     77 
     78     /**
     79      * 设置TextView的值
     80      * 
     81      * @param viewId
     82      * @param text
     83      * @return
     84      */
     85     public ViewHolder setText(int viewId, String text) {
     86         TextView tv = getView(viewId);
     87         tv.setText(text);
     88         return this;
     89     }
     90 
     91     public ViewHolder setImageResource(int viewId, int resId) {
     92         ImageView view = getView(viewId);
     93         view.setImageResource(resId);
     94         return this;
     95     }
     96 
     97     public ViewHolder setImageBitmap(int viewId, Bitmap bitmap) {
     98         ImageView view = getView(viewId);
     99         view.setImageBitmap(bitmap);
    100         return this;
    101     }
    102 
    103     public ViewHolder setImageDrawable(int viewId, Drawable drawable) {
    104         ImageView view = getView(viewId);
    105         view.setImageDrawable(drawable);
    106         return this;
    107     }
    108 
    109     public ViewHolder setBackgroundColor(int viewId, int color) {
    110         View view = getView(viewId);
    111         view.setBackgroundColor(color);
    112         return this;
    113     }
    114 
    115     public ViewHolder setBackgroundRes(int viewId, int backgroundRes) {
    116         View view = getView(viewId);
    117         view.setBackgroundResource(backgroundRes);
    118         return this;
    119     }
    120 
    121     public ViewHolder setTextColor(int viewId, int textColor) {
    122         TextView view = getView(viewId);
    123         view.setTextColor(textColor);
    124         return this;
    125     }
    126 
    127     public ViewHolder setTextColorRes(int viewId, int textColorRes) {
    128         TextView view = getView(viewId);
    129         view.setTextColor(mContext.getResources().getColor(textColorRes));
    130         return this;
    131     }
    132 
    133     @SuppressLint("NewApi")
    134     public ViewHolder setAlpha(int viewId, float value) {
    135         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    136             getView(viewId).setAlpha(value);
    137         } else {
    138             // Pre-honeycomb hack to set Alpha value
    139             AlphaAnimation alpha = new AlphaAnimation(value, value);
    140             alpha.setDuration(0);
    141             alpha.setFillAfter(true);
    142             getView(viewId).startAnimation(alpha);
    143         }
    144         return this;
    145     }
    146 
    147     public ViewHolder setVisible(int viewId, boolean visible) {
    148         View view = getView(viewId);
    149         view.setVisibility(visible ? View.VISIBLE : View.GONE);
    150         return this;
    151     }
    152 
    153     public ViewHolder linkify(int viewId) {
    154         TextView view = getView(viewId);
    155         Linkify.addLinks(view, Linkify.ALL);
    156         return this;
    157     }
    158 
    159     public ViewHolder setTypeface(Typeface typeface, int... viewIds) {
    160         for (int viewId : viewIds) {
    161             TextView view = getView(viewId);
    162             view.setTypeface(typeface);
    163             view.setPaintFlags(view.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    164         }
    165         return this;
    166     }
    167 
    168     public ViewHolder setProgress(int viewId, int progress) {
    169         ProgressBar view = getView(viewId);
    170         view.setProgress(progress);
    171         return this;
    172     }
    173 
    174     public ViewHolder setProgress(int viewId, int progress, int max) {
    175         ProgressBar view = getView(viewId);
    176         view.setMax(max);
    177         view.setProgress(progress);
    178         return this;
    179     }
    180 
    181     public ViewHolder setMax(int viewId, int max) {
    182         ProgressBar view = getView(viewId);
    183         view.setMax(max);
    184         return this;
    185     }
    186 
    187     public ViewHolder setRating(int viewId, float rating) {
    188         RatingBar view = getView(viewId);
    189         view.setRating(rating);
    190         return this;
    191     }
    192 
    193     public ViewHolder setRating(int viewId, float rating, int max) {
    194         RatingBar view = getView(viewId);
    195         view.setMax(max);
    196         view.setRating(rating);
    197         return this;
    198     }
    199 
    200     public ViewHolder setTag(int viewId, Object tag) {
    201         View view = getView(viewId);
    202         view.setTag(tag);
    203         return this;
    204     }
    205 
    206     public ViewHolder setTag(int viewId, int key, Object tag) {
    207         View view = getView(viewId);
    208         view.setTag(key, tag);
    209         return this;
    210     }
    211 
    212     public ViewHolder setChecked(int viewId, boolean checked) {
    213         Checkable view = (Checkable) getView(viewId);
    214         view.setChecked(checked);
    215         return this;
    216     }
    217 
    218     /**
    219      * 关于事件的
    220      */
    221     public ViewHolder setOnClickListener(int viewId,
    222             View.OnClickListener listener) {
    223         View view = getView(viewId);
    224         view.setOnClickListener(listener);
    225         return this;
    226     }
    227 
    228     public ViewHolder setOnTouchListener(int viewId,
    229             View.OnTouchListener listener) {
    230         View view = getView(viewId);
    231         view.setOnTouchListener(listener);
    232         return this;
    233     }
    234 
    235     public ViewHolder setOnLongClickListener(int viewId,
    236             View.OnLongClickListener listener) {
    237         View view = getView(viewId);
    238         view.setOnLongClickListener(listener);
    239         return this;
    240     }
    241 
    242 }
    ViewHolder

    加入以上四个类  可以都加在同一个包下  

    例子:

     1 package com.zhy.quickdev.adapter.sample.adapter;
     2 
     3 import android.content.Context;
     4 
     5 import com.zhy.quickdev.adapter.MultiItemCommonAdapter;
     6 import com.zhy.quickdev.adapter.MultiItemTypeSupport;
     7 import com.zhy.quickdev.adapter.R;
     8 import com.zhy.quickdev.adapter.ViewHolder;
     9 import com.zhy.quickdev.adapter.sample.bean.ChatMessage;
    10 
    11 import java.util.List;
    12 
    13 /**
    14  * Created by zhy on 15/9/4.
    15  */
    16 public class ChatAdapter extends MultiItemCommonAdapter<ChatMessage>
    17 {
    18     public ChatAdapter(Context context, List<ChatMessage> datas)
    19     {
    20         super(context, datas, new MultiItemTypeSupport<ChatMessage>()
    21         {
    22             @Override
    23             public int getLayoutId(int position, ChatMessage msg)
    24             {
    25                 if (msg.isComMeg())
    26                 {
    27                     return R.layout.main_chat_from_msg;
    28                 }
    29                 return R.layout.main_chat_send_msg;
    30             }
    31 
    32             @Override
    33             public int getViewTypeCount()
    34             {
    35                 return 2;
    36             }
    37             @Override
    38             public int getItemViewType(int postion, ChatMessage msg)
    39             {
    40                 if (msg.isComMeg())
    41                 {
    42                     return ChatMessage.RECIEVE_MSG;
    43                 }
    44                 return ChatMessage.SEND_MSG;
    45             }
    46         });
    47     }
    48 
    49     @Override
    50     public void convert(ViewHolder holder, ChatMessage chatMessage)
    51     {
    52 
    53         switch (holder.getLayoutId())
    54         {
    55             case R.layout.main_chat_from_msg:
    56                 holder.setText(R.id.chat_from_content, chatMessage.getContent());
    57                 holder.setText(R.id.chat_from_name, chatMessage.getName());
    58                 holder.setImageResource(R.id.chat_from_icon, chatMessage.getIcon());
    59                 break;
    60             case R.layout.main_chat_send_msg:
    61                 holder.setText(R.id.chat_send_content, chatMessage.getContent());
    62                 holder.setText(R.id.chat_send_name, chatMessage.getName());
    63                 holder.setImageResource(R.id.chat_send_icon, chatMessage.getIcon());
    64                 break;
    65         }
    66     }
    67 }
    ChatAdapter
  • 相关阅读:
    身份证号码验证(转)
    《编写有效用例》读书笔记2
    《编写有效用例》读书笔记3
    1
    《实例化需求:团队如何交付正确的软件》阅读笔记3
    《实例化需求:团队如何交付正确的软件》阅读笔记2
    《实例化需求:团队如何交付正确的软件》阅读笔记1
    项目目标文档
    系统利益相关者描述
    《代码阅读方法与实践》阅读笔记3
  • 原文地址:https://www.cnblogs.com/bimingcong/p/4844811.html
Copyright © 2020-2023  润新知