标签:
在Android开发中,我们经常会用到ListView、GridView,每次编码的时候都需要为他们写对应的Adapter,写多了就感觉很烦躁了,因为基本的编程思想都是一样的,但是每次都要重复去写,所以我们能不能把它们抽象成一个通用的模板,这样就不用每次都重复写相同的代码了,直接重复使用,这样不是更好,下面我们就来介绍介绍一个开源项目base-adapter-helper。public View getView(int pos, View convertView, ViewGroup parent){
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text));
holder.icon = (ImageView) convertView.findViewButId(R.id.icon));
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.text.setText(DATA[pos]);
holder.icon.setImageBitmap((pos & 1) == 1 ? mIcon1 : mIcon2);
return convertView;
}
static class ViewHolder {
TextView text;
ImageView icon;
}github链接:base-adapter-helper
可以看到BaseQuickAdapter继承自BaseAdapter,同样我们重点关注它的getView函数。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == 0) {
final H helper = getAdapterHelper(position, convertView, parent);
T item = getItem(position);
helper.setAssociatedObject(item);
convert(helper, item);
return helper.getView();
}
return createIndeterminateProgressView(convertView, parent);
}
private View createIndeterminateProgressView(View convertView, ViewGroup parent) {
if (convertView == null) {
FrameLayout container = new FrameLayout(context);
container.setForegroundGravity(Gravity.CENTER);
ProgressBar progress = new ProgressBar(context);
container.addView(progress);
convertView = container;
}
return convertView;
}static BaseAdapterHelper get(Context context, View convertView, ViewGroup parent, int layoutId, int position) {
if (convertView == null) {
return new BaseAdapterHelper(context, parent, layoutId, position);
}
// Retrieve the existing helper and update its position
BaseAdapterHelper existingHelper = (BaseAdapterHelper) convertView.getTag();
existingHelper.position = position;
return existingHelper;
}protected BaseAdapterHelper(Context context, ViewGroup parent, int layoutId, int position) {
this.context = context;
this.position = position;
this.views = new SparseArray<View>();
convertView = LayoutInflater.from(context) //
.inflate(layoutId, parent, false);
convertView.setTag(this);
}在BaseAdapterHelper的构造函数里面,定义了一个views,它其实就是传统Adapter里面的那个ViewHolder用来存放Item里面的各个view。convertView为我们要显示的Item的View,接着通过setTag函数将BaseAdapterHelper对象本身关联到convertView上面,所以我们知道每个Item对象都关联了一个BaseAdapterHelper对象。public QuickAdapter(Context context, int layoutResId, List<T> data) {
super(context, layoutResId, data);
}可以看到我们传入Item布局的layoutResId和要显示的数据data,data是List类型的。protected BaseAdapterHelper(Context context, ViewGroup parent, int layoutId, int position) {
this.context = context;
this.position = position;
this.views = new SparseArray<View>();
convertView = LayoutInflater.from(context) //
.inflate(layoutId, parent, false);
convertView.setTag(this);
}BaseAdapterHelper existingHelper = (BaseAdapterHelper) convertView.getTag(); existingHelper.position = position;他直接得到BaseAdapterHelper对象,然后重新设置它对应的位置postion,因为BaseAdapterHelper对象中引用到了重用的convertView,这样就可以直接使用这个view的Item了。
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<String> data = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
data.add("text1");
data.add("text2");
data.add("text3");
data.add("text4");
data.add("text5");
data.add("text6");
listView = (ListView) findViewById(R.id.listview);
QuickAdapter adapter = new QuickAdapter<String>(this, R.layout.item, data) {
@Override
protected void convert(BaseAdapterHelper helper, String item) {
helper.setText(R.id.textView, item);
}
};
listView.setAdapter(adapter);
}
}
Android中的万能适配器——base-adapter-helper解析
标签:
原文地址:http://blog.csdn.net/hp910315/article/details/51055402