我有一个应用程序,其中包含一些数据的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;
进入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);
}
基本上,您只需保存所选的上一个视图。
编辑:我在RecyclerView
项目上使用了一个selector
作为背景
编辑:将此行添加到要在选择时更改的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>
请尝试这个
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);
}
公共类 SomeRecyclerAdapter extensions 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
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);
}
我使用数据类中的布尔值管理当前选择的项目isSelected
。
回收商查看物料模型
data class OptionModel(
var title: String? = "",
var isSelected: Boolean = false, //control single selected view
var logo: Int? = 0
)
回收器视图适配器> onBindViewHolder(..,..)
holder.itemView.setOnClickListener {
toggleSelection(position)
}
一般的想法是知道选择了哪个位置,并将其布尔值切换为真,将其他项目的布尔值切换为假。
函数切换选择
fun toggleSelection(position: Int) {
var currentSelectIndex = position
listData.forEachIndexed { index, paymentOption ->
paymentOption.isSelected = index == currentSelectIndex
}
notifyDataSetChanged()
}
可以在代码中进一步使用相同的值,并且还有助于通过notifyDataSetChanged()
来更新 UI