"Android" Recyclerview

Source: Internet
Author: User

RecyclerviewRecyclerview Concise

Recyclerview in the v7.21+ package is a component for displaying a large amount of data, or an improved version of the ListView (note: The functionality of this stage is not perfect for the ListView, so it is unwise to completely replace the ListView).
It's more scalable than Listview,recyclerview, so it's also better suited for use with Android's new components, which makes it easier to use.

Recyclerview is similar to the ListView principle, which is essentially adapter-centric. Only the ListView cache is view,viewholder attached to the view, while the Recyclerview cache is Viewholder,view contained within the Viewholder.

This design can also be implemented if we modify the listviewadapter slightly:

Abstract class LVADAPTER<VH extends Lvadapter.viewholder> extends Baseadapter {public    class Viewholder {        Private view view;        Public Viewholder (view view) {            This.view = view;        }        Public View GetView () {            return View;        }    }    @Override public    int GetCount () {        return 0;    }    @Override public    Object getItem (int position) {        return null;    }    @Override public    long getitemid (int position) {        return 0;    }    @Override public    View getView (int position, view Convertview, ViewGroup parent) {        VH VH = Getviewholder (); 
   
    onbindviewholder (VH, position);        return Vh.getview ();    }    Public abstract VH Getviewholder ();    public abstract void Onbindviewholder (VH holder, int position);}
   

This is a bit not intuitive, so further, the ListView adapter can be more abstract, the data binding method to Viewholder, so adapter work is more simple.

A convenient listviewadapter can refer to the Base-adapter-helper

Recyclerview useAdapter

Since the adapter is the core, you just have to write the adapter:

Class Rvviewholder extends Recyclerview.viewholder {public TextView TV;        Public Rvviewholder (View Itemview) {super (Itemview);    TV = (TextView) Itemview.findviewbyid (r.id.tv);    }}class Rvadapter extends Recyclerview.adapter<rvviewholder> {context context;    string[] items;        Public Rvadapter (context context, string[] items) {This.context = context;    This.items = items; } @Override Public Rvviewholder oncreateviewholder (viewgroup parent, int viewtype) {View view = Layoutinflat        Er.from (Parent.getcontext ()). Inflate (R.layout.apt_v7_recycler_view, parent, false);    return new Rvviewholder (view); } @Override public void Onbindviewholder (rvviewholder holder, int position) {Holder.tv.setText (items[positio    n]);    } @Override public int getitemcount () {return items.length; }}rv.setadapter (New Rvadapter (This, new string[]{"A", "B", "C", "D", "a", "B", "C", "D", "a", "B", " C, "D", "a", "B", "C", "D", "a", "B", "C", "D", "a", "B", "C", "D", "a", "B", "C", "D",  "A", "B", "C", "D", "a", "B", "C", "D", "a", "B", "C", "D", "a", "B", "C", "D", "a", "B", "C", "D", "A", "B", "C", "D"})); Linearlayoutmanager Linearlayoutmanager = new Linearlayoutmanager (this); Linearlayoutmanager.setorientation ( linearlayout.vertical); Rv.setlayoutmanager (Linearlayoutmanager);
Layout Manager and Landscape list

Recyclerview Another feature is the layout manager (LayoutManager), the personal feel of a more convenient place is, finally can be conveniently implemented landscape list ! The role of layout management is to be responsible for placing view and recycling view.
Similar to

int findfirstvisibleitemposition (); int findlastvisibleitemposition ();

Method is also in the layout manager

Set the item animation

Rv.setitemanimator (Itemanimator);

Split Line

Since the layout of the Recyclerview is not fixed, there is no way to set the divider directly like a ListView, which may need to be implemented on its own. A simple and rude vertical list of split lines is as follows:

Rv.additemdecoration (New Recyclerview.itemdecoration () {    Paint paint = new Paint ();    @Override public    void OnDraw (Canvas C, Recyclerview parent, recyclerview.state State) {        Super.ondraw (c, parent, State);    }    @Override public    void Ondrawover (Canvas C, Recyclerview parent, recyclerview.state State) {        Super.ondrawover (c , parent, state);        for (int i = 0, size = Parent.getchildcount (); i < size; i++) {            View child = Parent.getchildat (i);            C.drawline (Child.getleft (), Child.getbottom (), Child.getright (), Child.getbottom (), paint);        }    }    @Override public    void Getitemoffsets (Rect outrect, view view, Recyclerview parent, recyclerview.state state) {        super.getitemoffsets (Outrect, view, parent, State);    });

Also refer to: divideritemdecoration

Binding events

Recyclerview does not have a ListView Onitemclick method, so a similar method needs to be handled by itself.

    1. You can bind an event directly to Itemview or its child view, the binding can be either event binding in Viewholder, or it can be done in adapter.
      However, it is not possible to obtain the original item data in Viewholder, which requires further processing (such as setting tag to Itemview) in order to achieve the purpose of acquiring raw data, so how to use it is a trade-off.

      Class Rvviewholder extends Recyclerview.viewholder implements View.onclicklistener {

      Public TextView tv;public Rvviewholder (View itemview) {    super (Itemview);    TV = (TextView) Itemview.findviewbyid (r.id.tv);    Itemview.settag (this);    Itemview.setonclicklistener (this);} @Overridepublic void OnClick (View v) {    int vId = V.getid ();    if (vId = = r.id.tv) {        toast.maketext (Itemview.getcontext (), getPosition () + "", Toast.length_short). Show ();    }}

      }

    2. can use

      Addonitemtouchlistener (Recyclerview.onitemtouchlistener Listener)

      To perform global processing. Of course, the disadvantage is the need to deal with event details, more cumbersome.

Demo

Demo

Android Share Q Group: 315658668

"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.