java - 如何在android RecyclerView中使用onBindViewHolder与多个项目
问题描述
我正在创建一个包含 CardView 的 RecyclerView,每张卡片都有标题、描述和图像。使用onBindViewHolder
允许我只添加一个标题,我不能让它添加其他项目。这是我尝试做到的方式。
这是我的适配器:
public class userDressAdapter extends RecyclerView.Adapter<userDressAdapter.ViewHolder> {
private LayoutInflater layoutInflater;
private List<String> data;
private Object ViewGroup;
userDressAdapter(Context context, List<String> data) {
this.layoutInflater = LayoutInflater.from(context);
this.data = data;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = layoutInflater.inflate(R.layout.dress_recycler_view, (android.view.ViewGroup) ViewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
String title = data.get(position);
ViewHolder.dressTitle.setText(title);
ViewHolder.dressDescription.setText(title);
}
@Override
public int getItemCount() {
return data.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
static TextView dressTitle;
static TextView dressDescription;
public ViewHolder(@NonNull View itemView) {
super(itemView);
dressTitle = itemView.findViewById(R.id.userDressTitleText);
dressDescription = itemView.findViewById(R.id.userDressDescriptionText);
}
}
}
我使用它作为我视图的主适配器,这是我从主 Java 类调用它的方式:
public class UserDressView extends AppCompatActivity {
private static final String TAG = "UserDressView";
RecyclerView recyclerView;
com.innoventiq.fostania.userDressAdapter userDressAdapter;
ArrayList<String> items;
ArrayList<String> description;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_dress_view);
items = new ArrayList<>();
description = new ArrayList<>();
ReadDressData();
}
public void AddDressToList(String theData) {
items.add(theData);
recyclerView = findViewById(R.id.userDressRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
userDressAdapter = new userDressAdapter(this, items);
recyclerView.setAdapter(userDressAdapter);
}
public void AddDressDescriptionToList(String theData) {
description.add(theData);
recyclerView = findViewById(R.id.userDressRecyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
userDressAdapter = new userDressAdapter(this, description);
recyclerView.setAdapter(userDressAdapter);
}
public void ReadDressData() {
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("dress")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult())) {
Log.d(TAG, document.getId() + " => " + document.getData());
AddDressToList(String.valueOf(document.getData().get("title")));
AddDressDescriptionToList(String.valueOf(document.getData().get("description")));
}
} else {
Log.w(TAG, "Error getting documents.", task.getException());
}
}
});
}
}
由于我对 android studio 和 JAVA 很陌生,所以我没有找到这方面的指南。我向 Adapter 添加了 2 个添加到不同列表的函数,但结果是一团糟。到处都在添加文本,并且项目被复制。
解决方案
添加项目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>
推荐阅读
- angular - 如何在角度 6 中的日期变量中添加 24 小时
- java - org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException:来自服务器的错误
- c# - 带有 LUIS 和 WaterFall 对话框的 Azure Bot Framework Bot 以异常方式执行。意外的对话流
- javascript - 我无法将标题转换为在 IOS 上工作
- python - joblib.Parallel 是否保持传递数据的原始顺序?
- node.js - Firestore (6.2.0) 内部未处理错误
- javascript - 什么是javascript中的基础对象
- python - 如何使硒不等待页面加载一次?
- python-3.x - 如何提取在谷歌浏览器中查看页面源时未显示的信息?
- java - 有没有办法在 WebView 应用程序中创建还原点?