
我已经实现了带有收件箱样式滑动视图的RecyclerVIEw.刷卡时,我使用以下方法删除了列表项:
public voID removeItem(int position) { countrIEs.remove(position); notifyItemRemoved(position);}类似地,当按下FAB时,我使用该方法添加数据,
public voID addItem(String country) { countrIEs.add(country); notifyItemInserted(countrIEs.size());}但是,当我通过滑动删除数据项时,它将从ArrayList和RecyclerVIEw列表中删除,但是当我通过FAB添加数据时,删除的数据仍会显示在列表中.我检查了ArrayList数据集.这是预期的.
在上面的屏幕截图中,您可以看到String Test是新添加的数据.我已经删除了最后两行的数据.随机显示.
我的适配器和活动的完整代码.
public class DataAdapter extends RecyclerVIEw.Adapter<DataAdapter.VIEwHolder> { private ArrayList<String> countrIEs; private TextVIEw tv_country; public DataAdapter(ArrayList<String> countrIEs) { this.countrIEs = countrIEs; } @OverrIDe public DataAdapter.VIEwHolder onCreateVIEwHolder(VIEwGroup vIEwGroup, int i) { VIEw vIEw = LayoutInflater.from(vIEwGroup.getContext()).inflate(R.layout.row_layout, vIEwGroup, false); return new VIEwHolder(vIEw); } @OverrIDe public voID onBindVIEwHolder(DataAdapter.VIEwHolder vIEwHolder, int i) { tv_country.setText(countrIEs.get(i)); } @OverrIDe public int getItemCount() { return countrIEs.size(); } public voID addItem(String country) { countrIEs.add(country); notifyItemInserted(countrIEs.size()); } public voID removeItem(int position) { countrIEs.remove(position); notifyItemRemoved(position); } public class VIEwHolder extends RecyclerVIEw.VIEwHolder{ public VIEwHolder(VIEw vIEw) { super(vIEw); tv_country = (TextVIEw)vIEw.findVIEwByID(R.ID.tv_country); } }}MainActivity.java
public class MainActivity extends AppCompatActivity implements VIEw.OnClickListener{ private ArrayList<String> countrIEs = new ArrayList<>(); private DataAdapter adapter; private RecyclerVIEw recyclerVIEw; private RecyclerVIEw.LayoutManager layoutManager; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); initVIEws(); } private voID initVIEws(){ floatingActionbutton fab = (floatingActionbutton) findVIEwByID(R.ID.fab); fab.setonClickListener(this); recyclerVIEw = (RecyclerVIEw)findVIEwByID(R.ID.card_recycler_vIEw); recyclerVIEw.setHasFixedSize(true); layoutManager = new linearlayoutmanager(getApplicationContext()); recyclerVIEw.setLayoutManager(layoutManager); adapter = new DataAdapter(countrIEs); recyclerVIEw.setAdapter(adapter); countrIEs.add("Australia"); countrIEs.add("India"); countrIEs.add("United States of America"); countrIEs.add("Germany"); countrIEs.add("Russia"); adapter.notifyDataSetChanged(); itemtouchhelper.SimpleCallback simpleItemtouchCallback = new itemtouchhelper.SimpleCallback(0, itemtouchhelper.left | itemtouchhelper.RIGHT) { @OverrIDe public boolean onMove(RecyclerVIEw recyclerVIEw, RecyclerVIEw.VIEwHolder vIEwHolder, RecyclerVIEw.VIEwHolder target) { return false; } @OverrIDe public voID onSwiped(RecyclerVIEw.VIEwHolder vIEwHolder, int direction) { int position = vIEwHolder.getAdapterposition(); if (direction == itemtouchhelper.left){ adapter.removeItem(position); } } @OverrIDe public voID onChildDraw(Canvas c, RecyclerVIEw recyclerVIEw, RecyclerVIEw.VIEwHolder vIEwHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { Paint p = new Paint(); Bitmap icon; if(actionState == itemtouchhelper.ACTION_STATE_SWIPE){ VIEw itemVIEw = vIEwHolder.itemVIEw; if(dX > 0){ p.setcolor(color.parsecolor("#388E3C")); c.drawRect((float) itemVIEw.getleft(), (float) itemVIEw.gettop(), dX,(float) itemVIEw.getBottom(), p); icon = BitmapFactory.decodeResource( getResources(), R.drawable.ic_edit_white); float height = (float) itemVIEw.getBottom() - (float) itemVIEw.gettop(); float wIDth = height / 3; RectF dest = new RectF((float) itemVIEw.getleft() + wIDth ,(float) itemVIEw.gettop() + wIDth,(float) itemVIEw.getleft()+ wIDth+wIDth,(float)itemVIEw.getBottom() - wIDth); c.drawBitmap(icon,null,dest,p); } else { p.setcolor(color.parsecolor("#D32F2F")); c.drawRect((float) itemVIEw.getRight() + dX, (float) itemVIEw.gettop(),(float) itemVIEw.getRight(), (float) itemVIEw.getBottom(), p); icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_delete_white); float height = (float) itemVIEw.getBottom() - (float) itemVIEw.gettop(); float wIDth = height / 3; RectF dest = new RectF((float) itemVIEw.getRight() - wIDth - wIDth ,(float) itemVIEw.gettop() + wIDth,(float) itemVIEw.getRight() - wIDth,(float)itemVIEw.getBottom() - wIDth); c.drawBitmap(icon,null,dest,p); } } super.onChildDraw(c, recyclerVIEw, vIEwHolder, dX, dY, actionState, isCurrentlyActive); } }; itemtouchhelper itemtouchhelper = new itemtouchhelper(simpleItemtouchCallback); itemtouchhelper.attachToRecyclerVIEw(recyclerVIEw); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()){ case R.ID.fab: adapter.addItem("Test"); Log.d("Raj",countrIEs.toString()); break; } }}我试过了什么
我试过像这样使用notifyItemRangeChanged():
public voID removeItem(int position) { countrIEs.remove(position); notifyItemRemoved(position); notifyItemRangeChanged(position, countrIEs.size());}解决方法:
我不能确定这会解决您的问题,但有一点不正确的是您如何绑定数据. tv_country不应该是DataAdapter的一部分;它应该是每个VIEwHolder的一部分.我们使用VIEwHolder模式的原因之一是保持对每行中视图的简单引用.
你的bind方法应该类似于:
public voID onBindVIEwHolder(DataAdapter.VIEwHolder vIEwHolder, int i) { vIEwHolder.tv_country.setText(countrIEs.get(i));}并确保将tv_country设置为内部VIEwHolder类的公共字段.
public class VIEwHolder extends RecyclerVIEw.VIEwHolder{ public TextVIEw tv_country; public VIEwHolder(VIEw vIEw) { super(vIEw); tv_country = (TextVIEw) vIEw.findVIEwByID(R.ID.tv_country); }} 总结 以上是内存溢出为你收集整理的java – RecyclerView显示List上删除的数据全部内容,希望文章能够帮你解决java – RecyclerView显示List上删除的数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)