在RecyclerView中不在视图中时,突出显示/选定的项目的颜色会发生变化 [英] Hilighted/Selected items colour changes when not in view in RecyclerView
问题描述
我有一个带有大量物品清单的RecyclerView.此RecyclerView具有用于无限滚动的OnScrollListener.
I have a RecyclerView with a large list of items. This RecyclerView has OnScrollListener for endless scrolling.
- 选择一个项目后,我会用特定的颜色突出显示该项目,并且
- 未选择时,颜色变为正常/白色.
我面临的问题是,当我向上或向下滚动以选择更多项目时,在视图中选择了一些可见项目之后,已经选择的项目的颜色变为白色.
The issue that I am facing is after selecting a few visible items in my view when I scroll up or down to select a few more items the colour of already selected items changes to white.
我尝试在模型类中添加一个布尔变量(isSelected)并突出显示选中的项目,但仍然面临着与之前相同的问题.目前,recyclerView仅允许从视图中选择一项,经过研究后,我发现其中一些概念取自
I tried adding a boolean variable (isSelected) in the model class and highlight the selected item but still I am facing the same issue as earlier. Currently the recyclerView allows to select just one item from the view and after some research I figured some of the concepts were taken from this article to implement single item selection. I wonder how do I modify/tweak this code to be able to select multiple items.
我不会提供该代码,因为它非常庞大且是机密的,但是如果针对这种情况有任何通用的解决方案,那会是什么?
I am not presenting the code as it is quite huge and is confidential but if there is any general fix for this scenario then what would it be?
背景:该应用程序是一个聊天应用程序,我正在显示发送和接收的文本.用户应该能够选择一些特定的文本,并且应该能够与其他人共享.
Background: the application is a chat app and I am showing the sent and received texts. The user should be able to select a few specific texts and should be able to share it with someone else.
我将代码放入onBindViewHolder:
I am putting the code in my onBindViewHolder:
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final PostDataColumns mPostDataColumns = data.get(position);
holder.textCardView.setBackgroundColor(mPostDataColumns.isSelected() ? getResources().getColor(R.color.long_press):
getResources().getColor(android.R.color.white));
holder.textCardView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
mPostDataColumns.setSelected(!mPostDataColumns.isSelected());
if(mPostDataColumns.isSelected()) {
holder.textCardView.setBackgroundResource(R.color.long_press);
multipleSelectedPositions.add(holder.getLayoutPosition());
} else if(!mPostDataColumns.isSelected()) {
holder.textCardView.setBackgroundResource(android.R.color.white);
multipleSelectedPositions.remove(holder.getAdapterPosition());
}
//Adapter.this.onLongClick(holder, position);
return true;
}
});
holder.textCardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.textCardView.setBackgroundResource(android.R.color.white);
/* clearLongSelection(holder, position, alignParentRight,
data.get(position).getReceiverUserId().length() > 5); */
}
});
}
当需要选择单个项目时,使用了我在onCLick和onLongClick中注释过的代码.
The code which I have commented in onCLick and onLongClick were used when the requirement was to select a single item.
这些是在onClick和onLOngClick中调用的方法:
these are the methods which were called in onClick and onLOngClick:
public boolean clearLongSelection(ViewHolder holder, int position) {
if (selectedPosition >= 0) {
if (selectedPosition == position) {
holder.parentLayout.setBackgroundResource(android.R.color.transparent);
if (alignParentRight) {
holder.mediaCardView.setBackgroundResource(android.R.color.white);
holder.assessmentCardView.setBackgroundResource(android.R.color.white);
holder.surveyCardView.setBackgroundResource(android.R.color.white);
holder.documentCardView.setBackgroundResource(android.R.color.white);
holder.textCardView.setBackgroundResource(android.R.color.white);
} else {
holder.mediaCardView.setBackgroundResource(R.color.long_press);
holder.assessmentCardView.setBackgroundResource(R.color.long_press);
holder.surveyCardView.setBackgroundResource(R.color.long_press);
holder.documentCardView.setBackgroundResource(R.color.long_press);
holder.textCardView.setBackgroundResource(R.color.long_press);
}
selectedPosition = -1;
invalidateOptionsMenu();
getSupportActionBar().setTitle(intentData.getName());
}
return true;
}
return false;
}
public void onLongClick(ViewHolder holder, int position) {
if (selectedPosition < 0) {
holder.parentLayout.setBackgroundResource(R.color.long_press);
holder.mediaCardView.setBackgroundResource(R.color.long_press);
holder.assessmentCardView.setBackgroundResource(R.color.long_press);
holder.surveyCardView.setBackgroundResource(R.color.long_press);
holder.documentCardView.setBackgroundResource(R.color.long_press);
holder.textCardView.setBackgroundResource(R.color.long_press);
selectedPosition = position;
invalidateOptionsMenu();
getSupportActionBar().setTitle("1 Selected");
} else {
}
}
onClick和clearLongSelection中的变量selectedPosition在类中初始化为实例变量selectedPosition = -1.
The variable selectedPosition in onClick and clearLongSelection is initialised in the class as instance variable- selectedPosition = -1.
推荐答案
使用SparseBooleanArray跟踪回收器视图适配器中的选定项目
Use SparseBooleanArray to keep track of selected items in recycler view adapter
将SparseBooleanArray初始化为私有成员变量
Initialize the SparseBooleanArray as private memeber variable
private SparseBooleanArray mClickedItems=new SparseBooleanArray();
然后在单击任何项目时单击功能内,将单击的项目位置存储为true.
Then inside your click function while clicking any item,store the clicked item position as true.
mClickedItems.put(getAdapterPosition(),true);
notifyDataSetChanged();
然后在onBindViewHolder中检查位置是否已被选择
Then in the onBindViewHolder check if the position is already selected or not like this
if(mClickedItems.get(position)==true){
//Show selected color
}else {
//show unselected color
}
这篇关于在RecyclerView中不在视图中时,突出显示/选定的项目的颜色会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!