I have a Child recyclerView nested in a parent recyclerView giving me the concept of topics in different Chapters as seen in the attached photo below.
MY CHALLENGE is highlighting a single selected child item (Topic) in any given section or Chapter(nested child RecyclerView).
As can be seen in the photo, items (or Topic) previously selected in any other section (chapter) does not de-select when a topic from another section is selected. Can someone please give a hint on HOW best to highlight a selected item in a nested recyclerView.
HERE is my code snippet for the Child RecyclerView.
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Topic topic = topicList.get(position);
holder.clearSelection();
if (currentItemPosition == position) {
holder.position.setTextColor(holder.itemView.getResources().getColor(R.color.red));
holder.title.setTextColor(holder.itemView.getResources().getColor(R.color.red));
holder.description.setTextColor(holder.itemView.getResources().getColor(R.color.blue));
} else {
holder.position.setTextColor(holder.itemView.getResources().getColor(R.color.black));
holder.title.setTextColor(holder.itemView.getResources().getColor(R.color.black));
holder.description.setTextColor(holder.itemView.getResources().getColor(R.color.black));
}
holder.position.setText(String.valueOf(topic.getPosition()));
holder.title.setText(topic.getTitle());
holder.description.setText(topic.getDescription());
holder.duration.setText(topic.getDuration());
if (downloadedTopics.contains(topic.getTitle())) {
holder.downloadIcon.setImageResource(R.drawable.downloaded_icon);
} else {
holder.downloadIcon.setImageResource(R.drawable.undownloaded_icon);
}
}
HERE is the OnBindViewHolder of the Parent (or Main RecyclerView)
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
ArrayList<View> topicViewList = new ArrayList<>();
RootTopic topicGroup = rootTopicsGroupList.get(position);
ArrayList<Topic>topicList = topicGroup.getTopicGroup();
String titleConstruct = "Chapter " + (position + 1) + "- " + topicGroup.getRootTopicName();
holder.SectionTitle.setText(titleConstruct);
setUpTopicGroupRec(topicList,downloadedTopicList,holder.groupedTopicsRV,holder.itemView.getContext());
}
Here Is the setUpTopicGroupRec method called
private void setUpTopicGroupRec(ArrayList<Topic> topicList, ArrayList<String>downloads, RecyclerView recyclerView, Context context, ArrayList<RootTopic> rootTopicsGroupList){
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,RecyclerView.VERTICAL,false);
topicAdapter = new TopicAdapter(topicList, downloads);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(topicAdapter);
topicAdapter.setOnItemClickListener(new TopicAdapter.OnItemClickListener() {
@Override
public void onTopicClick(int position, Topic topic) {
currentTopic = topic;
onTopicClickLD.postValue(currentTopic);
int currentTopicPos = topic.getPosition();
}
@Override
public void onDownloadIconClick(int position, Topic topic) {
currentTopic = topic;
onDownloadIconClickLD.postValue(currentTopic);
}
});
}
On child recycler view adapter add a code that listens the onClick event on the specific item and see its position and finally apply change in the item at that position.