如何在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);

        }

    }

}

由于我对 android studio 和 JAVA 很陌生,所以我没有找到这方面的指南。我添加了 2 个函数,这些函数添加到 Adapter 的不同列表中,但结果是一团糟。到处都在添加文本,并且项目被复制。



幕布斯6054654
浏览 143回答 1
1回答

慕码人2483693

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

相关分类

Java