android– 从RecyclerView中删除项目

android– 从RecyclerView中删除项目,第1张

概述我在RecyckerView中从列表中删除项目时遇到问题.我想要做以下功能.当我按下RecyckerView列表中的复选框时,我希望从列表中删除此项目.当我按下一些顶部/底部复选框时,基本上这是有效的,但当我按下其中一些时,我有其他复选框被标记为已检查,甚至没有触摸它们.我不知道这个问题来自哪

我在RecyckerVIEw中从列表中删除项目时遇到问题.我想要做以下功能.当我按下RecyckerVIEw列表中的复选框时,我希望从列表中删除此项目.当我按下一些顶部/底部复选框时,基本上这是有效的,但当我按下其中一些时,我有其他复选框被标记为已检查,甚至没有触摸它们.我不知道这个问题来自哪里.

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

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;}@OverrIDepublic 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;}@OverrIDepublic voID onBindVIEwHolder(ShopPingListVIEwHolder shopPingListVIEwHolder, int position) {    shopPingListVIEwHolder.bindShopPingList(mItems.get(position));}@OverrIDepublic 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;@OverrIDeprotected 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));}@OverrIDepublic 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;}@OverrIDepublic 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);}

解决方法:

我认为“自动”检查项目重用已删除项目的视图持有者(包括选中的复选框).当您bind VIEwHolder尝试(重新)将复选框’state设置为false时:

ShopPingListVIEwHolder {    public voID bindShopPingList(Item item){        mShopPingListItem.setText(item.getItemDescription());        mCheckBox.setChecked(false);// <- this    }}

onBindViewHolder是您设置项目视图以匹配项目数据的位置

总结

以上是内存溢出为你收集整理的android – 从RecyclerView中删除项目全部内容,希望文章能够帮你解决android – 从RecyclerView中删除项目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1119205.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存