Android--The use of Recyclerview

Source: Internet
Author: User

Description

Recyclerview is a new component in the support-v7 package that you can use to replace the ListView and Cridview, From the name you can see that Recyclerview will automatically recycle item, it does not care about the item's style, the item's data.

So what do you do to prepare for using Recyclerview?

1, since Recyclerview is the component in the SUPPORT-V7 package, then we must download the SDK on the SUPPORT-V7 this package, we open the sdk\extras\android\support\v7 directory can be found to have a Recyclerview this folder,

Yes, that's the recyclerview we're going to use today.

2, we add in the Build.gradle file:

dependencies {    compile fileTree(dir‘libs‘, include:[‘*.jar‘])    ‘com.android.support:appcompat-v7:22.2.1‘    ‘com.android.support:recyclerview-v7:22.2.1‘}

Ps:eclipse users can import Recyclerview this project and then rely on their project to Recyclerview this project

We're done with our preparations here, and then we're going to start coding.

In the layout file, add:

<android.support.v7.widget.RecyclerView       android:id="@+id/recyclerView"       android:layout_width="match_parent"       android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>

Yes, that's right, so we're going to add a Recyclerview control to the layout.

Activity code:

ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.support.v7.widget.GridLayoutManager;ImportAndroid.support.v7.widget.LinearLayoutManager;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.support.v7.widget.StaggeredGridLayoutManager;ImportJava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.List;ImportJava.util.Map; Public  class mainactivity extends appcompatactivity {    PrivateRecyclerview Mrecyclerview;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_main); Mrecyclerview = (Recyclerview) Findviewbyid (R.id.recyclerview);//Linear layout managerLinearlayoutmanager LLMH =NewLinearlayoutmanager ( This);//Set to LandscapeLlmh.setorientation (linearlayoutmanager.horizontal);//Linear layout managerLinearlayoutmanager LLMV =NewLinearlayoutmanager ( This);//Set to PortraitLlmv.setorientation (linearlayoutmanager.vertical);//Grid layout manager, second parameter for specifying several columnsGridlayoutmanager GLM =NewGridlayoutmanager ( This,3);//Flow layout manager, the first parameter is set to several columns, the second parameter is arrangedStaggeredgridlayoutmanager SGLM =NewStaggeredgridlayoutmanager (3, staggeredgridlayoutmanager.vertical);//Set layout manager for RecyclerviewMrecyclerview.setlayoutmanager (SGLM);//Set data sourcelist<map<string,object>> data =NewArraylist<map<string,object>> ();intloop =Ten; while(loop>0) {Map<string,object> Map =NewHashmap<string, object> (); Map.put ("Text","I ' m data"+loop+" ! ");            Data.add (map);        loop--; } Recycleadapter adapter =NewRecycleadapter (data, This);//Add AdapterMrecyclerview.setadapter (adapter); }}

Recycleadapter Code:

Import Android.support.v7.widget.recyclerview;import Android.view.layoutinflater;import Android.view.View;import Android.view.viewgroup;import Android.widget.textview;import Java.util.list;import Java.util.Map; Public  class recycleadapter extends recyclerview. Adapter<recycleadapter. Viewholder>{    PrivateList<map<string,object>> Mdata; PublicRecycleadapter (list<map<string,object>> data) { This. mdata = data; } class viewholder extends recyclerview. Viewholder {        PrivateTextView TV; PublicViewholder (View v) {Super(v); This. TV = (TextView) V.findviewbyid (r.id.tv); }} @Override Public intGetItemCount () {returnMdata.size (); }//Fill data@Override Public voidOnbindviewholder (Viewholder holder,intPosition) {Holder.tv.setText (Mdata.get (position). Get ("Text")+""); Holder.tv.setTag (Mdata.get (position). Get ("Text")+"");//Set Listener eventsHolder.tv.setOnClickListener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {Toast.maketext (Mcontext,view.gettag () +"", Toast.length_short). Show ();    }        }); }//Create Viewholder@Override PublicViewholder Oncreateviewholder (viewgroup parent,intPosition) {Layoutinflater Inflater = Layoutinflater.from (Parent.getcontext ()); View v = inflater.inflate (r.layout.item_layout, parent,false); Viewholder VH =NewViewholder (v);returnVh }}

Item_layout.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:background="Attr/selectableitembackground">                <TextViewandroid:id= "@+id/tv"android:layout_width="match_parent "android:layout_height="wrap_content "android:minheight=" 50DP "/>                                 </linearlayout>

So far, the code is written, and here's the following:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android--The use of 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.