【发布时间】:2020-12-20 23:39:06
【问题描述】:
我有一个子 recyclerView 嵌套在父 recyclerView 中,为我提供了不同章节中主题的概念,如下图所示。
我的挑战是在任何给定的部分或章节(嵌套的子 RecyclerView)中突出显示单个选定的子项(主题)。
从照片中可以看出,之前在任何其他部分(章节)中选择的项目(或主题)在选择其他部分的主题时不会取消选择。 有人可以提示如何最好地突出嵌套 recyclerView 中的选定项目。
这里是 Child RecyclerView 的代码 sn-p。
@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 是父级(或主 RecyclerView)的 OnBindViewHolder
@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());
}
这里调用了setUpTopicGroupRec方法
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);
}
});
}
【问题讨论】:
-
使用材料 recyclerView 自己就可以了。
-
我希望这是@NathanGetachew 的一个选项,但是必须对项目进行分段,因此是嵌套回收器视图选项的原因。
标签: java android nestedrecyclerview