recyclerview中使用DataBinding

1.app模块gradle配置:

android {
 ...
    dataBinding {
        enabled = true
    }

 ...
}

2.新建item布局,item布局文件item_list.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="book"
            type="com.example.jetpackStudy.databinding.Book" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:text='@{book.name   + @string/blankspace + book.price}' />
    </RelativeLayout>
</layout>

注意:textview设置文本有好几种写法,以上是用单引号,也就是'@{book.name   + @string/blankspace + book.price}',其中@string/blankspace会显示3个空格。

strings.xml的blankspace:

 <string name="blankspace">&#160;&#160;&#160;</string>

3.新建activity的布局文件,activity的布局文件activity_recyclerview.xml代码如下:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </RelativeLayout>
</layout>

4.新建Adapter,Adapter代码如下:

public class MyBookAdapter extends RecyclerView.Adapter<MyBookAdapter.MyViewHolder> {

    private  List<Book> books;

    public MyBookAdapter(List<Book> books){
        this.books = books;
    }
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemListBinding itemListBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext())
                , R.layout.item_list,parent,false);
        return new MyViewHolder(itemListBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        Book book  = books.get(position);
        holder.itemListBinding.setBook(book);
    }

    @Override
    public int getItemCount() {
        return books == null?0:books.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ItemListBinding itemListBinding;
        
        public MyViewHolder(@NonNull ItemListBinding itemView) {
            super(itemView.getRoot());
            itemListBinding = itemView;
        }
    }

}

5.Activity的布局文件如下:

public class RecyclerviewActivity extends AppCompatActivity {
    private List<Book> books = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityRecyclerviewBinding activityRecyclerviewBinding
                = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);

        for (int i = 0; i < 50; i++) {
            Book book = new Book("book "+ i,"¥100"+i+".00");
            books.add(book);
        }
        activityRecyclerviewBinding.rv.setLayoutManager(new LinearLayoutManager(this));
        activityRecyclerviewBinding.rv.setAdapter(new MyBookAdapter(books));
    }
}

6.Activity的布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </RelativeLayout>
</layout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值