架构DatabindingAndroid知识

打造基于Databinding与RecyclerView的通用A

2017-02-19  本文已影响1551人  码农明明

一 想法产生

做Android开发的童鞋都知道,展示一个列表页面需要以下几个步骤:
- 创建一个ListView或RecyclerView

二 Adapter实现思路与事件(使用RecyclerView )

数据: 数据可以使用泛型

private List<T1> list

构造函数: Adapter需要绑定Item布局,可以在构造函数中将布局作为参数传进去。添加@LayoutRes注解指明布局资源。

 public BaseAdapter(Context context, @LayoutRes int layout){
  inflater=LayoutInflater.from(context);
  this.context=context;
  this.layout= layout;
  list=new ArrayList<>();

}
  重写父方法:onCreateViewHolder、getItemCount
@Override
public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  View view = inflater.inflate(layout,parent,false);
  return new BaseViewHolder(view);
}
@Override
public int getItemCount() {
  return list.size();
}

ViewHolder创建:生成ViewDataBinding并提供get方法(注意不要漏掉static)

public static class BaseViewHolder extends RecyclerView.ViewHolder {
  private ViewDataBinding b;
  public BaseViewHolder(View itemView) {
  super(itemView);
  b=DataBindingUtil.bind(itemView);
  }
  public ViewDataBinding getBinding() {
  return b;
  }
}

回调接口:用于ViewDataBinding的回调,并在onBindViewHolder中将得到的holder与position回调出去

public interface BindView<T2> {
  void onBindViewHolder(T2 b,int position);
}

@Override
public void onBindViewHolder(BaseViewHolder holder, int position) {
  bindView.onBindViewHolder((T2) holder.getBinding(),position);
}

使用:为adapter创建回调。

adapter.setOnBindViewHolder(new BaseAdapter.BindView<ItemActionBinding>() {
  @Override
  public void onBindViewHolder(ItemActionBinding b, int position) {
  b.setAction(actionInfos.get(position));
  }
});

三 Adapter全部代码

package com.sunmoon.helper.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class BaseAdapter<T1, T2 extends ViewDataBinding> extends RecyclerView.Adapter<BaseAdapter.BaseViewHolder> {
    private List<T1> list;
    private LayoutInflater inflater;
    private Context context;
    @LayoutRes
    private int layout;
    private BindView<T2> bindView;

    public BaseAdapter(Context context, @LayoutRes int layout) {
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.layout = layout;
        list = new ArrayList<>();

    }

    public void initList(List<T1> list) {
        this.list = list;
    }

    public void setOnBindViewHolder(BindView<T2> bindView) {
        this.bindView = bindView;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(layout, parent, false);
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, int position) {
        bindView.onBindViewHolder((T2) holder.getBinding(), position);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface BindView<T2> {
        void onBindViewHolder(T2 b, int position);
    }

    public static class BaseViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding b;

        public BaseViewHolder(View itemView) {
            super(itemView);
            b = DataBindingUtil.bind(itemView);
        }

        public ViewDataBinding getBinding() {
            return b;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读