简体   繁体   English

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

[英]radio button state is not retaining in recycler view

The problem here is whenever i scroll up my recycler view and come back to the top the selected radio button disappears . 这里的问题是,每当我向上滚动回收器视图并回到顶部时,所选的单选按钮就会消失。 also when the recycler view card is reused the radio button appears selected in the new card. 同样,当回收者视图卡被重复使用时,新卡中的单选按钮也会显示为选中状态。

the below is my viewholder used in recyclerview and its xml 下面是我在recyclerview及其xml中使用的viewholder

radioButton.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 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);
            }
        });


    }
}

Try removing RadioGroup. 尝试删除RadioGroup。 RadioGroup makes those two radio button mutually exclusive. RadioGroup使这两个单选按钮互斥。

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

Add above code in your bind method bind方法中添加以上代码

RecyclerView views are recycled when user scrolls, so you need to create a Model(Bean) class and pass that class to arraylist. 当用户滚动时,RecyclerView视图将被回收,因此您需要创建一个Model(Bean)类并将该类传递给arraylist。 That model class will contain all the required values plus an additional boolean value(isChecked). 该模型类将包含所有必需的值以及一个附加的布尔值(isChecked)。 If you set the radio button, than set that value to true and everytime, you need to check the value in onBind method....This is the simplest workaround for the situation. 如果设置了单选按钮,则每次都要将该值设置为true,则需要在onBind方法中检查该值。...这是这种情况下最简单的解决方法。 Also, you can use setRecyclable(false) property.. 另外,您可以使用setRecyclable(false)属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM