我的应用包含recyclerView
一些数据。场景就是这样的;当我单击其中一个项目的图像更改以及当我单击另一个项目时我想要的内容时,单击项目图像将更改,之前单击的项目图像将变为正常。我已经实现了代码但我的代码问题是,当我点击其他项目时,之前点击的项目图像保持不变。
代码: -
public void toggleSelection(int pos) {
currentSelectedIndex = pos;
if (selectedItems.get(pos, false)) {
selectedItems.delete(pos);
animationItemsIndex.delete(pos);
} else {
selectedItems.put(pos, true);
animationItemsIndex.put(pos, true);
animationItemsIndex.delete(pos);
}
notifyItemChanged(pos);
}
private SparseBooleanArray selectedItems;
// array used to perform multiple animation at once
private SparseBooleanArray animationItemsIndex;
private boolean reverseAllAnimations = false;
答案 0 :(得分:0)
进入YourViewHolderClass
@Override
public void onClick(View view) {
imageView.setSelected(true);
if (previousSelection != null && imageView != previousSelection)
previousSelection.setSelected(false);
previousSelection = imageView;
if (mClickListener != null)
mClickListener.onItemClick(view, getAdapterPosition());
}
此外,您必须在适配器
中定义一个接口 // allows clicks events to be caught
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface ItemClickListener {
void onItemClick(View view, int position);
}
基本上您只需保存之前选择的视图。
编辑:我在selector
的项目上使用RecyclerView
作为背景
编辑:将此行添加到您希望在选择时更改的recyclerview item
视图
android:background="@drawable/ic_material_type_selector"
<强> ic_material_type_selector.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:drawable="<!-- drawable / color for selected state -->"/>
<item android:state_selected="false"
android:drawable="<!-- drawable / color for unselected state -->"/>
</selector>
答案 1 :(得分:0)
请试试这个
currentSelectedIndex = -1;
public void toggleSelection(int pos) {
if(currentSelectedIndex!=-1) {
//Previous Item is unselected
}
currentSelectedIndex = pos;
if (selectedItems.get(pos, false)) {
// Current item is selected
} else {
// Current item is unselected
}
notifyItemChanged(pos);
}
答案 2 :(得分:0)
公共类SomeRecyclerAdapter扩展了Adapter {
private int mLastClickedPosition = Integer.MIN_VALUE;
...
@Override
public void onBindViewHolder(SomeViewHolder holder, int position) {
final boolean activatedState = mLastClickedPosition == position;
holder.itemView.setActivated(activatedState);// this is only example, you can use "activatedState" in different ways
}
public void onItemClick(int position) {
final int lastClickedPosition = mLastClickedPosition;
mLastClickedPosition = position;
if (lastClickedPosition >= 0) {
notifyItemChanged(lastClickedPosition);
}
if (mLastClickedPosition >= 0) {
notifyItemChanged(mLastClickedPosition);
}
}
}
从这里开始:https://gist.github.com/anonymous/4ef280c79c7c3f4aae1406c818149f8c
答案 3 :(得分:0)
final int currentClickedPosition = -1;
under button click.....under onBindViewHolder method....
final int lastClickedPosition = currentClickedPosition ;
currentClickedPosition = position;
//do your stuff here............
if(lastClickedPosition != currentClickedPosition ){
notifyItemChanged(lastClickedPosition);
}
答案 4 :(得分:0)
我使用数据类中的 isSelected
布尔值管理当前选择的项目。
RecyclerView 项目模型
data class OptionModel(
var title: String? = "",
var isSelected: Boolean = false, //control single selected view
var logo: Int? = 0
)
RecyclerViewAdapter > onBindViewHolder(..,..)
holder.itemView.setOnClickListener {
toggleSelection(position)
}
一般的想法是知道选择了哪个位置并将其布尔值切换为真,其他项目的布尔值切换为假。
功能切换选择
fun toggleSelection(position: Int) {
var currentSelectIndex = position
listData.forEachIndexed { index, paymentOption ->
paymentOption.isSelected = index == currentSelectIndex
}
notifyDataSetChanged()
}
可以在代码中进一步使用相同的值,还可以通过 notifyDataSetChanged()