Android Development Learning--recycleview Getting Started

Source: Internet
Author: User

This control is used to display a large number of datasets in a limited window, in fact, the control of such a function is not unfamiliar to us, for example: ListView, GridView

By setting the different layoutmanager,itemdecoration it provides, itemanimator achieves an astonishing effect.

    • The way you want to control its display, through the layout manager LayoutManager
    • You want to control the interval between item (can be drawn), please pass the itemdecoration
    • You want to control the animation of item additions and deletions, please pass Itemanimator
    • You want to control the click, long press events, please write yourself

Activity_main.xml

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns: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"    >    <Android.support.v7.widget.RecyclerViewAndroid:id= "@+id/id_recyclerview_horizontal"Android:layout_width= "Match_parent"Android:layout_height= "120DP"android:layout_centervertical= "true"Android:scrollbars= "None"         /></Relativelayout>

Item.xml

<?XML version= "1.0" encoding= "Utf-8"?><Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "120DP"Android:layout_height= "120DP">    <ImageViewAndroid:id= "@+id/iv"Android:layout_width= "80DP"Android:layout_height= "80DP"Android:layout_alignparenttop= "true"Android:layout_centerhorizontal= "true"Android:layout_margin= "5DP"Android:scaletype= "Centercrop"/>    <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_below= "@id/iv"Android:layout_centerhorizontal= "true"Android:layout_margintop= "5DP"Android:layout_marginbottom= "5DP"Android:text= "some info"android:textsize= "12DP"        /></Relativelayout>

Mainactivity.java

 Public classMainactivityextendsappcompatactivity {PrivateRecyclerview MRV; PrivateList<integer>Mdatas; PrivateGalleryadapter Madapter; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        InitData (); MRV=(Recyclerview) Findviewbyid (r.id.id_recyclerview_horizontal); Linearlayoutmanager Li=NewLinearlayoutmanager ( This);       Li.setorientation (linearlayoutmanager.horizontal); Mrv.setlayoutmanager (li);//Set Layout manager Madapter=NewGalleryadapter ( This, Mdatas);    Mrv.setadapter (Madapter);//set Adapter } Private voidInitData () {Mdatas=NewArraylist<integer>(Arrays.aslist (R.drawable.a, r.drawable.b, r.drawable.c, R.DRAWABLE.E, R.DRAWABLE.F, R.D    RAWABLE.G)); }}
galleryadapter. java
 Public classGalleryadapterextends recyclerview.adapter<galleryadapter.viewholder> {    PrivateLayoutinflater Minflter; PrivateList<integer>Mdatas;  PublicGalleryadapter (context context, list<integer>datas) {Minflter=Layoutinflater.from (context); Mdatas=datas; }    /*** The Viewholder we create must inherit the Recyclerview.viewholder, which must be passed into a view when constructed, this view is equivalent to our ListView g     Convertview in Etview * (that is, we need to inflate the item layout to be passed in). */     Public classViewholderextends recyclerview.viewholder{ PublicViewholder (View v) {Super(v);        } ImageView Miv;    TextView MTV; }    //Create Viewholer@Override PublicViewholder Oncreateviewholder (viewgroup parent,intViewType) {View View= Minflter.inflate (R.layout.item,parent,false); Viewholder Viewholder=Newviewholder (view); VIEWHOLDER.MIV=(ImageView) View.findviewbyid (R.ID.IV); returnViewholder; }    //set the value; Bind data to Viewholder@Override Public voidOnbindviewholder (Viewholder holder,intposition)    {Holder.miv.setImageResource (Mdatas.get (position)); }    //How many data are there?@Override Public intGetItemCount () {returnmdatas.size (); }}

Android Development Learning--recycleview Getting Started

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.