基于dataBinding的万能adapter

先看实现的布局:很简单的一个布局,这里主要是用了一下databinding的数据绑定和事件绑定

基于dataBinding的万能adapter_第1张图片
Screenshot_SmartSelect_2017-07-19-11-30-37.png



    

        

        
    

    

        

        


        

            

            
        


    



封装的adapter:

public abstract class BaseBindingAdapter extends BaseAdapter {
    private List list;
    protected Context context;
    int itemId;
    public BaseBindingAdapter(Context context, List list, int itemId) {
        this.list = list;
        this.context = context;
        this.itemId = itemId;
    }

    public BaseBindingAdapter(Context context) {
        this.context = context;
    }
    public List getList(){
        return this.list;
    }
    public void setList(List list) {
        this.list = list;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return list == null ? 0 :list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        W w;
        if(convertView == null){
            convertView = View.inflate(context,itemId,null);
            w = DataBindingUtil.bind(convertView);
            convertView.setTag(w);
        }
        if (list != null && list.get(position) != null) {
            w = (W) convertView.getTag();
            T t = list.get(position);
            bindObj(w, t);
            operateView(convertView,t);
        }

        return convertView;
    }
    public abstract void bindObj(W w,T t);
    public void operateView(View view, T t){
    }

}

说明:
1、在布局里面绑定事件和数据:

android:text="@{obj.nickname}"
 android:onClick="@{(view)->event.playOnClick(view,obj)}"

2、拓展的方法

//这里可以拿到相应位置的view和data进行其他操作
            @Override
            public void operateView(View view, MusicListBean.DataBean dataBean) {
                super.operateView(view, dataBean);
            }

用法:
1、在写好上面的布局itembinding后,构建一下,会自动生成ItemBinding类
定义全局变量:
BaseBindingAdapter adapter ;
2、自定义事件,比如这里要实现点击播放和删除:

 public class ClickBinding {
        public void playOnClick(View view, MusicListBean.DataBean dataBean) {
            //播放音乐
        }

        public void deleOnClick(View view, MusicListBean.DataBean dataBean) {
            //删除当前条目
        }
    }

3、创建适配器,传入相应参数,给listView(也可以是gridview之类的)设置adapter

adapter = new BaseBindingAdapter(this, selectedList, R.layout.itembinding) {

            @Override
            public void bindObj(ItemBinding itemBinding, MusicListBean.DataBean dataBean) {
                               itemBinding.setObj(dataBean);//绑定数据
                               itemBinding.setEvent(new ClickBinding());//绑定事件
                }
            }
        };

        listView.setAdapter(adapter);

4、更新adapter:在请求到服务器数据后:

adapter.setList(list);//list是数据源,setlist方法里面调用了notifyDataSetChanged

·········同样的,用这个adapter实现的其他布局:

基于dataBinding的万能adapter_第2张图片
Screenshot_SmartSelect_2017-07-19-11-24-25.png
基于dataBinding的万能adapter_第3张图片
image.png

你可能感兴趣的:(基于dataBinding的万能adapter)