gpt4 book ai didi

java - 选中复选框时如何将项目移动到最后一个位置? (回收 View ,ViewHolder)

转载 作者:行者123 更新时间:2023-11-30 05:10:35 25 4
gpt4 key购买 nike

enter image description here

我尝试制作一个包含复选框的 RecyclerView(如果已购买)。我想将项目移动到 CheckBox 选中更改的最后位置并将背景设置为绿色。我试图从 ArrayList 中删除项目,然后再次添加它,但它只适用于字符串名称,它不保持状态(复选框状态或颜色)。其他时候它抛出 Exception: Cannot call this method while RecyclerView is computing a layout or scrolling。有人可以告诉我我做错了什么吗?

对不起我的英语。

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
{
private ArrayList<String> mDataset;
private ArrayList<FinalListItem> finalListItems;
public MyAdapter(ArrayList<String> mDataset) {
this.mDataset = mDataset;
[enter image description here][1]finalListItems = new ArrayList<>();
for (String a : mDataset) {
finalListItems.add(new FinalListItem(a, false));
}
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.final_list_TextView1.setText(finalListItems.get(position).getName());
if(finalListItems.get(position).isChecked){
holder.linearLayout.setBackgroundColor(Color.rgb(0, 225, 0));
}
else{
holder.linearLayout.setBackgroundColor(Color.rgb(255, 255, 255));
}
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
FinalListItem finalItem = new FinalListItem(finalListItems.get(position).getName(),true);
if (isChecked) {
holder.linearLayout.setBackgroundColor(Color.rgb(0, 225, 0));
finalListItems.remove(finalListItems.get(position));
finalListItems.add(finalItem);
notifyDataSetChanged(); //weird behaviour of app
} else {
holder.linearLayout.setBackgroundColor(Color.rgb(255, 255, 255));
}

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

private class FinalListItem {
private String name;
private boolean isChecked;

public FinalListItem(String name, boolean isChecked) {
this.name = name;
this.isChecked = isChecked;
}

public boolean isChecked() {
return isChecked;
}

public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}

public String getName() {
return name;
}
}

public class MyViewHolder extends RecyclerView.ViewHolder {
final public TextView final_list_TextView1;
final public CheckBox checkBox;
final public LinearLayout linearLayout;

public MyViewHolder(View view) {
super(view);
final_list_TextView1 = (TextView) view.findViewById(R.id.final_list_TextView1);
checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
linearLayout = (LinearLayout) view.findViewById(R.id.linearLayout1);
}
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.finallist_list_layout, parent, false);
return new MyViewHolder(view);
}

最佳答案

关于您的代码,您应该更改的主要内容是更改数据,然后让适配器根据更改后的数据显示列表,这一点很重要。这意味着,你不应该改变,例如你自己的背景,但你应该改变 FinalListItem 的列表并通知适配器。

@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
holder.final_list_TextView1.setText(finalListItems.get(position).getName());

// remove the Listener before setting the checked state programmatically
// and set all the attributes (checked state and background color) here
holder.checkBox.setOnCheckedChangeListener(null);
if(finalListItems.get(position).isChecked){
holder.linearLayout.setBackgroundColor(Color.rgb(0, 225, 0));
holder.checkBox.setChecked(true);
}
else{
holder.linearLayout.setBackgroundColor(Color.rgb(255, 255, 255));
holder.checkBox.setChecked(false);
}
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Lint warning : don't use "final int position"
int currentPosition = holder.getAdapterPosition();
if (isChecked) {
FinalListItem finalItemBefore = finalListItems.get(currentPosition);
FinalListItem finalItemAfter = new FinalListItem(finalItemBefore.getName(),true);
finalListItems.remove(finalItemBefore);
finalListItems.add(finalItemAfter);
notifyDataSetChanged();
} else {
finalListItems.get(currentPosition).setChecked(false);
// no need to update the whole list since only one item has changed
notifyItemChanged(currentPosition);
}
}
});
}

另请注意,从 getItemViewType() 返回 position 并不是一个好主意。通过这样做,您告诉运行时每个 View 都是唯一的并且回收 View 是不可能的,这对 RecyclerView 的性能不利.

列表的实现方式,一种View 类型就足够了,因此您根本不需要重写该方法,因为这是默认实现:

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

关于java - 选中复选框时如何将项目移动到最后一个位置? (回收 View ,ViewHolder),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53821060/

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