Recyclerview (i)

Source: Internet
Author: User

The Android system launched the new control--recyclerview in version 5.0. Recyclerview almost replaced the listview we used to use. Because we no longer need to manage a viewholder with the static keyword, we no longer need to control the item's recycling, but it also gives us an animated interface for adding and deducting item. Finally, most importantly, it provides a better decoupling for such a multi-repeating layout as the ListView. A list such as the ListView is disassembled into a recyclable layout without layout rules, and a layout manager.

This series will record my Learning to use the Recyclerview process, I hope through this time, I can use it to completely replace the Recyclerview.

First look at basic use:

The basic use of Recyclerview is composed of 4 parts, namely Recyclerview, Adapter, Viewholder and LayoutManager.

Among them, adapter is responsible for the connection of Viewholder, data and Recyclerview, while LayoutManager manages several viewholder relationships.

Here I write a simple example:

Viewholder:

ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.view.View;ImportAndroid.widget.TextView;/*** Created by Fishbonelsy on 2016/6/25.*/ Public classItemviewholder<textendsBasebean>extendsRecyclerview.viewholder {TextView TextView;  PublicItemviewholder (View itemview) {Super(Itemview); TextView=(TextView) Itemview.findviewbyid (r.id.item_textview_id); }     Public voidBindviewholder (T Bean,intposition) {        if(BeaninstanceofDatabean)        {Textview.settext ((Databean) bean). GetName ()); }    }}

We get an instance of the item child control in the construction method of the Viewholder. Then, you write a public method that connects the data to the Viewholder.

Adapter:

ImportAndroid.content.Context;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;Importjava.util.List;/*** Created by Fishbonelsy on 2016/6/25.*/ Public classRecyclerviewadapterextendsRecyclerview.adapter<itemviewholder>{Context mcontext; List<BaseBean>mdatalist;  PublicRecyclerviewadapter (context context, list<basebean>dataList) {Mcontext=context; Mdatalist=dataList; } @Override PublicItemviewholder Oncreateviewholder (viewgroup parent,intViewType) {View View= Layoutinflater.from (Mcontext). Inflate (r.layout.recyclerview_item_layout, parent,false); return Newitemviewholder (view); } @Override Public voidOnbindviewholder (Itemviewholder holder,intposition)    {Holder.bindviewholder (Mdatalist.get (position), position); } @Override Public intGetItemCount () {if(Mdatalist! =NULL){            returnmdatalist.size (); }        return0; }}

There are three methods in adapter, namely:

Gets the Viewholder instance, usually an item that is only called once in a list;

Connect Viewholder and data, this method is called once when item is recycled and needs to be displayed again;

Gets the number of items managed by Recyclerview, called at Setadapter and adapter.notifydatasetchanged.

Finally, let's take a look at the Recyclerview settings:

  

ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.support.v7.widget.LinearLayoutManager;ImportAndroid.support.v7.widget.RecyclerView;ImportAndroid.support.v7.widget.StaggeredGridLayoutManager;Importjava.util.ArrayList;Importjava.util.List; Public classMainactivityextendsappcompatactivity {Recyclerview recyclerview;    Recyclerviewadapter adapter; List<BaseBean>dataList; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); DataList=NewArraylist<>(); Recyclerview=(Recyclerview) Findviewbyid (r.id.recycler_view_id); Adapter=NewRecyclerviewadapter ( This, dataList);        Recyclerview.setadapter (adapter); Recyclerview.layoutmanager LayoutManager=NewLinearlayoutmanager ( This);        Recyclerview.setlayoutmanager (LayoutManager);        Inittest ();    Adapter.notifydatasetchanged (); }    Private voidinittest () { for(inti = 0; I < 30; i++) {Datalist.add (NewDatabean ("position =" +i)); }    }}

  

In this way, a basic class listview list is born. At present, its main advantage is only two, one is a better recovery mechanism, and the other is the layout of the free way.

The disadvantage is a lot of: no Headerview and Footerview, no fastscroller and so on. Next, we will gradually discover its more advantages and compensate for its flaws.

  

done~

Recyclerview (i)

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.