Personally think that the Recyclerview component is really worth learning and used in our project to learn the relevant content in the front. Add some of the relevant stuff today.
1, the implementation of the data in the Recyclerview to join and delete operations. Or the same as the direct 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://JOIN operation
if (madapter! = null) {
Madapter.add (1);//We join in 2nd position, corresponding Position=1
}
Break
Case r.id.action_del://Delete operation
if (madapter! = null) {
Madapter.delete (1);//We delete the 2nd position. corresponding 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 added");
Data.settitle ("join");
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. To add a separator line to the item in Recyclerview:
Implement the 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 on the Internet to find Oh, there are a lot of related animation implementation, can participate: 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 assumption is written as true. The POS order of our item is opposite to that shown
Mrecyclerview.setlayoutmanager (LL);
B, we have the same ability to give Recyclerview a brilliant separation line, for example: Https://github.com/yqritc/RecyclerView-FlexibleDivider
Android Learning Note: Recyclerview Component Usage Summary