nested recyclerview inside adapter - android

I am developing an android app where I have used nested RecyclerView but I am not achieving what I want.I want to achieve on this list it should show Title and below multiple images like json below how can I achieve that I am using two viewholder and it should scrool as well.
json structure
below Adapter class
public class UranAdapter extends RecyclerView.Adapter {
public List<Exhibit> exhibitList;
public Context context;
public UranAdapter(List<Exhibit> uranList, Context context) {
this.exhibitList = uranList;
this.context = context;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView;
switch (viewType) {
case Exhibit.TEXT_TYPE:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exhibit_list, parent, false);
return new ViewHolder(itemView);
case Exhibit.IMAGE_TYPE:
itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.exhibit_list2, parent, false);
return new ImageViewHolder(itemView);
}
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
}
#Override
public int getItemViewType(int position) {
return exhibitList.get(position).type;
}
public int getItemCount() {
return exhibitList.size();
}
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
Exhibit exhibit = exhibitList.get(position);
switch (exhibit.type) {
case Exhibit.TEXT_TYPE:
((ViewHolder) holder).exhibition_textView.setText(exhibit.getTitle());
break;
case Exhibit.IMAGE_TYPE:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
((ViewHolder) holder).exhibition_imageView.setImageResource(exhibit.image);
}
break;
}
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView exhibition_imageView;
TextView exhibition_textView;
public ViewHolder(View view) {
super(view);
exhibition_textView = (TextView) view.findViewById(R.id.exhibition_textview);
}
}
public static class ImageViewHolder extends RecyclerView.ViewHolder {
public ImageView exhibition_imageView;
TextView exhibition_textView;
public ImageViewHolder(View view) {
super(view);
exhibition_imageView = (ImageView) view.findViewById(R.id.exhibition_imageview);
}
}
}
below MainActivity
public class MainActivity extends AppCompatActivity {
public List<Exhibit> exhibitList = new ArrayList<>();
Context context;
RecyclerView recyclerView;
public UranAdapter uranAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ApiInterface apiInterface = ApiClient.getApiService();
Call<ExhibitsLoader> call = apiInterface.getExhibitList();
call.enqueue(new Callback<ExhibitsLoader>() {
#Override
public void onResponse(Call<ExhibitsLoader> call, Response<ExhibitsLoader> response) {
exhibitList = response.body().getExhibitList();
exhibitList.add(new Exhibit(Exhibit.IMAGE_TYPE));
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
uranAdapter = new UranAdapter(exhibitList, context); // changes
recyclerView.setAdapter(uranAdapter);
}
#Override
public void onFailure(Call<ExhibitsLoader> call, Throwable t) {
}
});
}
}
below image what I want

I think what you need here is flatMap the embedded image list into one-level list and you're going correctly to have two ViewHolder of different types, one for title, and one for image. Don't understand why your ViewHolders are static.

You forget to call notifyDatasetChanged() after setting the adapter.
uranAdapter = new UranAdapter(exhibitList, context); // changes
recyclerView.setAdapter(uranAdapter);
uranAdapter.notifyDatasetChanged();

Use Expandable CardView for this ..
Like : https://www.youtube.com/watch?v=z9qScBaKfnM&t=727s

Why don't you use two RecyclerViews, one inside the other?
One RecyclerView will have the title and a children RecyclerView. The second RecyclerView(the children RecyclerView) will have just the images.
Check this post: Recyclerview inside Recyclerview , get click position of child row inside parent Adapter

Related

NestedRecyclerView Problem - Last item of the second RecyclerView is duplicated into the first one after the keyboard appears

