这是我的适配器中的代码:
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中删除项目所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)