gpt4 book ai didi

android - SparseBooleanArray 探针用于垂直 RecyclerView 内部的水平 Recyclerview

转载 作者:行者123 更新时间:2023-11-30 00:53:49 25 4
gpt4 key购买 nike

我在 verticalRecycleview 中有 horizo​​ntalRecycleView。并且我使用 SparseBooleanArray 使 horizo​​ntatRecycleView 可选择。因此,每个用户单击水平列表中的一个项目,我都会更改该位置的背景。

问题是为每个 View /行初始化的 SparseBooleanArray 我猜使用相同的物理地址。因为我选择的每个位置都反射(reflect)在所有水平列表上。

这是我的代码:

RecycleView-垂直:

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 3:
return new HorizontalListHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_adapter_recycleview_horizontal, parent, false));
}
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (type) {
int type = chatAdapterModels.get(position).getVIEWTYPE();

case HORIZONTAL_LIST:
HorizontalListHolder viewHolder = (HorizontalListHolder) holder;
viewHolder.chatHorizontalAdapter.updateData(chatAdapterModels.get(position).getChatHorizontalModels());
break;
}
}

public final class HorizontalListHolder extends RecyclerView.ViewHolder {
ChatHorizontalAdapter chatHorizontalAdapter = null;
private SparseBooleanArray sparseBooleanArray;

@BindView(R.id.recycle_view)
RecyclerView recyclerView;

public HorizontalListHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
sparseBooleanArray = new SparseBooleanArray();

chatHorizontalAdapter = new ChatHorizontalAdapter(context, sparseBooleanArray);
chatHorizontalAdapter.onItemClickListener(this);
recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
recyclerView.setAdapter(chatHorizontalAdapter);
}
}
}

Horizo​​ntal-RecycleView

public class ChatHorizontalAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private Context context;
private ChatHorizontalAdapter.ClickListner clickListner;
ArrayList<ChatHorizontalModel> horizontalModels;
private SparseBooleanArray sparseBooleanArray;

private int lastPosition;

public ChatHorizontalAdapter(Context activity, SparseBooleanArray sparseBooleanArray) {

context=activity;
this.sparseBooleanArray=sparseBooleanArray;
}

public void updateData(ArrayList<ChatHorizontalModel> horizontalModels,int viewType){
this.horizontalModels=horizontalModels;
notifyDataSetChanged();
// sparseBooleanArray.clear();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case 1: return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_recycle_adapter_view, parent, false));
// case 2: return new something
}
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
ViewHolder viewHolder = (ViewHolder) holder;
String strings=horizontalModels.get(position).getStr();
viewHolder.textView.setText(strings);
viewHolder.imageView.setImageResource(horizontalModels.get(position).getImages());
changeBackgroundColor(sparseBooleanArray.get(position),viewHolder.cardView,viewHolder.imageView,viewHolder.textView,horizontalModels.get(position).getImages());

}

@Override
public int getItemViewType(int position) {
return viewType;
}

@Override
public int getItemCount() {
return horizontalModels.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
@BindView(R.id.mainCardView)CardView cardView;
@BindView(R.id.image)ImageView imageView;
@BindView(R.id.textView)TextView textView;
public ViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
cardView.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if(clickListner!=null){
clickListner.onItemClick(getAdapterPosition());

handleSelection(getAdapterPosition());
notifyDataSetChanged();
}
}
}

public interface ClickListner {
void onItemClick(int position);
}

public void onItemClickListener(ChatHorizontalAdapter.ClickListner clickListner){
this.clickListner=clickListner;
}

public void handleSelection(int position){
sparseBooleanArray.clear();
for (int i = 0; i <=horizontalModels.size() ; i++) {
if (i==position) {
sparseBooleanArray.put(i, true);
} else {
sparseBooleanArray.put(i,false);
}
}
}

public void changeBackgroundColor(boolean set, View cardView, ImageView imageView, TextView textView, int images){
// change bck color of selected item
}
}

最佳答案

RecyclerView不会为列表中的每一行创建 View ,如果您有 100 行,它可能会创建大约 7 或 8 个“只是屏幕上可见的数字”,因此您将有大约 7 或 8 个 SparseBooleanArray并且它将在所有行中重用它们,因此如果一行中的更改使用相同的 View 引用,则会影响另一行。
你应该创建一个 ArrayList<SparseBooleanArray>与您在 VerticalAdapter 中的行数相匹配的数字并更新 SparseBooleanArray里面HorizontalAdapter什么时候onBindViewHolder()被称为

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
HorizontalListHolder viewHolder = (HorizontalListHolder) holder;
// Where mSparseList is ArrayList<SparseBooleanArray>
viewHolder.updateSparseArray(mSparseList.get(position));
}

并在水平适配器中添加方法 updateSparseArray()更新 SparseBooleanArray数据。

关于android - SparseBooleanArray 探针用于垂直 RecyclerView 内部的水平 Recyclerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40502762/

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