【问题标题】:How To Highlight Selected Child Item In A Nested RecyclerView如何在嵌套的 RecyclerView 中突出显示选定的子项
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 我已经完成了@sulav,因为我正在使用嵌套的回收,先前选择的父recyclerview中的其他部分中的项目在选择另一部分中的项目时,不会被选中。
  • 你能添加你到目前为止尝试过的代码吗?
  • 我已经编辑了问题,按照建议添加了我尝试过的代码和照片
  • 您可以创建一个 livedata 并将其值更改为 1 或 0。如果选择了父项,则为 1;如果选择了子项,则为 0,并使用活页夹中的 livedata 自动切换。
  • 非常感谢,@Sulav,您提供的帮助不只是
猜你喜欢
  • 2021-08-22
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 2015-01-27
  • 1970-01-01
  • 2021-09-03
  • 2021-12-11
相关资源
最近更新 更多