How do I use Android's newest Recyclerview instead of a ListView?

Source: Internet
Author: User

As follows:


Before using Recyclerview, you need to import the jar package where Android.support.v7.widget.RecyclerView is located. Just below the SUPPORT.V7, the directory structure is as follows:

... \android-sdk-windows\extras\android\support\v7\recyclerview\libs\android-support-v7-recyclerview.jar


Find your SDK directory, follow the above path to find Android-support-v7-recyclerview.jar, and then put it in the Android project directory under Libs, you can start using:

Import Android.support.v7.widget.RecyclerView;

Give the full source code above, first of all the Mainactivity.java:

Package Zhangphil.recyclerview;import Android.support.v7.app.actionbaractivity;import Android.support.v7.widget.linearlayoutmanager;import Android.support.v7.widget.recyclerview;import Android.view.view;import Android.view.viewgroup;import Android.widget.linearlayout;import Android.widget.TextView         Import Android.widget.toast;import android.graphics.color;import android.os.bundle;/** * @author Zhang Phil * This example is a concise demonstration of how to use Android's newest Recyclerview instead of the ListView * */public class Mainactivity extends Actionbaractivity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Recyclerview Mrecyclerview = (recyclerview) Findviewbyid (R.id.my_recycler_view); Linearlayoutmanager Mlayoutmanager = new Linearlayoutmanager (this);//Set parameter: Linearlayout.horizontal, you can easily achieve the horizontal " ListView ". If vertical (linearlayout.vertical), this is the previous ListView style. Mlayoutmanager.setorientation (linearlayout.horizontal); Mrecyclerview.setlayoutmanager (mLayoutManager),//test DataSet string[] data = new String[20];for (int i = 0; i < data.length; i++) {Data[i] = "data-" + I;} Recyclerview.adapter madapter = new Myadapter (data); Mrecyclerview.setadapter (Madapter);} Recyclerview adapters//Recyclerview and ListView are similar and need to be given a adapter. public class Myadapter extends recyclerview.adapter<myadapter.viewholder> {//DataSet private string[] Data;public Myadapter (string[] dataset) {super ();d ATA = DataSet;} This method is intended to complete the creation of the Viewholder neutron view, in other words, to initialize the individual objects defined in the Viewholder. Imagine, if not, then the set in our onbindviewholder is not in null above operation?! @Overridepublic viewholder Oncreateviewholder (viewgroup viewgroup, int i) {//Inflate a view, for demonstration, We use the layout provided by the Android system directly as a test layout,//a textviewview view for displaying content = View.inflate (Getapplication (), Android. R.layout.simple_list_item_1, NULL);//Create a viewholder//Note: Here, you need to pass the view instance of the previous step as a parameter to the Viewholder as the parameter passed to the Android system for use. Viewholder holder = new Viewholder (view); Holder.mtextview = (TextView) View.findviewbyid (Android. R.ID.TEXT1); Holder.mTextView.setTextColor (color.red); HolDer.mTextView.setOnClickListener (New View.onclicklistener () {@Overridepublic void OnClick (View v) {Toast.maketext ( Getapplication (), ((TextView) v). GetText (), Toast.length_short). Show ();}); return holder;} In this method, the data elements are associated with the view. Fill in the data into the view. Populate the view with data, in other words, render the view with data. In doing so, loose coupling can be achieved: separation of data and view. @Overridepublic void Onbindviewholder (viewholder viewholder, int i) {//Render data to Viewholder ViewHolder.mTextView.setText ( Data[i]);} Needless to say, here and the adapter in the ListView returns the length that tells the ListView how long data to load is similar. @Overridepublic int GetItemCount () {return data.length;} The Viewholder here only serves as a container for all types of objects, storing only cached data. Basically, in this viewholder, you define only the relevant operations, defining the items in the view. Of course, you can also do some more related actions inside. public class Viewholder extends Recyclerview.viewholder {public TextView mtextview;public Viewholder (View itemview) { Super (Itemview);}}}

Mainactivity.java Loaded Activity_main.xml:

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent ">    <android.support.v7.widget.recyclerview        Android:id= "@+id/my_recycler_view"        android:layout_width= "match_parent"        android:layout_height= "Match_ Parent "/></framelayout>




How do I use Android's newest Recyclerview instead of a ListView?

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.