Abhishek Dabholkar
Abhishek Dabholkar

Reputation: 61

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>

Upvotes: 1

Views: 2385

Answers (2)

Abhishek Dabholkar
Abhishek Dabholkar

Reputation: 61

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

Upvotes: 0

Viktor Apoyan
Viktor Apoyan

Reputation: 10755

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.

Upvotes: 0

Related Questions