RecyclerView自帶滑動效果,可以實現(xiàn)橫向也可縱向禽炬,這點與listview不同俏拱。默認情況是縱向的,如果需要使用橫向处坪,需要通過setLayoutManager來進行設(shè)置。
這篇文章記錄了使用data binding來實現(xiàn)一個簡單的RecyclerView的過程架专,其中包括兩個布局文件要使用data binding同窘,一個是包含RecyclerView的Activity的布局,另外一個是用于承載RecyclerView item的布局文件部脚。
下面看看使用RecyclerView的關(guān)鍵步驟:
- Activity布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_trigger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刷新列表"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/view_recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
- RecyclerView 布局
這個布局文件用于定義每條View需要顯示的UI元素想邦,這個demo顯示的View從左到右要顯示一個序號(TextView), 一個品牌名(TextView)
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="vm"
type="com.mike.androidtips.recyclerViewDatabinding.DeviceModel" />
</data>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@{vm.rank}"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:text="@{vm.brand}"
/>
</LinearLayout>
</layout>
- 定義數(shù)據(jù)實體Model
package com.mike.androidtips.recyclerViewDatabinding;
import androidx.databinding.ObservableField;
public class DeviceModel {
public ObservableField<String> rank = new ObservableField();
public ObservableField<String> brand = new ObservableField<>();
public DeviceModel(DataEntity dataEntity) {
this.rank.set(dataEntity.getRank());
this.brand.set(dataEntity.getBrand());
}
public void setData(DataEntity dataEntity) {
this.rank.set(dataEntity.getRank());
this.brand.set(dataEntity.getBrand());
}
}
DataEntity
package com.mike.androidtips.recyclerViewDatabinding;
public class DataEntity {
String rank;
String brand;
public DataEntity(String rank, String brand) {
this.rank = rank;
this.brand = brand;
}
@Override
public String toString() {
return "DataEntity{" +
"rank='" + rank + '\'' +
", brand='" + brand + '\'' +
'}';
}
public String getBrand() {
return brand;
}
public String getRank() {
return rank;
}
}
- 自定義一個RecyclerView的adapter,繼承自RecyclerView.Adapter<RecyclerView.ViewHolder>并復(fù)寫其關(guān)鍵方法委刘,自定義的adapter里面需要去自定義一個ViewHolder丧没,看下面代碼:
package com.mike.androidtips.recyclerViewDatabinding;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.mike.androidtips.R;
import com.mike.androidtips.databinding.RecyclerviewItemsBinding;
import java.util.ArrayList;
import java.util.List;
public class RecycleViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final String TAG = RecycleViewAdapter.class.getSimpleName();
List<DataEntity> dataEntities = new ArrayList<>();
public void setDataEntities(List<DataEntity> entities) {
dataEntities = entities;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_items, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
MyViewHolder myViewHolder = (MyViewHolder) holder;
Log.d(TAG, "position = " + position + " , content: " + dataEntities.get(position));
myViewHolder.binding.setVm(new DeviceModel(dataEntities.get(position)));
}
@Override
public int getItemCount() {
return dataEntities.size();
}
private class MyViewHolder extends RecyclerView.ViewHolder {
public RecyclerviewItemsBinding binding;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
binding = RecyclerviewItemsBinding.bind(itemView);
}
}
}
- Activity里面的操作步驟:
5.1 給RecyclerView 設(shè)置setLayoutManager
5.2 實例化一個自定義adapter對象
5.3 給adapter賦初始list
5.4 把adapter設(shè)置給RecyclerView
package com.mike.androidtips.recyclerViewDatabinding;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.mike.androidtips.R;
import com.mike.androidtips.databinding.ActivityRecyclerDatabindingBinding;
import com.mike.androidtips.databinding.ActivityTestDatabindingBinding;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewDataBindingActivity extends Activity {
private final String TAG = RecyclerViewDataBindingActivity.class.getSimpleName();
private RecycleViewAdapter adapter;
ActivityRecyclerDatabindingBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_recycler_databinding);
binding.viewRecyclerview.setLayoutManager(new LinearLayoutManager(this)); //默認是vertical
//比如制作一個水平方向的水果列表,圖標名字上下排列
//binding.viewRecyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
adapter = new RecycleViewAdapter();
List<DataEntity> list = new ArrayList<>();
DataEntity dataEntity = new DataEntity("1", "Apple");
DataEntity dataEntity1 = new DataEntity("2", "Samsung");
DataEntity dataEntity2 = new DataEntity("3", "OPPO");
list.add(dataEntity);
list.add(dataEntity1);
list.add(dataEntity2);
adapter.setDataEntities(list);
binding.viewRecyclerview.setAdapter(adapter);
initUI();
}
private void initUI() {
binding.btnTrigger.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<DataEntity> list = new ArrayList<>();
DataEntity dataEntity = new DataEntity("1", "Samsung");
DataEntity dataEntity1 = new DataEntity("2", "chuanyin");
DataEntity dataEntity2 = new DataEntity("3", "chuizi");
DataEntity dataEntity3 = new DataEntity("4", "vivo");
list.add(dataEntity);
list.add(dataEntity1);
list.add(dataEntity2);
list.add(dataEntity3);
adapter.setDataEntities(list);
adapter.notifyDataSetChanged();
}
});
}
}
-
運行結(jié)果
注:最初在調(diào)試時候遇到了問題:無論如何修改锡移,整個list顯示的都是最后一組數(shù)據(jù)
原因:DeviceModel里的數(shù)據(jù)被定義成了static, 干掉static即解決了問題
public static ObservableField<String> rank = new ObservableField();
public static ObservableField<String> brand = new ObservableField<>();