Android 5.0 Recyclerview Use

Source: Internet
Author: User

Recyclerview is a 5.0 start out of the new ListView, mainly to improve the performance and display mode is also diversified.

In the old version, we optimized the view by defining a holder, and now the Recyclerview encapsulates a holder

Supports horizontal scrolling and vertical scrolling of 2 lists

also supports grid grid layout and disorderly layout


ICON + TEXT

public class Item {public    int imgid;    Public String desc;    Public Item (String desc, int imgid) {        THIS.DESC = desc;        This.imgid = Imgid;    }}

Simple holder.

public class Myviewholder extends Recyclerview.viewholder {public    myviewholder (View itemview) {        super ( Itemview);}    }


public class Myrecycleradapter extends recyclerview.adapter<myviewholder> {private activity activity;    Private list<item> List;        Public Myrecycleradapter (Activity Act, list<item> List) {this.activity = act;    This.list = list; } @Override//Bind a UI as holder high performance public Myviewholder Oncreateviewholder (viewgroup parent, int viewtype) {V        Iew v = layoutinflater.from (activity). Inflate (R.layout.recy_item,null);        Myviewholder holder = new Myviewholder (v);    return holder; }//Set data @Override public void Onbindviewholder (myviewholder holder, int position) {Item item = List.get (        position);        TextView Text1 = (TextView) holder.itemView.findViewById (R.id.text);        Text1.settext (ITEM.DESC);        ImageView img = (ImageView) holder.itemView.findViewById (r.id.img);    Img.setimageresource (Item.imgid);    } @Override public int getitemcount () {return list.size (); }}

Recy_item.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "android:layout_height=" match_parent "    android:orientation=" Horizontal "    >    <imageview        android:layout_width=" 70DP "        android:layout_height=" 70DP "        Android:id = "@+id/img"        android:scaletype= "Fitcenter"        android:layout_marginleft= "2DP"        android:layout _gravity= "center_vertical"        />     <textview         android:id= "@+id/text"         android:layout_width= "Wrap_content"         android:layout_height= "wrap_content"         android:layout_marginleft= "3DP"         android: layout_margintop= "5DP"         android:textcolor= "#336699"         android:textsize= "18SP"         /></ Linearlayout>



public class Mainactivity extends Actionbaractivity {private list<item> itemList;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Initview ();  private void Initview () {String title = "To travel hopefully was a better thing than to arrive";        String title = "";        ItemList = new arraylist<item> (5);        Itemlist.add (New Item (Title,r.drawable.cat0));        Itemlist.add (New Item (Title,r.drawable.cat0));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT2));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT3));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT4));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT4));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT3));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT2));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT3));     Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT2));   Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT4));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT4));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT4));        Itemlist.add (New Item (Title,r.drawable.cat0));        Itemlist.add (New Item (TITLE,R.DRAWABLE.CAT3));    Recyclerview list = (Recyclerview) Findviewbyid (R.id.listview);        Linearlayoutmanager mg = new Linearlayoutmanager (this);       Horizontal or vertical placement, can not Horizontalscrollview//mg.setorientation (linearlayoutmanager.horizontal); Gridlayoutmanager mg = new Gridlayoutmanager (this,3);//grid placement//staggered placement, a bit win8 that lattice style, preferably using CardView as item, with border and rounded corners//S       Taggeredgridlayoutmanager mg = new Staggeredgridlayoutmanager (2,staggeredgridlayoutmanager.vertical);        List.setlayoutmanager (MG);        Myrecycleradapter adapter = new Myrecycleradapter (this, itemList);    List.setadapter (adapter); } }

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= ". Mainactivity ">    <android.support.v7.widget.recyclerview        android:id =" @+id/listview "        android: Layout_width= "Fill_parent"        android:layout_height = "Fill_parent"        /></relativelayout>

Several operating conditions:

The normal list View



Horizontal scrolling



Lattice conditions

Gridlayoutmanager mg = new Gridlayoutmanager (this,3); 3 columns



Mixed and staggered. Our images need dynamic size to see the effect. Comment out the text and show only the pictures.

Staggeredgridlayoutmanager mg = new Staggeredgridlayoutmanager (2,staggeredgridlayoutmanager.vertical);

    public void Onbindviewholder (myviewholder holder, int position) {        Item item = list.get (position);        TextView Text1 = (TextView) holder.itemView.findViewById (r.id.text);        Text1.settext (ITEM.DESC);        ImageView img = (ImageView) holder.itemView.findViewById (r.id.img);        int ww = + (int) (Position * New Random (). Nextint ());        if (ww >) {            ww = 200;//Each picture is too wide to look good, calculate an appropriate value        }        img.getlayoutparams (). width = ww;        Img.getlayoutparams (). Height = + (int) (Position * new random (). Nextint (50));//Random high-width        img.setimageresource ( Item.imgid);    }


This staggered content size is not fixed, so it is recommended to use CardView can have a border effect


This demo is simple and basic usage is these.


Android 5.0 Recyclerview Use

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.