Android BaseAdapter 封装总结(一)

Android BaseAdapter 封装总结

针对BaseAdapter的封装一般有两类,一种是ListView的Adapter,还有一种是RecyclerView的Adapter。两种Adapter封装的原理都类似。

封装的方式主要包含两方面:

  • 一种是对ViewHolder的封装

    • 具体的实现参考GitHub上开源库:base-adapter-helper
  • 还有就是针对多种类型的viewType,通过模块代理(Delegate)的方式,将不同类型的viewType组装起来。

    • 具体的实现参考GitHub上开源库:AdapterDelegates

当然Adapter的封装还可以做更多的工作,比如说:Sticky Head加载更多 ...

两种封装方式在ListView上的具体实现

1. BaseViewHolder的封装

先看一下普通的ListView的适配器代码

public class SimpleListAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private List dataList;


    public SimpleListAdapter(Context context, List dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.simple_list_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.id_tv_title.setText("hello");
        return convertView;
    }


    public static class ViewHolder {
        public View rootView;
        public TextView id_tv_title;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.id_tv_title = (TextView) rootView.findViewById(R.id.id_tv_title);
        }
    }

第一步,提供获取BaseViewHolder实例对象的静态方法

先看下SimpleListAdaptergetView()方法中的一段代码:

ViewHolder viewHolder = null;
if (convertView == null) {
    convertView = layoutInflater.inflate(R.layout.simple_list_item, parent, false);
    viewHolder = new ViewHolder(convertView);
    convertView.setTag(viewHolder);
} else {
    viewHolder = (ViewHolder) convertView.getTag();
}

这段代码重复率非常高,主要作用是:获取与convertView 绑定(bind)在一起的ViewHolder

可以将这段代码提取到我们的BaseViewHolder中,于是我们开始写BaseViewHolder

public class BaseViewHolder {


    private View convertView;//adapter中getView() 返回的对象,用于与ListView的数据绑定

    public BaseViewHolder(View convertView) {
        this.convertView = convertView;
    }

    public static BaseViewHolder getViewHolder(Context context, int position, View convertView, ViewGroup parent, @LayoutRes int resource) {
        BaseViewHolder baseViewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(resource, parent, false);
            baseViewHolder = new BaseViewHolder(convertView);
            convertView.setTag(baseViewHolder);
        } else {
            baseViewHolder = (BaseViewHolder) convertView.getTag();
        }
        return baseViewHolder;
    }

    public View getConvertView() {
        return convertView;
    }
}

此时我们的SimpleListAdaptergetView()方法中的代码将得到初步的简化

由原来的

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.simple_list_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.id_tv_title.setText("hello");
        return convertView;
    }

简化后的代码如下:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseViewHolder baseViewHolder = BaseViewHolder.getViewHolder(context, position, convertView, parent, R.layout.simple_list_item);
        return baseViewHolder.getConvertView();
    }

第二步,BaseViewHolder提供获取convertViewviews的方法,以便在adapter中绘制数据

convertView中的Views我们可以通过如下方式获取到:

    private SparseArrayCompat viewSparseArrayCompat = new SparseArrayCompat<>();
    
    public View getViewByViewId(@IdRes int viewId) {
        View view = viewSparseArrayCompat.get(viewId);
        if (view == null) {
            view = convertView.findViewById(viewId);
            viewSparseArrayCompat.put(viewId, view);
        }
        return view;
    }


至此我们的BaseViewHolder封装完成,完成后的代码如下:

public class BaseViewHolder {

    private View convertView;//adapter中getView() 返回的对象,用于与ListView的数据绑定
    private SparseArrayCompat viewSparseArrayCompat = new SparseArrayCompat<>();

    public BaseViewHolder(View convertView) {
        this.convertView = convertView;
    }

    public static BaseViewHolder getViewHolder(Context context, int position, View convertView, ViewGroup parent, @LayoutRes int resource) {
        BaseViewHolder baseViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(resource, parent, false);
            baseViewHolder = new BaseViewHolder(convertView);
            convertView.setTag(baseViewHolder);
        } else {
            baseViewHolder = (BaseViewHolder) convertView.getTag();
        }
        return baseViewHolder;
    }



    public View getViewByViewId(@IdRes int viewId) {
        View view = viewSparseArrayCompat.get(viewId);
        if (view == null) {
            view = convertView.findViewById(viewId);
            viewSparseArrayCompat.put(viewId, view);
        }
        return view;
    }

    public View getConvertView() {
        return convertView;
    }
}

SimpleListAdapter中使用如下, 重点看getView()方法:

public class SimpleListAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private List dataList;


    public SimpleListAdapter(Context context, List dataList) {
        this.context = context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseViewHolder baseViewHolder = BaseViewHolder.getViewHolder(context, position, convertView, parent, R.layout.simple_list_item);
        TextView textView = (TextView) baseViewHolder.getViewByViewId(R.id.id_tv_title);
        textView.setText("Hello");
        return baseViewHolder.getConvertView();
    }

}

封装好BaseViewHolder之后,我们开始封装我们的Adapter,定义为CommonAdapter 封装Adapter的代码就比较简单了,直接上封装后的代码

public abstract class CommonAdapter extends BaseAdapter {

    private Context context;
    private int layoutResId;
    private List dataList;

    public CommonAdapter(@NonNull Context context, @LayoutRes int layoutResId, @NonNull List dataList) {
        this.context = context;
        this.layoutResId = layoutResId;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        BaseViewHolder baseViewHolder = BaseViewHolder.getViewHolder(context, position, convertView, parent, layoutResId);
        convert(baseViewHolder, dataList.get(position));
        return baseViewHolder.getConvertView();
    }

    protected abstract void convert(BaseViewHolder baseViewHolder, T t);
}

FragmentListView使用CommonAdapter设置Adapter


public class MyBaseAdapterFragment extends BaseFragment {

    private List mDatas = new ArrayList<>(Arrays.asList("Hello", "World", "Welcome"));

    @BindView(R.id.lv)
    ListView listView;


    public static MyBaseAdapterFragment newInstance() {
        MyBaseAdapterFragment fragment = new MyBaseAdapterFragment();
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_listivew_baseadapter, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }


    private void initView() {
        listView.setAdapter(new CommonAdapter(getContext(), R.layout.simple_list_item, mDatas) {

            @Override
            protected void convert(BaseViewHolder baseViewHolder, String s) {
                TextView title = (TextView) baseViewHolder.getViewByViewId(R.id.id_tv_title);
                title.setText(s);
            }
        });
    }
}

你可能感兴趣的:(Android BaseAdapter 封装总结(一))