Analysis of the use method of Android Recyclerview _android

Source: Internet
Author: User

1. Introduction

Recyclerview is a new view group with the goal of providing similar rendering for any adapter based view. It is used as a successor to the ListView and GridView controls and is supported in the latest SUPPORT-V7 version. The Recyclerview architecture provides a plug-and-pull experience, highly decoupled, unusually flexible, and itemanimator achieve stunning results by setting the different layoutmanager,itemdecoration it offers.

In short, ListView and the GridView can do the Recyclerview can do, and can achieve the waterfall flow effect

2. Use

Configuring Recyclerview libraries in Build.gradle
Compile ' com.android.support:recyclerview-v7:23.3.0 '

//Set Recyclerview Adapter adapter = new Myrecyclerviewadapter (Recyclerviewactivity.this,
Datas);
 
Recyclerview.setadapter (adapter); LayoutManager Recyclerview.setlayoutmanager (New Linearlayoutmanager (Recyclerviewactivity.this,
Linearlayoutmanager.vertical, false));
 
Recyclerview.scrolltoposition (Datas.size ()-1); Add Recyclerview line Recyclerview.additemdecoration (new Dividerlistitemdecoration (Recyclerviewactivity.this,
 
Dividerlistitemdecoration.vertical_list));

Sets the animation recyclerview.setitemanimator (New Defaultitemanimator ()); 
public class Myadapter extends Recyclerview.adapter<myadapter.myviewholder> {private final context;
 
