【问题标题】:radio button state is not retaining in recycler view单选按钮状态未保留在回收站视图中
【发布时间】:2018-01-08 20:15:40
【问题描述】:

这里的问题是,每当我向上滚动回收站视图并返回顶部时,选定的单选按钮就会消失。同样,当重复使用回收站视图卡时,单选按钮会在新卡中显示为选中状态。

以下是我在 recyclerview 中使用的视图及其 xml

radioButton.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/dimen_10dp"
        android:background="@drawable/pdp_card_background"
        android:orientation="vertical"
        android:padding="@dimen/dimen_10dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.android.sea.corenative.customviews.MenuIconTextView
                android:id="@+id/rating_star"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="AAAAA"
                android:textColor="@color/star_color"
                android:textSize="@dimen/txt_12sp" />

            <com.android.sea.corenative.customviews.NewTextFieldNormal
                android:id="@+id/review_helpful_count"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:text="@string/review_helpful_count"
                android:textColor="@color/ratings_text_color"
                android:textSize="@dimen/txt_12sp" />

        </LinearLayout>

        <com.android.sea.corenative.customviews.NewTextViewSemiBold
            android:id="@+id/review_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dimen_10dp"
            android:text="Not that bad for the price"
            android:textColor="@color/ratings_text_color"
            android:textSize="@dimen/txt_14sp" />

        <com.android.sea.corenative.customviews.NewTextFieldNormal
            android:id="@+id/review_author_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dimen_5dp"
            android:lineSpacingMultiplier="1.42"
            android:text="By Enovak"
            android:textColor="@color/ratings_and_review_author_text"
            android:textSize="@dimen/txt_12sp" />

        <com.android.sea.corenative.customviews.NewTextFieldNormal
            android:id="@+id/review_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingMultiplier="1.42"
            android:text="Fort Johnson, NY | Tue, May 16, 2017"
            android:textColor="@color/ratings_and_review_author_text"
            android:textSize="@dimen/txt_12sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="5dp"
            android:layout_marginBottom="@dimen/dimen_10dp"
            android:layout_marginTop="@dimen/dimen_10dp"
            android:background="@drawable/dotted"
            android:layerType="software" />

        <LinearLayout
            android:id="@+id/verified_purchase_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.android.sea.corenative.customviews.MenuIconTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:text="G"
                android:textColor="@color/ratings_text_color"
                android:textSize="@dimen/txt_12sp" />

            <com.android.sea.corenative.customviews.NewTextViewSemiBold
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/dimen_3dp"
                android:text="Verified Purchase"
                android:textColor="@color/ratings_text_color"
                android:textSize="@dimen/txt_12sp" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/review_images_recyclerview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dimen_5dp">

        </android.support.v7.widget.RecyclerView>

        <com.android.sea.corenative.customviews.TextViewOpenSansRegular
            android:id="@+id/review_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dimen_5dp"
            android:text="description"
            android:textColor="@color/ratings_text_color"
            android:textSize="@dimen/txt_14sp" />
        <!--android:lineSpacingMultiplier="1.3"-->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginTop="@dimen/dimen_10dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/dimen_10dp"
                android:text="@string/review_helpful" />

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <android.support.v7.widget.AppCompatRadioButton
                    android:id="@+id/yes_radio_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/dimen_10dp"
                    android:buttonTint="@color/app_specific_text_color"
                    android:text="@string/yes" />

                <android.support.v7.widget.AppCompatRadioButton
                    android:id="@+id/no_radio_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="@dimen/dimen_10dp"
                    android:buttonTint="@color/app_specific_text_color"
                    android:text="@string/no" />

            </RadioGroup>


        </LinearLayout>

        <TextView
            android:id="@+id/rr_report_issue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/dimen_10dp"
            android:text="@string/report_issue"
            android:textSize="@dimen/txt_12sp"
            android:textColor="@color/star_color"/>


    </LinearLayout>

</LinearLayout>

