Recyclerview analysis of new Android control and pull-up and pull-down refresh

Source: Internet
Author: User

Overview:

Recyclerview is a new Widgets,recyclerview in the android-support-v7-21 version, which is a more advanced and flexible version of the ListView. In future development we will be able to replace the ListView directly with Recyclerview.


Feature Introduction:

1. Can be displayed horizontally

2. Eliminate dislocation problems

3. Standardization of Viewholder


Landscape:

private void Inithorizaontal (list<itemmodel> models) {Recyclerview Recyclerview = (recyclerview) Findviewbyid ( R.id.activity_main_horizontal_recyclerview); Linearlayoutmanager LayoutManager = new Linearlayoutmanager (mainactivity.this); Layoutmanager.setorientation ( Linearlayoutmanager.horizontal); Recyclerview.setlayoutmanager (LayoutManager); Recyclerviewadapter adapter = new Recyclerviewadapter (mainactivity.this, models); Recyclerview.setadapter (adapter);}

Vertical:

public void Initvertical (list<itemmodel> models) {Recyclerview Recyclerview = (recyclerview) Findviewbyid ( R.id.activity_main_vertical_recyclerview); Linearlayoutmanager LayoutManager = new Linearlayoutmanager (mainactivity.this); Layoutmanager.setorientation ( linearlayoutmanager.vertical); Recyclerview.setlayoutmanager (LayoutManager); Recyclerviewadapter adapter = new Recyclerviewadapter (mainactivity.this, models); Recyclerview.setadapter (adapter);}

Changes in adapter:

Google in the improvement of Recyclerview also for us to solve a beginner's headache problem: dislocation.

public class Recyclerviewadapter extends recyclerview.adapter<viewholder> {private list<itemmodel> mList = Null;private context Mcontext = null;private Layoutinflater minflater = null;public Recyclerviewadapter (context context, List<itemmodel> list) {Mcontext = Context;mlist = List;minflater = Layoutinflater.from (MContext);} @Overridepublic int GetItemCount () {if (mlist! = null) {return mlist.size ();} return 0;} @Overridepublic void Onbindviewholder (viewholder viewholder, int position) {((Itemviewholder) viewholder). Mlabeltextview.settext (Mlist.get (position). Getlabel ());} @Overridepublic viewholder Oncreateviewholder (viewgroup viewgroup, int arg1) {View view = Minflater.inflate ( R.layout.list_item, ViewGroup, false) Itemviewholder holder = new Itemviewholder (view); Holder.mlabeltextview = ( TextView) View.findviewbyid (R.id.list_item_textview); return holder;} public static class Itemviewholder extends viewholder{public itemviewholder (View Itemview) {Super (itEmview);    } private TextView mlabeltextview = null; }}


pull-up or drop-down refresh:

For the drop-down refresh, Android itself has a control swiperefreshlayout already implemented. We wrap the swiperefreshlayout outside the Recyclerview and then set it to bind to a onrefreshlistener.


layout:

<android.support.v4.widget.swiperefreshlayout    xmlns:android= "http://schemas.android.com/apk/res/android "    xmlns:tools=" Http://schemas.android.com/tools "    android:id=" @+id/swipe_refresh_widget "    android: Layout_width= "Match_parent"    android:layout_height= "match_parent" >    < Android.support.v7.widget.RecyclerView        android:id= "@android: Id/list"        android:layout_width= "Match_ Parent "        android:layout_height=" match_parent "        android:cachecolorhint=" @null "        android:scrollbars=" Vertical "/></android.support.v4.widget.swiperefreshlayout>


Monitoring:

@Override public    void Onrefresh () {        handler.sendemptymessagedelayed (0, +);    }

For the pull-up refresh, we are customizing a footerview and threads.

Mrecyclerview.setonscrolllistener (New Recyclerview.onscrolllistener () {            @Override public            Void Onscrollstatechanged (Recyclerview recyclerview,                    int newstate) {                super.onscrollstatechanged (RecyclerView, NewState);                if (newstate = = Recyclerview.scroll_state_idle && Lastvisibleitem + 1 = = Adapter.getitemcount ()) {                    Handler.sendemptymessagedelayed (1, +);                }            }            @Override public            void onscrolled (recyclerview recyclerview, int dx, int dy) {                super.onscrolled (Recyclerview, DX, dy);                Lastvisibleitem = Mlayoutmanager.findlastvisibleitemposition ();            }        });


related source Download:

Recyclerview Preliminary use

Recyclerview pull-up and drop-down refreshes


Recyclerview analysis of new Android control and pull-up and pull-down refresh

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.