单选按钮状态未保留在回收器视图中

时间:2018-01-08 11:30:14

标签: android

这里的问题是每当我向上滚动我的回收器视图并返回到顶部时,所选单选按钮消失。再次使用回收站视图卡时,新卡中会出现单选按钮。

以下是我在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);
            }
        });


    }
}

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

  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方法

中添加以上代码

答案 2 :(得分:0)

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