启用 DataBinding
app build.gradle 配置启用?DataBinding
android {
...
buildFeatures {
dataBinding = true
}
...
}
主布局
?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
item 布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="obSwordsman"
type="com.imooc.book.papger11.ObSwordsman" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:text="@{obSwordsman.name}"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:text="@{obSwordsman.age}"
android:layout_height="wrap_content"/>
</LinearLayout>
</layout>
Model 实体类
public class ObSwordsman {
public ObservableField<String> name = new ObservableField();
public ObservableField<String> age = new ObservableField();
public ObSwordsman(String name, String age) {
this.name.set(name);
this.age.set(age);
}
public ObservableField<String> getName() {
return name;
}
public void setName(ObservableField<String> name) {
this.name = name;
}
public ObservableField<String> getAge() {
return age;
}
public void setAge(ObservableField<String> age) {
this.age = age;
}
}
Adapter
public class BindingAdapter extends RecyclerView.Adapter<BindingAdapter.BindingViewHolder> {
private List<ObSwordsman> datas;
public BindingAdapter(List<ObSwordsman> datas) {
this.datas = datas;
}
@NonNull
@Override
public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ItemSwordsmanBinding itemBinding =
DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_swordsman,
parent,false);
return new BindingViewHolder(itemBinding);
}
@Override
public void onBindViewHolder(@NonNull BindingViewHolder holder, int position) {
holder.getBinding().setObSwordsman(datas.get(position));
}
@Override
public int getItemCount() {
return datas.size();
}
public class BindingViewHolder extends RecyclerView.ViewHolder{
private ItemSwordsmanBinding binding;
public BindingViewHolder(ItemSwordsmanBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public ItemSwordsmanBinding getBinding() {
return binding;
}
}
}
基本使用
public class DataBindingActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_databinding);
RecyclerView recyclerView = findViewById(R.id.recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
BindingAdapter adapter = new BindingAdapter(getData());
recyclerView.setAdapter(adapter);
}
private List<ObSwordsman> getData() {
List<ObSwordsman> datas = new ArrayList<>();
datas.add(new ObSwordsman("jake11","12"));
datas.add(new ObSwordsman("jake12","13"));
return datas;
}
}
|