繁体   English   中英

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

[英]How To Highlight Selected Child Item In A Nested RecyclerView

我有一个子 recyclerView 嵌套在父 recyclerView 中,为我提供了不同章节中主题的概念,如下图所示。

我的挑战是在任何给定部分或章节(嵌套子 RecyclerView)中突出显示单个选定的子项(主题)。

从照片中可以看出,之前在任何其他部分(章节)中选择的项目(或主题)在选择另一个部分的主题时不会取消选择。 有人可以提示如何最好地突出嵌套 recyclerView 中的选定项目。

在此处输入图像描述

这是我的 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);
    }

}

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

}

在子回收器视图适配器上添加一个代码,该代码在特定项目上侦听 onClick 事件并查看其 position,最后在该项目中应用更改 position。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM