自定义BaseAdapter
在自定义基类之前,先简单分析一下,我们须要自定义一个支撑单种视图的Adapter,还要自定义一个支撑多种视图类型的Adapter,两个类都要持续BaseAdapter,先将两个类都公用的部分采掏出来定义为MyAdapter。
- public abstract class MyAdapter<T> extends BaseAdapter {
- protected List<T> dataList = new ArrayList<>();
- protected Context context;
- protected LayoutInflater inflater;
- public MyAdapter(Context context) {
- this.context = context;
- inflater = LayoutInflater.from(context);
- }
- public void setDataList(List<T> dataList) {
- this.dataList = dataList;
- notifyDataSetChanged();
- }
- @Override
- public int getCount() {
- if (null == dataList) {
- return 0;
- }
- return dataList.size();
- }
- @Override
- public T getItem(int position) {
- return dataList.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- }
在RecyclerView的Adapter实现中是没有getView()办法的,下面我们就分析一下getView()办法若何拆分,一般情况下我们在实现getView()办法都是如下贱程。
- public View getView(int position,
推荐阅读
Relations 本文译自 What is a NoSQL Database? Learn By Writing>NoSQL 这个词在近些年正变得到处可见. 然则到底 "NoSQL" 指的是什么? 它是若何并且为什么这么竽暌剐用? 在本文, 我们将会>>>详细阅读
本文标题:Android自定义BaseAdapter最佳实践
地址:http://www.17bianji.com/lsqh/35304.html
1/2 1