I'm desperate.
I have a nested recycler view. Each outer element has an array of inner elements. A different adapter has been created for the inner elements. I am creating an array of external elements of class "KairosWithEvents", each of which contains internal elements of class "Event". Everything is displayed well. When elements are added, everything is also updated. For testing, I created two objects of the "KairosWithEvent" class. In the first object I have placed two objects of the "Event" class, and in the second - three objects. But when I want to change the EditText value, the keyboard appears. And the last element of the second object appears in the first object. How can I fix it? Objects are not moved or duplicated, but showed incorrectly.
This is what a nestled recycler view looks like initially.
And this is what a nested recycler view looks like after the keyboard appears. The "Эвент5" element is duplicated to the first element for some reason.
Here's my code: Outer Adapter:
public class EventAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static List<Item> items;
private static RecyclerViewClickInterface listener;
private RecyclerView.RecycledViewPool sharedPool = new RecyclerView.RecycledViewPool();
private Context context;
public EventAdapter() {
items = new ArrayList<>();
this.context = context;
}
public EventAdapter(List<Item> newItems){
items = newItems;
}
class KairosViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private TextView title;
private CheckBox iv;
private RecyclerView rv;
public KairosViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv);
iv = itemView.findViewById(R.id.iv);
rv = itemView.findViewById(R.id.rvSteaks);
itemView.setBackgroundColor(Color.parseColor("#91b3f2"));
itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
}
#RequiresApi(api = Build.VERSION_CODES.N)
void bind(KairosWithEvents kairosWithEvents){
title.setText(kairosWithEvents.kairos.kairosId + " = " + kairosWithEvents.kairos.title);
LinearLayoutManager layoutManager = new LinearLayoutManager(
rv.getContext(),
LinearLayoutManager.VERTICAL,
false);
List<Item> itemsEvents = new ArrayList<>();
kairosWithEvents.events.forEach(i -> itemsEvents.add(new Item(Constants.EVENT_KAIROS, i)));
SubAdapter childAdapter = new SubAdapter(itemsEvents);
rv.setLayoutManager(layoutManager);
rv.setAdapter(childAdapter);
rv.setRecycledViewPool(sharedPool);
}
#Override
public void onClick(View view) {
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION){
listener.onItemKairosWithEvents( ((KairosWithEvents) items.get(position).object) );
}
}
#Override
public boolean onLongClick(View view) {
return false;
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
switch (viewType){
case Constants.KAIROS:
return new KairosViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_ex, parent, false));
}
return null;
}
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case Constants.KAIROS:
KairosWithEvents kairos = ((KairosWithEvents) items.get(position).object);
((KairosViewHolder) holder).bind(kairos);
break;
}
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public int getItemViewType(int position) {
return items.get(position).type;
}
}
Inner Adapter:
public class SubAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private static List<Item> items;
public SubAdapter(List<Item> newItems) {
items = newItems;
}
static class EventViewHolder extends RecyclerView.ViewHolder {
private TextView title;
public EventViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv);
itemView.setBackgroundColor(Color.parseColor("#ffc8a8"));
}
void bind(Event event){
title.setText(event.eventId + " = " + event.title);
}
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
switch (viewType) {
case Constants.EVENT_KAIROS:
return new EventViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item_sub, parent, false));
}
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case Constants.EVENT_KAIROS:
Event event = (Event) items.get(position).object;
((EventViewHolder) holder).bind(event);
break;
}
}
#Override
public int getItemCount() {
return items.size();
}
#Override
public int getItemViewType(int position) {
return items.get(position).type;
}
}
In SubAdapter, remove the static keyword from your items field:
private static List<Item> items;
Should be this instead:
private List<Item> items;
You should make the same change to your outer adapter, but since there's only one of them at a time it doesn't wind up causing issues.

Make ItemTouchHelper run OnMove only when I release my touch on a RecyclerView List

