1 回答
TA贡献1860条经验 获得超9个赞
添加项目ArrayList<Dress>,然后设置DressAdapter唯一一次。
有了@Bindable字段,就可以对卡片视图进行数据绑定。
class Dress extends BaseObservable {
private String title;
private String desc;
/** Constructor */
public Dress() {}
/** Constructor, new instance from Firestore {@link QueryDocumentSnapshot} */
public Dress(@NonNull QueryDocumentSnapshot snapshot) {
this.fromSnapshot(snapshot);
}
public void setTitle(@NonNull String value) {
boolean changed = !TextUtils.equals(this.title, value);
if(changed) {
this.title = value;
notifyPropertyChanged(BR.title);
}
}
public void setDesc(@NonNull String value) {
boolean changed = !TextUtils.equals(this.desc, value);
if(changed) {
this.desc = value;
notifyPropertyChanged(BR.desc);
}
}
@Bindable
public String getTitle() {
return this.title;
}
@Bindable
public String getDesc() {
return this.desc;
}
public void fromSnapshot(@NonNull QueryDocumentSnapshot snapshot) {
this.setTitle(String.valueOf(snapshot.getData().get("title")));
this.setDesc(String.valueOf(snapshot.getData().get("description")));
}
}
添加项目:
ArrayList<Dress> mItems = new ArrayList<>();
...
if (task.isSuccessful()) {
for (QueryDocumentSnapshot snapshot : Objects.requireNonNull(task.getResult())) {
mItems.add(new Dress(snapshot));
}
recyclerView.setAdapter(new dressAdapter(mItems));
}
onBindViewHolder()正在为每个项目调用。
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="dress" type="com.acme.model.Dress"/>
</data>
<com.google.android.material.card.MaterialCardView
android:id="@+id/cardview"
style="@style/Widget.MaterialComponents.CardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:color="?android:colorControlHighlight"
android:fontFamily="sans-serif-medium"
android:text="@{dress.title}"
android:textColor="#000000"
android:textSize="16sp"
tools:text="@string/tools_dress_title"/>
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:color="?android:colorControlHighlight"
android:fontFamily="sans-serif-medium"
android:text="@{dress.desc}"
android:textColor="#000000"
android:textSize="16sp"
tools:text="@string/tools_dress_desc"/>
</androidx.appcompat.widget.LinearLayoutCompat>
</com.google.android.material.card.MaterialCardView>
</layout>
添加回答
举报