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">   </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>
|