[英]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.