简体   繁体   中英

Recyclerview items get reset on scrolling

So, here's my problem. My recyclerview items have a view at the bottom that I initially set to GONE. Now, when they are clicked I want to make them visible again. So in the onClick method I set the view to Visible. All's fine, but when I scroll down and scroll back up the view is hidden again. I guess it's got something to do with the ViewHolder patter. I want to keep the state as it is, ie, opened. How do I do it? Thanks.

View Holder:

public static class CustomCardViewHolder extends RecyclerView.ViewHolder {
    View mCard;
    View mFooter;
    ImageView mIcon;
    TextView mTitle;
    TextView mSummary;

    public CustomCardViewHolder(View view) {
        super(view);
        mCard = view.findViewById(R.id.container);
        mCard.setTag(this);
        mFooter = view.findViewById(R.id.footer); // view to be shown or hidden
        mIcon = (ImageView) view.findViewById(R.id.icon);
        mTitle = (TextView) view.findViewById(R.id.title);
        mSummary = (TextView) view.findViewById(R.id.summary);
    }

OnClick:

@Override
public void onClick(View view) {
    CustomCardViewHolder holder = (CustomCardViewHolder) view.getTag();
    if(holder.mFooter.getVisibility() == View.GONE) {
        expand(holder.mFooter); // this is just an animation and I'm setting the visibility to visible
        notifyItemChanged(holder.getPosition());
        notifyAll();
    } else {
        collapse(holder.mFooter); // similarly this too
        notifyItemChanged(holder.getPosition());
        notifyAll();
    }
}

Edit: Uploaded code. Also, I tried updating the boolean value of the Item in onClick and enforcing it onBindViewHolder. Problem is I have a sort of fake view(bumper) behind the toolbar. It gets invisible when I expand an item at the bottom of the recyclerview and scroll up again. It gradually starts appearing as I keep scrolling the recyclerview.

My activity xml:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/widget_bumper" />

    <include layout="@layout/widget_recyclerview"/>

    <include layout="@layout/widget_toolbar" />

</FrameLayout>

and my bumper:

<?xml version="1.0" encoding="utf-8"?>
<View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bumper"
    android:layout_width="match_parent"
    android:layout_height="@dimen/widget_bumper_height"
    android:background="?colorPrimary" >
</View>

Yes, you think right, you must keep some flags to determine which item in view is visible and which not. Depending on that you must set View.VISIBLE or View.GONE.

Just give a try and you will succeed. If not please share code I will say what to do.

Updated RecyclerView to the latest library. This fixed the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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