gpt4 book ai didi

java - RecyclerView多选速度慢

转载 作者:行者123 更新时间:2023-12-01 18:00:14 25 4
gpt4 key购买 nike

我已经实现了一个自定义的RecyclerView来执行此操作: enter image description here

我使用了这个自定义 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:clickable="true"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="50dp">

<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:id="@+id/color"
android:background="@drawable/appborder" />

<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/item"
android:textColor="@color/colorPrimary"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/color"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp" />

<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
app:srcCompat="@drawable/check"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:visibility="invisible"
android:layout_alignParentEnd="true"
android:layout_marginRight="7dp"
android:layout_marginEnd="7dp"
android:id="@+id/check" />

</RelativeLayout>

这是我的自定义适配器:

public class RecyclerColorsDialogAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Context context;
private int [] colors;
private boolean [] checked;
private String [] text;
private boolean isChecked;

public RecyclerColorsDialogAdapter(Context context, int[] colors, boolean[] checked, String[] text) {
this.context = context;
this.colors = colors;
this.checked = checked;
this.text = text;
}

public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_color_item, parent, false);
PaHolder mHolder = new PaHolder(view);
return mHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
String singleColor = text[position];
isChecked = checked[position];

final PaHolder mHolder = (PaHolder)holder;

mHolder.text.setText(singleColor);

setCorners(colors[position],mHolder.color);

if(isChecked)
mHolder.check.setVisibility(View.VISIBLE);
else
mHolder.check.setVisibility(View.INVISIBLE);

mHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isChecked) {
checked[position] = false;
isChecked = false;

} else {
checked[position] = true;
isChecked = true;
}

mHolder.check.setVisibility(checked[position] ? View.VISIBLE : View.INVISIBLE);
}
});
mHolder.text.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Avenir-Book.ttf"));
}

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

private class PaHolder extends RecyclerView.ViewHolder {
View view;
TextView text;
ImageView color, check;

public PaHolder(View itemView) {
super(itemView);
view = itemView;
text = (TextView)itemView.findViewById(R.id.item);
check = (ImageView)itemView.findViewById(R.id.check);
color = (ImageView)itemView.findViewById(R.id.color);
}
}

public void setCorners(int color,ImageView imageView) {
GradientDrawable gd = new GradientDrawable();

// Specify the shape of drawable
gd.setShape(GradientDrawable.RECTANGLE);

// Set the fill color of drawable
gd.setColor(context.getResources().getColor(color));// make the background transparent

// Create a 2 pixels width red colored border for drawable
gd.setStroke(2, context.getResources().getColor(R.color.corners)); // border width and color

// Make the border rounded
gd.setCornerRadius(15.0f); // border corner radius

// Finally, apply the GradientDrawable as TextView background
imageView.setBackground(gd);
}
}

问题是,当我单击某种颜色(例如“红色”)时,有时在第一次单击时它不会选择/取消选择该项目:在同一 View 中第二次单击时它会起作用。

我也尝试过设置 focusable = false 但问题是一样的。

有人可以对这个问题提出建议吗?

谢谢

最佳答案

好的,只需删除那里的 isChecked 变量即可。仅使用 checked[position] 来代替。

并在持有者的 onClickListener 中调用 notifyDataSetChanged,而不是隐藏和显示该项目。

if(checked[position])
mHolder.check.setVisibility(View.VISIBLE);
else
mHolder.check.setVisibility(View.INVISIBLE);

mHolder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (checked[position]) {
checked[position] = false;
} else {
checked[position] = true;
}

// Just add notify. Remove the show/hide code.
notifyDataSetChanged();
}
});

关于java - RecyclerView多选速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41552281/

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