Personally feel that the Recyclerview components are really worth learning and use to our project, the previous study of relevant content, today to add some related things.
1, the implementation of the data in the Recyclerview to add and delete operations, or the same directly on the code:
public class Mainactivity extends Actionbaractivity {
Private Recyclerview Mrecyclerview;
Private list<testdata> Mdatas;
Private Testadapter Madapter;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initdatas ();
Initviews ();
}
private void Initdatas () {
Mdatas = new arraylist<testdata> ();
TestData data = new TestData ();
for (int i = ' A '; i < ' z '; i++) {
Data.settitle ("Title" + (char) i);
Data.setcontent ("Content" + (char) i);
Mdatas.add (data);
}
}
private void Initviews () {
Mrecyclerview = (Recyclerview) Findviewbyid (R.ID.TEST_RCV);
Madapter = new Testadapter (this, mdatas);
Mrecyclerview.setadapter (Madapter);
Linearlayoutmanager ll = new Linearlayoutmanager (this, linearlayoutmanager.horizontal, false);
Mrecyclerview.setlayoutmanager (LL);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
int id = item.getitemid ();
Switch (ID) {
Case r.id.action_add://Add operation
if (madapter! = null) {
Madapter.add (1);//We add a 2nd position corresponding to the Position=1
}
Break
Case r.id.action_del://Delete operation
if (madapter! = null) {
Madapter.delete (1);//We delete the 2nd position, corresponding to the Position=1
}
Break
}
return super.onoptionsitemselected (item);
}
}
------------Adapter----------------------------------
public class Testadapter extends Adapter<testviewholder> {
Private Context Mcontext;
Private list<testdata> Mdatas;
Public Testadapter (Context mcontext, list<testdata> mdatas) {
This.mcontext = Mcontext;
This.mdatas = Mdatas;
}
/** Adding Data */
public void Add (int pos) {
TestData data = new TestData ();
Data.setcontent ("This is increased");
Data.settitle ("add");
Mdatas.add (POS, data);
Notifyiteminserted (POS);//must call notifyiteminserted instead of calling notifydatasetchanged ()
}
/** Delete Data */
public void Delete (int pos) {
Mdatas.remove (POS);
Notifyitemremoved (POS);//must call notifyitemremoved instead of calling notifydatasetchanged ()
}
@Override
Public Testviewholder Oncreateviewholder (viewgroup paramviewgroup, int paramint) {
View v = layoutinflater.from (mcontext). Inflate (R.layout.lv_item, NULL);
Testviewholder holder = new Testviewholder (v);
return holder;
}
@Override
public void Onbindviewholder (final testviewholder PARAMVH, final int paramint) {
ParamVH.titleTv.setText (Mdatas.get (paramint). GetTitle ());
ParamVH.contentTv.setText (Mdatas.get (paramint). GetContent ());
}
@Override
public int GetItemCount () {
return Mdatas.size ();
}
}
Class Testviewholder extends Viewholder {
ImageView Iconiv;
TextView Titletv;
TextView Contenttv;
Public Testviewholder (View holder) {
Super (Holder);
Iconiv = (ImageView) Holder.findviewbyid (R.ID.ITEM_IV);
Titletv = (TextView) Holder.findviewbyid (r.id.item_title);
Contenttv = (TextView) Holder.findviewbyid (r.id.item_content);
}
}
2. Add a separator line to the item in Recyclerview:
Implement Additemdecoration (Recyclerview.itemdecoration itemdecoration) method, we need to write a class to inherit Itemdecoration, now realize the separation function. Then you just need a code:
Mrecyclerview.additemdecoration (New Divideritemdecoration (this,divideritemdecoration.vertical_list));// Divideritemdecoration can be found on the Internet Oh, there are a lot of related animation implementation, you can refer to: Https://github.com/yqritc/RecyclerView-FlexibleDivider
3, some small summary:
A, when setting Linearlayoutmanager for Recyclerview,
Linearlayoutmanager ll = new Linearlayoutmanager (this, linearlayoutmanager.horizontal, false);//The third parameter, if written as true, The POS order of our item is opposite to that shown
Mrecyclerview.setlayoutmanager (LL);
B, we can also give Recyclerview a brilliant separation line, such as: Https://github.com/yqritc/RecyclerView-FlexibleDivider
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Learning Note: Recyclerview Component Usage Summary