I have a basic RecyclerView list and added a ItemTouchHelper in order to drag and drop list items.
The problem I have with it is that it runs onMove on every single list item it passes and it's lagging the app real hard.
Is there a way to implement it so that onMove only runs when I release my hold on a RecyclerView list item?
Thanks.
MainActivity.java
public class pillar_sort extends AppCompatActivity {
private MyRecyclerViewAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initialize layout
super.onCreate(savedInstanceState);
setContentView(R.layout.pillar_sort);
final SharedPreferences sharedPref = // Get SharedPreferences
final ArrayList<String> modules = // Import a list
final ArrayList<Boolean> visibility = // Import a list
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new MyRecyclerViewAdapter(this, modules, visibility, cardcolor, txtcolor);
recyclerView.setAdapter(adapter);
VerticalSpaceItemDecoration decor = new VerticalSpaceItemDecoration();
recyclerView.addItemDecoration(decor);
ItemTouchHelper helper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP|ItemTouchHelper.DOWN, 0) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder dragged, #NonNull RecyclerView.ViewHolder target) {
int position_dragged = dragged.getAdapterPosition();
int position_target = target.getAdapterPosition();
Collections.swap(modules,position_dragged,position_target);
adapter.notifyItemMoved(position_dragged,position_target);
adapter.notifyDataSetChanged();
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
}
});
helper.attachToRecyclerView(recyclerView);
}
MyRecyclerViewAdapter.java
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> {
// Variables go here
MyRecyclerViewAdapter( /* a lot of data to pass onto the adapter */ ) {
// get data from MainActivity and put on variables
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.recyclerview_row, parent, false);
CardView card = view.findViewById(R.id.card);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position){
// Put data to TextViews and set card color
}
#Override
public int getItemCount() {
return modules.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView myTextView;
ImageButton image_visibility;
ViewHolder(View itemView) {
super(itemView);
myTextView = itemView.findViewById(R.id.rowText);
image_visibility = itemView.findViewById(R.id.imageButton);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
}
}

Expand/collapse a nested RecyclerView

