如何在嵌套的recyclerview中突出显示选定的子项

e5nqia27  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(396)

我有一个孩子recyclerview嵌套在一个家长recyclerview给我的概念,在不同的章节,如所附的照片下面看到的主题。
我的挑战是在任何给定的节或章中突出显示单个选定的子项(主题)(嵌套的child recyclerview)。
从照片中可以看出,当选择了另一节(章节)中的主题时,之前在任何其他节(章节)中选择的项目(或主题)不会取消选择。有人能给一个提示如何最好地突出显示一个嵌套的回收视图中选定的项目。

下面是我的子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);
    }

}

这是父级(或主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());

}

下面是名为

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);

        }
    });

}
h79rfbju

h79rfbju1#

在“子回收器”视图适配器上,添加一个代码,用于侦听特定项上的onclick事件并查看其位置,最后在该位置对该项应用更改。

相关问题