Items of RecyclerView are not showing correctly

Amir_P

In my onBindViewHolder of my RecyclerView.Adapter<SearchAdapter.ViewHolder> when user clicks on cardview a button becomes visible. But when I'm scrolling recyclerview some other items buttons are shown as visible too. Why is this happening?

this is my code:

@Override
public void onBindViewHolder(final ViewHolder viewHolder, final int position) {
    viewHolder.card.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (viewHolder.content_layout.getVisibility() == View.VISIBLE) {
                viewHolder.content_layout.setVisibility(View.GONE);
                viewHolder.address.setMaxLines(2);
                viewHolder.attribute.setMaxLines(2);
            } else {
                viewHolder.content_layout.setVisibility(View.VISIBLE);
                viewHolder.address.setMaxLines(8);
                viewHolder.attribute.setMaxLines(8);
            }
        }
    });
    ...
}
reVerse

Once you start scrolling down the list your views get recycled. This means a previously inflated ViewHolder (some that gets created in onCreateViewHolder) is reused.
So what you have to do is to remember the clicked positions (e.g. via a SparseBooleanArray) and check in onBindViewHolder whether the view should be visible (previously clicked) or not.

You can find a basic usage example of the SparseBooleanArray in this StackOverflow post

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Items of RecyclerView are not showing correctly

From Dev

Layerlist not showing items correctly Android

From Dev

RecyclerView not showing items inside view pager fragment

From Dev

RecyclerView not showing any items unless i press homebutton and reenter the activity

From Dev

Android recyclerview filter showing no items until searchview icon is clicked

From Dev

Is there a callback for when RecyclerView has finished showing its items after I've set it with an adapter?

From Java

RecyclerView not displaying ArrayList correctly

From Dev

How to correctly populate RecyclerView?

From Dev

jqWidget Menu Not Showing correctly

From Dev

Table borders not showing correctly

From Dev

Hover background not showing correctly?

From Dev

UIBarButtonItem not showing correctly

From Dev

Japanese Text not showing correctly

From Dev

XDSoft DateTimePicker not showing correctly

From Dev

Google maps not showing correctly

From Dev

DateTime format not showing correctly

From Dev

Menu not showing correctly in IE

From Dev

Excel not showing time correctly

From Dev

Presentation background not showing correctly

From Dev

Table borders not showing correctly

From Dev

Annotation in pydev not showing correctly

From Dev

Related Products not showing correctly

From Dev

Table view not showing correctly

From Dev

UIScrollView showing subview not correctly

From Dev

values not showing correctly in dropdown

From Dev

adding items to actionbar correctly

From Dev

list items not displaying correctly

From Dev

RecyclerView is showing nothing

From Dev

RecyclerView is not showing anything