ReviewCardViewHolder.java

    public class ReviewCardViewHolder extends RatingsReviewViewHolder {
    TextView reviewStar, reviewHelpfulCount, reviewHeader, reviewAuthor;
    TextView reviewTimeStamp, reviewDesc;
    LinearLayout verifiedPurchaseLayout;
    RecyclerView imgRecyclerView;
    final AppCompatRadioButton yesRadioBtn, noRadioBtn;
    TextView reportIssue;

    public ReviewCardViewHolder(View itemView) {
        super(itemView);
        reviewStar = (TextView) itemView.findViewById(R.id.rating_star);
        reviewHelpfulCount = (TextView) itemView.findViewById(R.id.review_helpful_count);
        reviewHeader = (TextView) itemView.findViewById(R.id.review_header);
        reviewAuthor = (TextView) itemView.findViewById(R.id.review_author_name);
        reviewTimeStamp = (TextView) itemView.findViewById(R.id.review_time);
        reviewDesc = (TextView) itemView.findViewById(R.id.review_description);
        verifiedPurchaseLayout = (LinearLayout) itemView.findViewById(R.id.verified_purchase_layout);
        imgRecyclerView = (RecyclerView) itemView.findViewById(R.id.review_images_recyclerview);
        imgRecyclerView.setHasFixedSize(true);
        yesRadioBtn =  itemView.findViewById(R.id.yes_radio_btn);
        noRadioBtn = (AppCompatRadioButton) itemView.findViewById(R.id.no_radio_btn);
        reportIssue = itemView.findViewById(R.id.rr_report_issue);

        LinearLayoutManager horizontalLayoutManagaer = new LinearLayoutManager(mContext) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }

            @Override
            public void setOrientation(int orientation) {
                super.setOrientation(0);
            }
        };
        imgRecyclerView.setLayoutManager(horizontalLayoutManagaer);
    }

    public void bind(RatingsAndReviewItem item) {
        final Review data = (Review) item.getValue();
        AttributeRating attributeValue = data.getAttributeRating().get(0);
        reviewStar.setText(ratingsAndReviewPresenter.drawOverAllRatingStar(attributeValue.getAttributeValue()));
        ratingsAndReviewPresenter.setCustomTextVal(reviewHelpfulCount, mContext.getString(R.string.review_helpful_count), data.getHelpfulCount());
        reviewHeader.setText(data.getReviewSummary());

        Author author = data.getAuthor();
        reviewAuthor.setText("By " + author.getScreenName());
        String authCity = "", authState = "";
        if (author.getCity() != null && !author.getCity().isEmpty()) {
            authCity = author.getCity() + ", ";
        }
        if (author.getState() != null && !author.getState().isEmpty()) {
            authState = author.getState() + " | ";
        }
        String timeStamp = authCity + authState + data.getPublishedDate();
        reviewTimeStamp.setText(timeStamp);
        reviewDesc.setText(data.getReviewContent());

        if (author.getBuyer()) {
            verifiedPurchaseLayout.setVisibility(View.VISIBLE);
        } else {
            verifiedPurchaseLayout.setVisibility(View.GONE);
        }


        if (data.getIsReviewHelpful() != null && data.getIsReviewHelpful().equalsIgnoreCase("yes")) {
            yesRadioBtn.setChecked(true);
        }else{
            yesRadioBtn.setChecked(false);
        }

        if (data.getIsReviewHelpful() != null && data.getIsReviewHelpful().equalsIgnoreCase("no")) {
            noRadioBtn.setChecked(true);
        }else{
            noRadioBtn.setChecked(false);
        }

        yesRadioBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(((RadioButton)view).isChecked() ) {
                    data.setIsReviewHelpful("yes");
                }
                ratingClickInterface.setRatingHelpfulStatus(true,data);
            }
        });

        noRadioBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(((RadioButton)view).isChecked() ) {
                    data.setIsReviewHelpful("no");
                }
                ratingClickInterface.setRatingHelpfulStatus(false,data);
            }
        });


    }
}

【问题讨论】:

  • 您需要自己管理单选按钮的选择状态
  • 您必须在 RadioButton 更改后保留它的状态。这就是ListViewRecyclerView 的工作原理。
  • 一种简单的方法是使用SparseBooleanArray 来存储选定单选按钮的int 位置,其值为trueonBindViewHolder(),获取具有 true 值的位置并将其设置为单选按钮。

标签: android


【解决方案1】:

尝试删除 RadioGroup。 RadioGroup 使这两个单选按钮互斥。

【讨论】:

  • 删除单选组并手动取消选中其他单选按钮解决了该问题。谢谢@changdeo Jadhav
【解决方案2】:
  if (data.getIsReviewHelpful() != null && data.getIsReviewHelpful().equalsIgnoreCase("yes")) {
            yesRadioBtn.setChecked(true);
            noRadioBtn.setChecked(false);
        }else{
            yesRadioBtn.setChecked(false);
            noRadioBtn.setChecked(true);
        }

        if (data.getIsReviewHelpful() != null && data.getIsReviewHelpful().equalsIgnoreCase("no")) {
            noRadioBtn.setChecked(true);
            yesRadioBtn.setChecked(false);
        }else{
            noRadioBtn.setChecked(false);
            yesRadioBtn.setChecked(true);
        }

在您的bind 方法中添加上述代码

【讨论】:

  • 删除广播组并执行您提到的操作解决了问题。谢谢
【解决方案3】:

RecyclerView 视图在用户滚动时被回收,因此您需要创建一个 Model(Bean) 类并将该类传递给 arraylist。 该模型类将包含所有必需的值以及一个附加的布尔值(isChecked)。 如果您设置单选按钮,而不是每次将该值设置为 true,则需要检查 onBind 方法中的值....这是针对这种情况的最简单的解决方法。 此外,您可以使用 setRecyclable(false) 属性..

【讨论】:

  • 这一行 data.setIsReviewHelpful("yes");在上面的代码中描述了那个工作
猜你喜欢
  • 1970-01-01
  • 2016-05-15
  • 2014-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多