简体   繁体   English

如何在“回收者”视图中突出显示选定的项目

[英]How to Highlight the selected item in Recycler view

I am trying to select the value from Recyclerview highlight the selected item. 我正在尝试从Recyclerview中选择值,突出显示所选项目。 I want to change the bg color and text color and image. 我想更改背景色和文本颜色以及图像。 Now I am using the following code. 现在,我正在使用以下代码。

selector_row : 选择器行

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@android:color/darker_gray" android:state_pressed="false" android:state_selected="true" />
    <item android:drawable="@color/store_search_list_item_background"/>
</selector>

In recyclerview on onBindViewHolder : 在onBindViewHolder的recyclerview中

holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (selectedItems.get(position, false)) {
                    selectedItems.delete(position);
                    holder.itemView.setSelected(false);
                }
                else {
                    selectedItems.put(position, true);
                    holder.itemView.setSelected(true);
                }
            }
        });

Now I can highlight the selected row. 现在,我可以突出显示所选的行。 My problem is 1) When I select another row it's also getting highlighted. 我的问题是1)当我选择另一行时,它也被突出显示。 I want to highlight only one row. 我只想突出显示一行。 If user select another row previous row should not be highlighted. 如果用户选择另一行,则前一行不应突出显示。

2) How to change the text and image when highlight the row. 2)高亮显示行时如何更改文本和图像。

Please let me any idea to resolve this two problems. 请让我有任何想法来解决这两个问题。

You can save click position in a variable so whenever new click happens you simply replace your variable value with new clicked position and after that don't forget to use notifyItemChanged() ; 您可以将点击位置保存在变量中,因此,每当发生新点击时,您只需将变量值替换为新的点击位置,然后别忘了使用notifyItemChanged() ; Now its very simple in your bindview check for your selected position and change the background of item. 现在,在bindview中非常简单,检查您选择的位置并更改项目背景。

rough ex. 粗暴的

  onClick(){
    highlightedPosition = getLayoutPostion();
    notifyItemChanged();
    }

  onBindView(int position){
    if(highlightedPosition == position){
    higlightedBackground();
    }else{
    normalBackground();
    }

android:clickable="true" android:focusable="true" android:focusableInTouchMode="true" android:foreground="@drawable/xx_sel"
or 要么
android:foreground="@drawable/xx_bg_sel" I use it to let my album photo item pregress or fouce can has a mask on it. android:foreground="@drawable/xx_bg_sel"我用它来让我的相册照片项目前移或贴上一个面具。
May it will help you. 愿它能对您有帮助。

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

相关问题 如何突出显示所选的Recycler View项目? - how to highlight the selected Item of Recycler View? 如何在 RecyclerView 中选择和取消选择一个项目? 如何仅在回收站视图中突出显示所选项目? - how to select and de-select an item in RecyclerView? How to Highlight selected item only in recycler view? 如何通过位置android在回收器视图中突出显示项目 - How to highlight a item in recycler view by position android 如何设置“回收者视图”中所选项目的颜色? - how to set color the selected Item of Recycler View? 如何像Instagram一样在Drag / Touch上突出显示Recycler视图项? - How to Highlight the Recycler view Item on Drag/Touch like Instagram? 检查回收站视图中的项目是否已选中 - Check if the item in recycler view selected 回收商查看中心选定的项目 - recycler view center selected item 如何选择回收商视图中的项目并将项目位置获取到我的活动 - How a item in recycler view is selected and get the item position to my activity 选择时如何将项目设置为回收站视图的中心 - How to set item to center of Recycler view when selected 如何在Recycler视图中更改所选项目的文本颜色 - how to change the text color of selected item inside recycler view
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM