gpt4 book ai didi

java - 隐藏 View 时回收器 View 未取消选择

转载 作者:行者123 更新时间:2023-12-01 19:06:05 24 4
gpt4 key购买 nike

我有一个显示四个数字的回收器 View 。 1,2,3,4。当我选择这些数字中的任何一个时,一切看起来都很好。问题是当我水平滚动并且其他数字填充我的屏幕时:4,5,6,7。

状态1

enter image description here

状态2

在此示例中,在状态 1状态 2 之间来回滚动,当只应选择其中之一时,7 和 1 都会被选中

Question: How can I make it so that clicking one of the numbers deselects the number that isn't visible on screen?

适配器:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
private String[] days;
private String[] numbers;
SelectionTracker<Long> mSelectionTracker;
private static MyAdapter myAdapter;


public MyAdapter(String[] day, String[] number){
days = day;
numbers = number;
setHasStableIds(true);
}


//Set Keys
public void setSelectionTracker(SelectionTracker<Long> selectionTracker) {
mSelectionTracker = selectionTracker;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
String dayInit = days[position];
String numberInit = numbers[position];
Long id = getItemId(position);


boolean isSelected = false;
if(mSelectionTracker != null) {
if (mSelectionTracker.isSelected(id)) {
isSelected = true;
}
}


holder.bind(position, id, dayInit, numberInit, isSelected);
}

public static class MyViewHolder extends RecyclerView.ViewHolder{
public TextView dayView, numberView;
public View view;
ScheduleDetails scheduleDetails = new ScheduleDetails();

public MyViewHolder(View itemView){
super(itemView);
view = itemView;
dayView = itemView.findViewById(R.id.day);
numberView = itemView.findViewById(R.id.day_number);

}

void bind(int position, Long key, String dayInit, String numberInit, Boolean isSelected){
scheduleDetails.position = position;
scheduleDetails.identifier = key;

System.out.println("Check: " + position);

dayView.setText(dayInit);
numberView.setText(numberInit);
view.setActivated(isSelected);
}


public ItemDetailsLookup.ItemDetails<Long> getItemDetails(@NonNull MotionEvent motionEvent){
return scheduleDetails;
}
}

static class ScheduleDetails extends ItemDetailsLookup.ItemDetails<Long>{
int position;
Long identifier;

@Override
public int getPosition() {
return position;
}

@Nullable
@Override
public Long getSelectionKey() { return identifier; }

@Override
public boolean inSelectionHotspot(@NonNull MotionEvent e){
return true;
}
}

// Create new views (invoked by the layout manager)
@Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_layout, parent, false);

MyViewHolder vh = new MyViewHolder(view);
return vh;
}


@Override
public int getItemCount() {
return days.length;
}

@Override
public long getItemId(int position) {
return position;
}

}

最佳答案

您可以覆盖 onViewRecycled方法在您的 RecyclerView.Adapter 类中并禁用那里的 View 。以下是基于您提供的代码的示例:

@Override
public void onViewRecycled(@NonNull MyViewHolder holder) {
//disable activated view when it got recycle
holder.view.setActivated(false)
super.onViewRecycled(holder);
}

现在,每次回收 View 时,您都会取消选择它,并且当再次绑定(bind)此 View 时(在 MyViewHolder 类的 bind 方法中),您将选择只有正确的观点。

关于java - 隐藏 View 时回收器 View 未取消选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59553123/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com