Getting Started with Android Recyclerview

Source: Internet
Author: User

First, to achieve the simplest list of displays,

In this presentation, Recyclerview's role is limited to recycling and locating the textview on the screen, and when the user slides the screen, the previous view is recycled and the next page view is displayed, which is recycled.

Use of Recyclerview is inseparable from the use of 2 classes: Viewholder and Adapter,viewholder are used to hold the view view for display, and the data is set by adapter to the view to be displayed. Let Recyclerview set the adapter to achieve the display effect.

Use Recyclerview to support libraries, introduce v7 packages to eclipse, add references in Androidstudio, click File→project Structure .... The menu item switches to the Project structure window, selects the app module on the left, and then clicks the Dependencies Options page. Click the + button to pop up the dependent Libraries Add window. Locate and select the RECYCLERVIEW-V7 Support Library and click the OK button to complete the dependent library additions.

Define Recyclerview (Activity_main.xml) in the layout file:

<android.support.v7.widget.recyclerview
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/my_recycle_view"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >

</android.support.v7.widget.RecyclerView>

Code writing: (Mainactivity.java)

public class Mainactivity extends Appcompatactivity {
Private Recyclerview My_recycler_view;
Private Myadapter Mmyadapter;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
My_recycler_view = (Recyclerview) Findviewbyid (R.id.my_recycle_view);
Recyclerview when the view is created, it is forwarded to the LayoutManager object
My_recycler_view.setlayoutmanager (New Linearlayoutmanager (Getbasecontext ()));
Mmyadapter = new Myadapter (Getlistres ());//Create adapter Object
My_recycler_view.setadapter (mmyadapter);//Set Adapter
}

Public list<string> Getlistres () {
list<string> list = new arraylist<> ();
for (int i = 0; i <; i++) {
List.add ("Hello_world--->" + i);
}
return list;
}

Creating a Viewholder class for displaying views
Private class Myholder extends Recyclerview.viewholder {

Public TextView Tv_title;

Public Myholder (View Itemview) {
Super (Itemview);
Tv_title = (TextView) Itemview;
}
}

Create Adapter
Private class Myadapter extends Recyclerview.adapter<myholder> {

Private list<string> mlist;

Public Myadapter (list<string> List) {
Mlist = list;//Get Data resource
}

@Override
Public Myholder Oncreateviewholder (viewgroup parent, int viewtype) {
View view = Layoutinflater.from (Getbasecontext ()). Inflate (Android. R.layout
. Simple_list_item_1,
Parent, false);
return new Myholder (view);
}

@Override
public void Onbindviewholder (myholder holder, int position) {
Holder.tv_title.setText (Mlist.get (position) + "");//set content to the view in holder
}

@Override
public int GetItemCount () {
return mlist.size ();//Returns the length of the resource data
}
}
}



The above code creates the Oncreateviewholder of the 2 classes Myholder and myadapter,myadapter that inherit Recycler.viewholder and recycleradapter<vh>, respectively. Set the view, pass it to Myholder, get the control object in the Myholder constructor, and set the value for the Myholder view in the Onbindviewholder () method. Finally, let Recyclerview set the adapter.

Now further increase the complexity of the layout, creating a new layout file (list_item_layout)
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >

<imageview
Android:id= "@+id/iv_icon"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>

<textview
Android:id= "@+id/tv_title"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_torightof= "@id/iv_icon"/>

<textview
Android:id= "@+id/tv_content"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_below= "@id/tv_title"
android:layout_torightof= "@id/iv_icon"/>
</RelativeLayout>


Add code to get the data:
Public list<string> getlistcontent () {
list<string> list = new arraylist<> ();
for (int i = 0; i <; i++) {
List.add ("Android--->" + i);
}
return list;
}

Modify the contents of a Myadapter
Private class Myadapter extends Recyclerview.adapter<myholder> {

Private list<string> mlisttitle, mlistcontent;

Public Myadapter (list<string> listtitle, list<string> listcontent) {
Mlisttitle = listtitle;//Get Data resource
Mlistcontent = listcontent;
}

@Override
Public Myholder Oncreateviewholder (viewgroup parent, int viewtype) {
View view = Layoutinflater.from (Getbasecontext ()). Inflate (R.layout.list_item_layout,
Parent, false);
return new Myholder (view);
}

@Override
public void Onbindviewholder (myholder holder, int position) {
Holder.tv_title.setText (Mlisttitle.get (position) + "");//set content to the view in holder
Holder.tv_content.setText (Mlistcontent.get (position) + "");
Holder.iv_icon.setImageResource (R.mipmap.ic_launcher);
}

@Override
public int GetItemCount () {
return mlisttitle.size ();//Returns the length of the resource data
}
}


To modify the contents of the Myholder:
Public TextView Tv_title;
Public TextView tv_content;
Public ImageView Iv_icon;

Public Myholder (View Itemview) {
Super (Itemview);
Tv_title = (TextView) Itemview.findviewbyid (r.id.tv_title);
Tv_content = (TextView) Itemview.findviewbyid (r.id.tv_content);
Iv_icon = (ImageView) Itemview.findviewbyid (R.id.iv_icon);
}
Then change the Create adapter object code to
Mmyadapter = new Myadapter (Getlistres (), getlistcontent ());//Create adapter Object

The result is a simple, custom layout display:

Getting Started with Android Recyclerview

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.