第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在android RecyclerView中將onBindViewHolder與多個(gè)項(xiàng)目一起使用

如何在android RecyclerView中將onBindViewHolder與多個(gè)項(xiàng)目一起使用

幕布斯6054654 2023-06-08 19:25:54
我正在創(chuàng)建一個(gè)帶有 CardView 的 RecyclerView,每張卡片都有標(biāo)題、描述和圖像。使用onBindViewHolder允許我只添加一個(gè)標(biāo)題,我不能讓它添加其他項(xiàng)目。這是我嘗試這樣做的方法。這是我的適配器: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);        }    }}由于我對(duì) android studio 和 JAVA 很陌生,所以我沒(méi)有找到這方面的指南。我添加了 2 個(gè)函數(shù),這些函數(shù)添加到 Adapter 的不同列表中,但結(jié)果是一團(tuán)糟。到處都在添加文本,并且項(xiàng)目被復(fù)制。
查看完整描述

1 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超9個(gè)贊

添加項(xiàng)目ArrayList<Dress>,然后設(shè)置DressAdapter唯一一次。


有了@Bindable字段,就可以對(duì)卡片視圖進(jìn)行數(shù)據(jù)綁定。


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")));

    }

}

添加項(xiàng)目:


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()正在為每個(gè)項(xiàng)目調(diào)用。


<?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>



查看完整回答
反對(duì) 回復(fù) 2023-06-08
  • 1 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)