ListView、GridView等等非常多的东西都需要适配器。而如果开发一个app每一个listview都有写一个Adapter的话,那还怎么愉快的玩游戏。。
什么是ViewHolider以及的用法和为什么要用?
这位博主写的非常好。
http://www.cnblogs.com/lichenwei/p/4085107.html
所谓的万能适配器,无非是将适配器的重复代码抽取出来进行封装。不同功能的代码则留写一个方法留给用户复写,则每个listview的适配器就只变成几句话就够了。
这是一般适配器的代码:
private class MyAdapter extends BaseAdapter
{ private Context mcontext=null;
private LayoutInflater inflater; public MyAdapter(Context c)
{
this.mcontext=c;
this.inflater=LayoutInflater.from(c);//from告诉系统从哪里获得布局填充器
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mListTitle.length;
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ViewHolder viewholder =null;
if (arg1==null)
{
viewholder=new ViewHolder();
arg1=inflater.inflate(R.layout.iconlist, arg2, false);
viewholder.iv=(ImageView)arg1.findViewById(R.id.imageview);
viewholder.title=(TextView)arg1.findViewById(R.id.title);
viewholder.text=(TextView)arg1.findViewById(R.id.text);
arg1.setTag(viewholder);
}
else
{
viewholder=(ViewHolder)arg1.getTag();
}
viewholder.iv.setImageResource(R.drawable.icon1);
viewholder.title.setText(mListTitle[arg0]);
viewholder.text.setText(mListStr[arg0]);
return arg1;
} }
private class ViewHolder
{
private ImageView iv=null;
private TextView title=null;
private TextView text=null;
}
首先我们先对ViewHolder进行封装:
1、ViewHolder将xml文件转换成convertview,并将转换后的view返回
2、要设置标签
3、判断convertview是否为空,空的话重新new创建,不为空的话getTag();
代码:
package com.example.list_view;
import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class easyViewHolder { private SparseArray<View> sparseArray;
private Context context;
private int LayoutId;
private View convertview;
public easyViewHolder(Context mcontext,View mconvertview,int LayoutId,ViewGroup parent)
{
this.context=mcontext;
this.sparseArray=new SparseArray<View>();
this.convertview=LayoutInflater.from(mcontext).inflate(LayoutId, parent, false);
convertview.setTag(this);
}
public static easyViewHolder get(Context context,View convertview,int LayoutId,ViewGroup parent)
{
if (convertview==null)
{
return new easyViewHolder(context, convertview, LayoutId, parent);
} // 特别需要注意的一点,由于ListView的复用,比如屏幕只显示5个Item,那么当下拉到第6个时会复用第1个的Item,所以这边需要更新position
return (easyViewHolder)convertview.getTag(); }
public <T extends View> T getView(int viewId)//通过id找到组件
{
View view = sparseArray.get(viewId);
if (view==null)
{
view=(View)this.convertview.findViewById(viewId);
sparseArray.put(viewId, view);
}
return (T)view; }
public View getConvertView()
{
return this.convertview;
}
}
再封装一个适配器:
声明一个抽象类
1、保留不变的方法
2、修改getview()
3、写一个需要重写函数用来给用户传值。
package com.example.list_view; import java.util.ArrayList;
import java.util.List; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter; public abstract class easyAdapter<T> extends BaseAdapter { private Context context;
private ArrayList<T> data;
private LayoutInflater inflater;
private int LayoutId;
easyViewHolder easyholder;
public easyAdapter( Context context,ArrayList<T> data,int layoutid)
{
this.context=context;
this.data=data;
inflater.from(context);
this.LayoutId=layoutid;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
} @Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
} @Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
final easyViewHolder easyholder=getHolder(context, arg1, LayoutId, arg2);
setConverView(easyholder,data.get(arg0));//需要用户复写传值
return easyholder.getConvertView();
}
public abstract void setConverView(easyViewHolder easyViewHolder, T t);
public static easyViewHolder getHolder(Context context,View convertview,int LayoutId,ViewGroup parent)
{
return easyViewHolder.get(context, convertview, LayoutId, parent);
}
}
以上的两个类除了添加新功能不用在对其改变。
接下来我们就可以以最少的代码完成适配器:
package com.example.list_view; import java.util.ArrayList;
import java.util.List; import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView; public class MyAdapter extends easyAdapter<User> { public MyAdapter(Context context, ArrayList<User> data, int layoutId) {
super(context, data, layoutId);
// TODO Auto-generated constructor stub
}
//只需在下面函数传入你所需要的值
public void setConverView(easyViewHolder myViewHolder, User t) {
// TODO Auto-generated method stub ((ImageView)myViewHolder.getView(R.id.imageview)).setImageResource(R.drawable.icon1);
((TextView)myViewHolder.getView(R.id.title)).setText(t.getTitle());
((TextView)myViewHolder.getView(R.id.text)).setText(t.getText()); } }
MainActivity的代码:
for (int i=0;i<10;i++)
{
User user=new User();
user.setIv(R.drawable.icon1);
user.setTitle("用户"+i);
user.setText("1000"+i);
list.add(user);
}
MyAdapter adapter=new MyAdapter(MainActivity.this, list, R.layout.iconlist);
list1.setAdapter(adapter);
只是单纯传入你这个listview所需要的数据源,以及传入listview的布局,便可完成各式各样的listview。