 Private final arraylist<string> datas;
 Public Myadapter (Context context,arraylist<string> datas) {this.context = context;
 This.datas = datas; /** * is equivalent to creating holder layout of GetView in ListView adapters * * @param parent * @param viewtype * @return * * @Override public Myvi Ewholder Oncreateviewholder (viewgroup parent, int viewtype) {View view = View.inflate (context, R.layout.item_hello, null
 );
 Return to New Myviewholder (view); @Override public void Onbindviewholder (myviewholder holder, int position) {Holder.tv_text.setText Datas.get (positi
 ON));
 
 Holder.iv_icon.setBackgroundResource (R.mipmap.ic_launcher);
 @Override public int GetItemCount () {return datas.size ();
 Class Myviewholder extends Recyclerview.viewholder {private TextView tv_text;
 
 Private ImageView Iv_icon;
  Public Myviewholder (View Itemview) {super (Itemview); Tv_teXT = (TextView) Itemview.findviewbyid (R.id.tv_text);
 
 Iv_icon = (ImageView) Itemview.findviewbyid (R.id.iv_icon);
 }
 }
}

3. Set listview&gridview& Waterfall Flow type effect

Sets the list type effect
Recyclerview.setlayoutmanager new Linearlayoutmanager (Recyclerviewactivity.this, Linearlayoutmanager.vertical,false));
 
Set the grid type effect
recyclerview.setlayoutmanager new Gridlayoutmanager (Recyclerviewactivity.this, 2, Gridlayoutmanager.vertical, False));
Recyclerview.scrolltoposition ();
 
Sets the waterfall flow type effect
Recyclerview.setlayoutmanager (3, New Staggeredgridlayoutmanager staggeredgridlayoutmanager.vertical));

4. Split Line

Reference URL: http://blog.csdn.net/lmj623565791/article/details/45059587

Set split lines-Split lines need customization & can also customize the style of the split line
//No default split line
recyclerview.additemdecoration (new Dividerlistitemdecoration (this, dividerlistitemdecoration.vertical_list));
public class Dividerlistitemdecoration extends Recyclerview.itemdecoration {private static final int[] Attrs = new int[ ]{Android.

 R.attr.listdivider};

 public static final int horizontal_list = linearlayoutmanager.horizontal;

 public static final int vertical_list = linearlayoutmanager.vertical;

 Private drawable Mdivider;

 private int morientation; Public Dividerlistitemdecoration (context context, int orientation) {Final TypedArray a = Context.obtainstyledattributes
 (Attrs);
 Mdivider = a.getdrawable (0);
 A.recycle ();
 SetOrientation (orientation); public void setorientation (int orientation) {If orientation!= horizontal_list && Orientation!= vertical_l
 IST) {throw new IllegalArgumentException ("Invalid orientation");
 } morientation = orientation;

 @Override public void OnDraw (Canvas C, Recyclerview Parent) {//LOG.E ("Recyclerview-itemdecoration", "OnDraw ()");
 if (morientation = = vertical_list) {drawvertical (c, parent); else {DrawhorIzontal (c, parent);
 } public void Drawvertical (Canvas C, Recyclerview parent) {Final int left = Parent.getpaddingleft ();

 Final int right = Parent.getwidth ()-parent.getpaddingright ();
 Final int childcount = Parent.getchildcount ();
  for (int i = 0; i < ChildCount. i++) {final View child = Parent.getchildat (i);
  Android.support.v7.widget.RecyclerView v = new Android.support.v7.widget.RecyclerView (Parent.getcontext ());
  Final Recyclerview.layoutparams params = (recyclerview.layoutparams) child getlayoutparams ();
  Final int top = Child.getbottom () + Params.bottommargin;
  final int bottom = top + mdivider.getintrinsicheight ();
  Mdivider.setbounds (left, top, right, bottom);
 Mdivider.draw (c);
 } public void Drawhorizontal (Canvas C, Recyclerview parent) {Final int top = Parent.getpaddingtop ();

 final int bottom = Parent.getheight ()-Parent.getpaddingbottom ();
 Final int childcount = Parent.getchildcount (); for (int i = 0; i < ChildCount i++) {finAl View child = Parent.getchildat (i);
  Final Recyclerview.layoutparams params = (recyclerview.layoutparams) child getlayoutparams ();
  Final int left = Child.getright () + Params.rightmargin;
  Final int right = left + mdivider.getintrinsicheight ();
  Mdivider.setbounds (left, top, right, bottom);
 Mdivider.draw (c); @Override public void Getitemoffsets (Rect outrect, int itemposition, Recyclerview parent) {if (morientation = V
 Ertical_list) {outrect.set (0, 0, 0, mdivider.getintrinsicheight ());
 else {outrect.set (0, 0, mdivider.getintrinsicwidth (), 0);

 }}} dividerlistitemdecoration

To set the split line style:

①application settings

<!--Base Application theme. -->
<style name= "Apptheme" parent= "Theme.AppCompat.Light.DarkActionBar" >
 <!--Customize your Theme here. -->
 <item name= "colorprimary" > @color/colorprimary</item> <item name=
 "Colorprimarydark" > @color/colorprimarydark</item>
 <item name= "coloraccent" > @color/coloraccent</item>
 <item name= "Android:listdivider" > @drawable/divider_bg</item>
</style>

② in the activity

<activity android:name= ". Recyclerview. Recyclerviewactivity "
 android:theme=" @style/listdividertheme "/> <style name=" Listdividertheme "


Parent= "Theme.AppCompat.Light.DarkActionBar" >
 <!--Customize your Theme here--> <item
 " Colorprimary "> @color/colorprimary</item>
 <item name=" Colorprimarydark "> @color colorprimarydark</item>
 <item name= "coloraccent" > @color/coloraccent</item>
 <item Name= "Android:listdivider" > @drawable/divider_bg</item>
</style>

5. Custom Set Item Click event

Recyclerview default is no click event, you need to customize the Click event
Using the Knowledge point: Interface, Getlayoutposition ()

public class Myadapter extends recyclerview.adapter<myadapter.myviewholder>{the private final context;
 
 Private final arraylist<string> datas;
 Set click on an item to monitor public interface onitemclicklistener{void Onitemclick (View view,int position,string content);
 Private Onitemclicklistener Onitemclicklistener; public void Setonitemclicklistener (Onitemclicklistener onitemclicklistener) {This.onitemclicklistener =
 Onitemclicklistener;
 //Set click picture Public interface onimageviewclicklistener{void Onimageviewclick (View view,int position);
 Private Onimageviewclicklistener Onimageviewclicklistener;
 public void Setonimageviewclicklistener (Onimageviewclicklistener onimageviewclicklistener) {
 This.onimageviewclicklistener = Onimageviewclicklistener; ... class Myviewholder extends Recyclerview.viewholder {private TextView tv_te..). Classes----------------
 xt
 
 Private ImageView Iv_icon; Public Myviewholder (View Itemview) {super (Itemview);
  Tv_text = (TextView) Itemview.findviewbyid (R.id.tv_text);
 
  Iv_icon = (ImageView) Itemview.findviewbyid (R.id.iv_icon); Set Click event Itemview.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {if onit
   Emclicklistener!= null) {Onitemclicklistener.onitemclick (V,getlayoutposition (), Datas.get (GetLayoutPosition ()));
 
  }
  }
  }); Sets the Listener Iv_icon.setonclicklistener (new View.onclicklistener () {@Override public void OnClick (View v) {if onimage
   Viewclicklistener!= null) {Onimageviewclicklistener.onimageviewclick (v,getlayoutposition ());
 }
  }
  });
 }
 }
}

Use a custom Click event in an activity

Set Click event Myadapter.setonitemclicklistener on item
(new Myadapter.onitemclicklistener () {
 @Override
 public void Onitemclick (view view, int position, String content) {
 toast.maketext (recyclerviewactivity.this, " content== "+content+",--position== "+position, Toast.length_short). Show ();
 
Set click on a picture click event
myadapter.setonimageviewclicklistener (new Myadapter.onimageviewclicklistener () {
 @ Override public
 void Onimageviewclick (view view, int position) {
 Toast.maketext (recyclerviewactivity.this, "position==" +position+ ", view==" +view.tostring (), Toast.length_short). Show ();

6. Delete and add data

1_ Add and remove two new methods in the adapter

public class Myadapter extends Recyclerview.adapter<myadapter.myviewholder> {........... ...
 
 ... Public
 
 void AddData (int position,string content) {
 datas.add (position,content);
 notifyiteminserted (position);
 }
 
 public void Removedata (int position) {
 datas.remove (position);
 notifyitemremoved (position);
 }

In 2_activity

Btn_add.setonclicklistener (New View.onclicklistener () {
 @Override public
 void OnClick (View v) {
 Myadapter.adddata (0, "Content netdata");
 Navigate to position No. 0
 recyclerview.scrolltoposition (0);
 }
});
 
Btn_remove.setonclicklistener (New View.onclicklistener () {
 @Override public
 void OnClick (View v) {
 Myadapter.removedata (0);
 }
});

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.