Android RecyclerView更新子项目notifyItemChanged
android recyclerview更新子项目notifyitemchanged
在过去android的listview中,如果listview中某一个或者一批项目发生变化,需要通过adapter的notifydatasetchanged更新数据到最新,但是从listview演进到现在的recyclerview,数据的更新不需要像listview那样notifydatasetchanged,而是要采用recyclerview的notifyitemchanged,传入一个pos位置,通知recyclerview该pos位置已经失效,需要重新绘制和ui更新。现在给出一个例子,上面一个button按钮,下面一个recyclerview:
每次点击button按钮,将随机更新某一个位置的时间到最新:
package zhangphil.demo; import android.app.activity; import android.graphics.color; import android.os.bundle; import android.support.v7.widget.linearlayoutmanager; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.linearlayout; import android.widget.textview; import android.widget.toast; import java.util.arraylist; import java.util.random; public class mainactivity extends activity { private arraylist data = new arraylist(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); for (int i = 0; i < 5; i++) { data.add(system.currenttimemillis()); } recyclerview mrecyclerview = findviewbyid(r.id.recycler_view); linearlayoutmanager mlayoutmanager = new linearlayoutmanager(this); mlayoutmanager.setorientation(linearlayout.vertical); mrecyclerview.setlayoutmanager(mlayoutmanager); final recyclerview.adapter madapter = new myadapter(); mrecyclerview.setadapter(madapter); final random random = new random(); findviewbyid(r.id.button).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int pos = random.nextint(data.size()); data.set(pos, system.currenttimemillis()); madapter.notifyitemchanged(pos); toast.maketext(getapplicationcontext(), "更新:" + pos + "," + data.get(pos), toast.length_long).show(); } }); } private class myadapter extends recyclerview.adapter { public myadapter() { super(); } @override public viewholder oncreateviewholder(viewgroup viewgroup, int i) { view view = layoutinflater.from(getapplicationcontext()).inflate(android.r.layout.simple_list_item_2, null); viewholder holder = new viewholder(view); return holder; } @override public void onbindviewholder(viewholder viewholder, int i) { viewholder.text1.settext(i + ""); viewholder.text2.settext(data.get(i) + ""); } @override public int getitemcount() { return data.size(); } public class viewholder extends recyclerview.viewholder { public textview text1; public textview text2; public viewholder(view itemview) { super(itemview); text1 = itemview.findviewbyid(android.r.id.text1); text1.settextcolor(color.red); text2 = itemview.findviewbyid(android.r.id.text2); text2.settextcolor(color.blue); } } } }
事实上recyclerview的notifyitemchanged的底层调用的是notifyitemrangechanged:
/** * notify any registered observers that the item at position has changed. * equivalent to calling notifyitemchanged(position, null);. * *
this is an item change event, not a structural change event. it indicates that any * reflection of the data at
position is out of date and should be updated. * the item at
position retains the same identity.
* * @param position position of the item that has changed * * @see #notifyitemrangechanged(int, int) */ public final void notifyitemchanged(int position) { mobservable.notifyitemrangechanged(position, 1); }
该方法使得recyclerview批量范围内(range)数据更新,notifyitemchanged巧妙的将第二个参数计数器设置为1得以实现。后续再介绍recyclerview的notifyitemrangechanged。
上一篇: 教师节的美文摘抄