万能适配器的一些问题

不用万能适配器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

时间: 2024-10-12 00:43:35

万能适配器的一些问题的相关文章

Android进阶笔记10:Android 万能适配器

1. Android 万能适配器      项目中Listview GridView几乎是必用的组件,Android也提供一套机制,为这些控件绑定数据,那就是Adapter.用起来虽然还不错,但每次都需要去继承一个BaseAdapter,然后实现里面的一大堆方法,而我们每次最关心的无非就是getView方法,其余的方法几乎都是相同代码.这里是不是就可以优化起来呢?在其次,我们在使用Adapter的时候,为了优化性能,常常会创建一个Holder.而Holder里面每次存放的都是View,对Hole

Android开发之万能适配器

ListView.GridView等等非常多的东西都需要适配器.而如果开发一个app每一个listview都有写一个Adapter的话,那还怎么愉快的玩游戏.. 什么是ViewHolider以及的用法和为什么要用? 这位博主写的非常好. http://www.cnblogs.com/lichenwei/p/4085107.html 所谓的万能适配器,无非是将适配器的重复代码抽取出来进行封装.不同功能的代码则留写一个方法留给用户复写,则每个listview的适配器就只变成几句话就够了. 这是一般适

慕课网 万能适配器学习笔记

首先再次感谢 慕课网的技术大牛分享了该课程. 一.传统 ListView 适配器的模式:1. 创建Listview,并为listview 创建item布局2.封装数据 3.为Item布局创建特定的适配器 用于数据展示.(在这过程中,如果某一个ListView 的item中控件过多,控件的生命及调用会导致代码量上升,而如果一个app中有很多个地方需要用到ListView 时,需要分别写多个适配器,会导致很多重复性的操作,为了避免这种情况的发生,让编码更轻松,可参考 慕课网Android万能适配器

Android之ListView性能优化——万能适配器

如下图,加入现在有一个这样的需求图,你会怎么做?作为一个初学者,之前我都是直接用SimpleAdapter结合一个Item的布局来实现的,感觉这样实现起来很方便(基本上一行代码就可以实现),而且也没有觉得有什么不好的.直到最近在慕课网上看到鸿洋大神讲的“机器人小慕”和“万能适配器”两节课,才对BaseAdapter有所了解.看了鸿洋大神的课程之后,我又上网搜了几个博客,也看了一些源码和文档,于是打算写一个帖子来记录一下自己的学习历程. 在今天的帖子中,我们从一个最基本的实现BaseAdapter

安卓开发笔记——打造万能适配器(Adapter)

为什么要打造万能适配器? 在安卓开发中,用到ListView和GridView的地方实在是太多了,系统默认给我们提供的适配器(ArrayAdapter,SimpleAdapter)经常不能满足我们的需要,因此我们时常要去继承BaseAdapter类去实现一个自定义的适配器来满足我们的场景需要. 如果你是开发一个简单点的APP还好,可能ListView和GridView的数量不会太多,我们只要去写几个BaseAdapter实现类就可以了. 但如果有一天,你需要开发一个APP里面具有几十个ListV

打造android偷懒神器———RecyclerView的万能适配器

转载请注明出处谢谢:http://www.cnblogs.com/liushilin/p/5720926.html 很不好意思让大家久等了,本来昨天就应该写这个的,无奈公司昨天任务比较紧,所以没能按时给大家带来RecyclerView的适配器,楼主对期待的小伙伴表示最深刻地歉意. 如果你没有看前面的万能的ListView,GridView等的万能适配器,楼主推荐你去看一看,当然,大牛就免了. 另外,楼主今天在构思这个RecyclerView的过程中发现前天写的ListView有点毛病,现在楼主已

万能适配器

初学Android,通过网上视频.资料整理了万能适配器的编写,记录一下,方便以后使用. CommonAdapter public abstract class CommonAdapter<T> extends BaseAdapter { protected Context mContext; protected List<T> mDatas; protected LayoutInflater mInflater; private int layoutId; public Commo

RecyclerView万能适配器

转载请申明:http://blog.csdn.net/yoyo_newbie/article/details/48130813 没次当我们用RecyclerView是时候,不得不想到要添加点击事件, 头部,尾部,甚至分组繁琐代码烦恼,于是本人就无奈写了共用的adapter. 使用说明: 如,item资源为 R.layout.fragment_jiongtu_item_of_indexfregment item里面有个imageview id为 R.id.iv_photo private clas

RecyclerView的万能适配器+定义可以到底部自动刷新的RecyclerView

RecyclerView的重要性就不做重复说明了,为了方便以后直接使用写了这个,主要有: 万能适配器在使用的时候分为定义布局和绑定数据,方便直接套用.加入了底部刷新,需要配合自己写的RecyclerView一起使用,对于布局中各个子布局和控件可以做到响应各种点击事件: 首先抽取ViewHolder:这里的要点是用到了SparseArray(源码中类似ArrayList.直接使用Object数组进行实现): package com.fightzhao.baseadapterdemo.base; i