I have a RecyclerView adapter with multiple ViewHolders. Each ViewHolder has a header TextView and a nested RecyclerView which was working fine. But I wanted to implement an expand/collapse function so that nested RecyclerView is hidden until the header is clicked. I used this method RecyclerView expand/collapse items. It works but when I click the header to expand the nested recylerview, the recyclerview doesn't populate any data. To be clear, it retrieves data but it's not visible. Any ideas why this might be?
This is my onBindViewMethod:
public class EligibilityAdapter extends RecyclerView.Adapter<EligibilityAdapter.ViewHolder> {
private Context mContext;
private List<EligibilityDetails> mEligsList;
private List<Items> mItemslist;
private LayoutInflater inflater;
private int mExpandedPosition = -1;
public EligibilityAdapter(Context context, List<EligibilityDetails> eligsList) {
mContext = context;
mEligsList = eligsList;
inflater = LayoutInflater.from(context);
}
#Override
public int getItemViewType(int position) {
switch (position) {
case 0:
return R.layout.rv_eligs_item_domestic;
case 1:
return R.layout.rv_eligs_item_overseas;
default:
return R.layout.rv_eligs_item_military;
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
inflater = LayoutInflater.from(viewGroup.getContext());
View view = inflater.inflate(i, viewGroup, false);
ViewHolder holder = null;
switch (i) {
case R.layout.rv_eligs_item_domestic:
holder = new DomesticViewHolder(view);
break;
case R.layout.rv_eligs_item_overseas:
holder = new OverseasViewHolder(view);
break;
case R.layout.rv_eligs_item_military:
holder = new MilitaryViewHolder(view);
break;
}
return holder;
}
#Override
public int getItemCount() {
return mEligsList.size();
}
public abstract class ViewHolder extends RecyclerView.ViewHolder {
RecyclerView itemsRv;
TextView mHeader;
ItemsAdapter adapter;
public ViewHolder(View itemView) {
super(itemView);
mHeader = (TextView) itemView.findViewById(R.id.header_tv);
itemsRv = itemView.findViewById(R.id.recyclerViewItems);
}
public void setData(List<Items> list) {
adapter.updateList(list);
}
abstract void bind(EligibilityDetails item);
}
public class DomesticViewHolder extends ViewHolder {
TextView mHeader;
RecyclerView itemsRv;
ItemsAdapter adapter;
public DomesticViewHolder(View itemView) {
super(itemView);
mHeader = (TextView) itemView.findViewById(R.id.header_tv);
itemsRv = itemView.findViewById(R.id.recyclerViewItems);
}
public void setData(List<Items> list) {
adapter.updateList(list);
}
#Override
void bind(EligibilityDetails eligibilityDetails) {
mHeader.setText(eligibilityDetails.getRequirementHeader());
mItemslist = eligibilityDetails.getItemsList();
ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
itemsRv.setHasFixedSize(true);
itemsRv.setLayoutManager(new CustomLinearLayoutManager(mContext));
itemsRv.setAdapter(itemsAdapter);
itemsRv.setNestedScrollingEnabled(false);
}
}
public class OverseasViewHolder extends ViewHolder {
TextView mHeader;
RecyclerView itemsRv;
ItemsAdapter adapter;
public OverseasViewHolder(View itemView) {
super(itemView);
mHeader = (TextView) itemView.findViewById(R.id.header_tv);
itemsRv = itemView.findViewById(R.id.recyclerViewItems);
}
public void setData(List<Items> list) {
adapter.updateList(list);
}
#Override
void bind(EligibilityDetails eligibilityDetails) {
mHeader.setText(eligibilityDetails.getRequirementHeader());
mItemslist = eligibilityDetails.getItemsList();
ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
itemsRv.setHasFixedSize(true);
itemsRv.setLayoutManager(new CustomLinearLayoutManager(mContext));
itemsRv.setAdapter(itemsAdapter);
itemsRv.setNestedScrollingEnabled(false);
}
}
public class MilitaryViewHolder extends ViewHolder {
TextView mHeader;
RecyclerView itemsRv;
ItemsAdapter adapter;
public MilitaryViewHolder(View itemView) {
super(itemView);
mHeader = (TextView) itemView.findViewById(R.id.header_tv);
itemsRv = itemView.findViewById(R.id.recyclerViewItems);
}
public void setData(List<Items> list) {
adapter.updateList(list);
}
#Override
void bind(EligibilityDetails eligibilityDetails) {
mHeader.setText(eligibilityDetails.getRequirementHeader());
mItemslist = eligibilityDetails.getItemsList();
final ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
itemsRv.setHasFixedSize(true);
itemsRv.setLayoutManager(new CustomLinearLayoutManager(mContext));
itemsRv.setAdapter(itemsAdapter);
itemsRv.setNestedScrollingEnabled(false);
}
}
#Override
public void onBindViewHolder(#NonNull final EligibilityAdapter.ViewHolder viewHolder, int i) {
viewHolder.bind(mEligsList.get(i));
final boolean isExpanded = i == mExpandedPosition;
viewHolder.itemsRv.setVisibility(isExpanded?View.VISIBLE:View.GONE);
viewHolder.itemView.setActivated(isExpanded);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mExpandedPosition = isExpanded ? -1:viewHolder.getAdapterPosition();
ItemsAdapter itemsAdapter = new ItemsAdapter(mContext, mItemslist);
viewHolder.itemsRv.setAdapter(itemsAdapter);
//TransitionManager.beginDelayedTransition(recyclerView);
notifyDataSetChanged();
}
});
}
where itemsRv is the nested RecyclerView.
I've tried moving this logic to the individual viewholders and moving the recyclerview logic here like setting the adapter inside of the onClick method. Each time it comes up blank.
Thanks in advance.
Instead of having a new sub Recyclerview for every header you can create a multi view-type adapter that will have a view-type for your header and a view-type for your child-item.
And instead of using a just the header data-item for your list of data, use a generic type that will allow casting each data-type to its own view-type.
To do that we need to create an empty interface that all of our data-types will implement that way they are all generic.
public interface GenericDataType {}
so then your data-type will look like this
class HeaderItem implements GenericDataType {
//All of your pojo data
List<ChildrenItem> childrens; //ChildrenItem will also implement the GenericDataType that way both of the items are acceptable
}
So ones we did that we can replace the current items from
private List<EligibilityDetails> mEligsList;
private List<Items> mItemslist;
To
private List<GenericDataType> adapterItems;
Now we need to make sure that whenever we have a certain ViewHolder the item of that position will be of the right type. In order to do that we need to change our getItemViewType
#Override
public int getItemViewType(int position) {
GenericItem currentItem = adapterItems.get(position);
if (currentItem instanceOf HeaderItem) { //We have to use if else becasue java's switch does not supprt checking instancesOf
return R.layout.your_header_item_layout_id;
} else if(currentItem instanceOf ChildrenItem) {
return R.layout.your_children_item_layout_id;
} else if(repeat for any other types you have) {
return R.layout.your_other_item_layout_id;
} else {
throw new Throwable("Unsupported type"); //This should never happen but we add it to make the compiler compile
}
}
Now we can finally change our onBind method to support both types
#Override
public void onBindViewHolder(#NonNull final EligibilityAdapter.ViewHolder viewHolder, int i) {
if (viewHolder instanceOf HeaderViewHolder) {
HeaderViewHolder holder = (HeaderViewHolder) viewHolder;
headerItem = (HeaderItem) items.get(i);
//Do rest of binding here
holder.viewToAddMoreItems.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!headerItem.isEpanded()) { //Add to your header type a Boolean to check if it is expanded or not
adapterItems.addAll(headerItem.getChildrens()); //They are the same generic type but the getItemViewType will handle it for us
notifyItemRangeInserted(); //Provied item start index and size of the list
} else {
adapterItems.removeAll(headerItem.getChildrens());
notifyItemRangeRemoved(); I don't remember what it require but you'll figure that out
}
headerItem.setExpanded(!headerItem.isExpanded()); //Flipping the value
}
});
} else if (viewHolder instanceOf ChildrenViewHolder) {
ChildrenViewHolder holder = (ChildrenViewHolder) viewHolder;
childrenItem = (ChildrenItem) items.get(i);
//Do your binding here
} else if(repeate for other view-types) {}
}

