gpt4 book ai didi

android - 从 RecyclerView 中删除项目

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:00 52 4
gpt4 key购买 nike

当我从 RecycerView 的列表中删除项目时遇到问题。我想执行以下功能。当我在我的 RecycerView 列表中按下一个复选框时,该项目将从列表中删除。基本上,当我按下一些顶部/底部的复选框时,这是有效的,但是当我按下中间的一些时,我有其他复选框被标记为已选中,甚至没有触及它们。我不知道这个问题来自哪里。

enter image description here

这是我在适配器中的代码:

public class ShoppingListAdapter extends RecyclerView.Adapter<ShoppingListAdapter.ShoppingListViewHolder> {
private ArrayList<Item> mItems;
private Context mContext;

public ShoppingListAdapter(Context context, ArrayList<Item> items) {
mItems = items;
mContext = context;
}

@Override
public ShoppingListViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View view = LayoutInflater.from(mContext).inflate(R.layout.shopping_list_item,viewGroup,false);
ShoppingListViewHolder viewHolder = new ShoppingListViewHolder(view);

return viewHolder;
}

@Override
public void onBindViewHolder(ShoppingListViewHolder shoppingListViewHolder, int position) {
shoppingListViewHolder.bindShoppingList(mItems.get(position));
}

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

public class ShoppingListViewHolder extends RecyclerView.ViewHolder implements CompoundButton.OnCheckedChangeListener{
public TextView mShoppingListItem;
public CheckBox mCheckBox;

public ShoppingListViewHolder(View itemView) {
super(itemView);
mShoppingListItem = (TextView) itemView.findViewById(R.id.shoppingListItem);
mCheckBox = (CheckBox) itemView.findViewById(R.id.shoppingListCheckBox);
mCheckBox.setOnCheckedChangeListener(this);
}

public void bindShoppingList(Item item){
mShoppingListItem.setText(item.getItemDescription());
}


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
removeAt(getAdapterPosition(),this);
}
}

public void removeAt(int position,ShoppingListViewHolder viewHolder) {
mItems.remove(position);
notifyItemRemoved(position);
}

这是我在 MainActivity 中的代码:

public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private RecyclerView mRecyclerView;
private ArrayList<Item> shoppingListItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mToolbar = (Toolbar)findViewById(R.id.tool_bar);
setSupportActionBar(mToolbar);
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerView);
shoppingListItems = new ArrayList<>();
shoppingListItems.add(new Item("Apples"));
shoppingListItems.add(new Item("Bred"));
shoppingListItems.add(new Item("Potatoes"));
shoppingListItems.add(new Item("Muffins"));
shoppingListItems.add(new Item("Crackers"));
shoppingListItems.add(new Item("Spaghetti"));
shoppingListItems.add(new Item("Plastic Bags"));
shoppingListItems.add(new Item("Deodorant"));
shoppingListItems.add(new Item("Razors"));
shoppingListItems.add(new Item("Shampoo"));
shoppingListItems.add(new Item("Tooth brushes"));
shoppingListItems.add(new Item("Butter"));
shoppingListItems.add(new Item("Bagels"));
shoppingListItems.add(new Item("Coconut water"));
shoppingListItems.add(new Item("Tomatoes"));

ShoppingListAdapter adapter = new ShoppingListAdapter(this,shoppingListItems);
mRecyclerView.addItemDecoration(new SimpleDividerItemDecoration(getApplicationContext()));
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

最佳答案

我认为“自动”选中的项目会重用已删除项目的 View 持有者(包括选中的复选框)。当你bind ViewHolder 尝试(重新)将复选框的状态设置为 false:

ShoppingListViewHolder {
public void bindShoppingList(Item item){
mShoppingListItem.setText(item.getItemDescription());
mCheckBox.setChecked(false);// <- this
}
}

onBindViewHolder是你设置项目的 View 以匹配项目的数据的地方

关于android - 从 RecyclerView 中删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30933607/

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