DataBinding绑定ListView列表(4)

Listview和GridView等都是我们常用的列表,使用次数可想而知,而我们经常需要做的就是为它们挨个配上适配器,不过DataBinding绑定之后,你基本上只需要这一个适配器了,帮你省下来大把的功夫。

首先定义适配器

public class MyBaseAdapter<T> extends BaseAdapter {
    private Context context;
    private LayoutInflater inflater;
    private int layoutId;
    private int variableId;
    private List<T> list;
//Context 上下文,layoutID布局文件,List集合,resID相当于R文件中的id
 public MyBaseAdapter(Context context, int layoutId, List<T> list, int resId) {
        this.context = context;
        this.layoutId = layoutId;
        this.list = list;
        this.variableId = resId;
        inflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return 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) {
        ViewDataBinding dataBinding;
        if (convertView==null){
            dataBinding= DataBindingUtil.inflate(inflater,layoutId,parent,false);

        }else {
        dataBinding=DataBindingUtil.getBinding(convertView);
        }
        dataBinding.setVariable(variableId,list.get(position));
        return dataBinding.getRoot();
    }
}
 然后是布局
<variable
    name="adapter"
    type="qnkj.cn.mylianxi.adapter.MyBaseAdapter"/>
<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:adapter="@{adapter}"
    >
然后就是自己的Activity
private List<FoodBean> food;
private void initDate() {
    food=new ArrayList<>();
  for (int a=0;a<20;a++){
//姓名,网络图片地址,字符串,字符串,FoodBean里就是四个字符串,自己随意改
 FoodBean f=new FoodBean("张三"+a,URL,"","王五");
  food.add(f);
  }
//适配器(上下文,布局文件,集合,BR相当于R文件)最后的BR.food找到的是自己定义的条目的绑定名字
 MyBaseAdapter<FoodBean> adapter=new MyBaseAdapter<>(MainActivity.this,R.layout.item_lv,
            food,qnkj.cn.mylianxi.BR.food
    );
 binding.setAdapter(adapter);
    //列表的条目点击事件跳转,不用找findviewByID,用binding直接找就可以了,只要你记得自己定义的控件ID
    binding.lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent=new Intent(MainActivity.this,MyActivity.class);
            intent.putExtra("name",food.get(position).getDescription());
            intent.putExtra("age",food.get(position).getSummary());
            startActivity(intent);
        }
    });

}
条目布局
<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <data>
        <variable
            name="food"
            type="qnkj.cn.mylianxi.bean.FoodBean"/>

    </data>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical"
    >
    <ImageView
        android:id="@+id/iv"
        android:layout_width="96dp"
        android:layout_height="96dp"
        app:img="@{food.img}"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_toRightOf="@id/iv"
        android:ellipsize="end"
        android:maxLines="3"
        android:text="@{food.description}"
        />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_toRightOf="@id/iv"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="2dp"
    android:text="@{food.keywords}"
    android:textStyle="bold"
    />



</RelativeLayout>
    </layout>
这样一来,不同的条目布局,只要在适配器里传入不同的布局就可以了,而布局内部的文件绑定好Bean类,传入Bean集合会自动将
条目对应上去,这样一来就只需要一个适配器了,是不是很省事?


猜你喜欢

转载自blog.csdn.net/Liu_ser/article/details/80707899
今日推荐