LinearLayoutManager - repetition in a circle

Is there any easy way to make LinearLayoutManager to be as a circle (repetition items in a circle)?
I investigated it and found only way to make own custom LayoutManager, so it would be cool to find a way with less code
First thing to do is change your adapter's getItemCount() method to return Integer.MAX_VALUE. This is not actually infinite, but it's over two billion so I doubt anyone's going to scroll that far.
Next, whenever you retrieve an item from your data source, don't just use position. Instead, use position % dataSource.size()... this will repeat your items as the user scrolls.
Here's an example:
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recycler = (RecyclerView) findViewById(R.id.recycler);
recycler.setAdapter(new MyAdapter());
}
private static class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private final List<String> items;
private MyAdapter() {
items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View itemView = inflater.inflate(R.layout.itemview, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
String item = items.get(position % items.size());
((TextView) holder.itemView).setText(item);
}
#Override
public int getItemCount() {
return Integer.MAX_VALUE;
}
}
private static class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
}
}
}

Selected image in recyclerview is get unselected upon scroll

In my recyclerview has one imageview and textview. I'm changing image of ImageView onn onClickListener. Now the problem is if i click on image of position 3 and scroll down... image of position 8 is also changed and again if i scroll up...image of position 2 is changed.
public class PortraitListviewAdapter extends RecyclerView.Adapter<PortraitListviewAdapter.ViewHolder> {
Context context;
static List<PortraitParentListAdapterBean> list;
static List<String> selectedPosition ;
public PortraitListviewAdapter(Context context, List<PortraitParentListAdapterBean> list) {
this.context = context;
this.list = list;
selectedPosition = new ArrayList<>();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.listview_parent_portrait, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
PortraitParentListAdapterBean portBean = list.get(position);
Log.i("pos",position+"");
holder.parentHeading.setText(portBean.getHeading());
if (selectedPosition.contains(list.get(position).getHeading())){
holder.parentImage.setImageResource(R.drawable.sad);
}
}
#Override
public int getItemCount() {
return list.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
protected TextView parentHeading;
protected ImageView parentImage;
public ViewHolder(View itemView) {
super(itemView);
parentHeading = (TextView)itemView.findViewById(R.id.parent_heading);
parentImage = (ImageView)itemView.findViewById(R.id.imageView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
selectedPosition.add(list.get(getAdapterPosition()).getHeading());
parentImage.setImageResource(R.drawable.sad);
}
});
}
}
}
the above code is my implementation for recyclerview adapter. please help to understand the concept.
SOLUTION
change
if (selectedPosition.contains(list.get(position).getHeading())){
holder.parentImage.setImageResource(R.drawable.sad);
}
with
if (selectedPosition.contains(list.get(position).getHeading())){
holder.parentImage.setImageResource(R.drawable.sad);
} else {
holder.parentImage.setImageResource(R.drawable.your_default_drawable);
}
EXPLANATION
When you scroll your RecyclerView the system doesn't recreate always your ViewHolder but reuse one you previously scrolled that is no longer visible, so you need to reset your standard values in order to avoid showing wrong values.

Categories

Resources