今天會使用databinding的方式去實作一個Recyclerview。
public class DBData {
public final ObservableField<String> name=new ObservableField<>();
public final ObservableField<String> sex=new ObservableField<>();
public final ObservableField<Integer> age=new ObservableField<>();
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="data"
type="com.example.newtest.datadindRecyclerviewTest.DBData" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/UserName"
android:textSize="30sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{data.name}"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/UserSex"
android:textSize="30sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{data.sex}"
android:textSize="30sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/UserAge"
android:textSize="30sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(data.age)}"
android:textSize="30sp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/colorPrimary"/>
</LinearLayout>
</layout>
這裡會發現在data.age
加了一個String.valueOf()
,data.age
是int,如果不加String.valueOf()
會報錯。
public class BindRecyclerViewAdapter extends RecyclerView.Adapter<BindRecyclerViewAdapter.ViewHolder> {
AppCompatActivity activity;
List<DBData> dBDataList;
public BindRecyclerViewAdapter(AppCompatActivity activity,List<DBData> dBDataList){
this.activity=activity;
this.dBDataList=dBDataList;
}
@Override
public ViewHolder onCreateViewHolder( ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(activity);
ItemDataListBinding binding=ItemDataListBinding.inflate(inflater,parent,false);
return new ViewHolder(binding);
}
@Override
public void onBindViewHolder( ViewHolder holder, int position) {
DBData dbData =dBDataList.get(position);
holder.bind(dbData);
}
@Override
public int getItemCount() {
return dBDataList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ItemDataListBinding itemDataListBinding;
public ViewHolder(ItemDataListBinding binding) {
super(binding.getRoot());
itemDataListBinding=binding;
}
void bind(DBData dbData){
itemDataListBinding.setData(dbData);
itemDataListBinding.executePendingBindings();
}
}
}
將ViewHolder的參數從view改成ItemDataListBinding,onBindViewHolder
將原本用setText的方式顯示UI改成bind(dbData)
,而在bind(dbData)
記得要加executePendingBindings()
,executePendingBindings()
會立即更新UI。
設定要傳入的資料
private void init() {
recyclerView=findViewById(R.id.recyclerview1);
for (int i=1;i<11;i++){
DBData dBdata=new DBData();
dBdata.name.set("eles"+i);
dBdata.sex.set("boy");
dBdata.age.set(i+15);
dBDataList.add(dBdata);
}
bindRecyclerViewAdapter =new BindRecyclerViewAdapter(this,dBDataList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(bindRecyclerViewAdapter);
}
MainActivity完整程式碼
public class MainActivity3 extends AppCompatActivity {
BindRecyclerViewAdapter bindRecyclerViewAdapter;
RecyclerView recyclerView;
ArrayList<DBData> dBDataList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
init();
}
private void init() {
recyclerView=findViewById(R.id.recyclerview1);
for (int i=1;i<11;i++){
DBData dBdata=new DBData();
dBdata.name.set("eles"+i);
dBdata.sex.set("boy");
dBdata.age.set(i+15);
dBDataList.add(dBdata);
}
bindRecyclerViewAdapter =new BindRecyclerViewAdapter(this,dBDataList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(bindRecyclerViewAdapter);
}
}