Android Recyclerview,cardview and corresponding pull-up refresh pull-down loading

Source: Internet
Author: User
Tags recyclerview android

With the launch of the new design language, the new Android Support Library V7, which includes the implementation--cardview of the Material design language on the card concept, is Material. Recyclerview is also the Google V7 package under the new control, used to replace the use of the ListView, in Recyclerview standardized viewholder similar to the ListView Convertview used to do the view cache.

The advantage of Recyclerview is that he can quickly implement the effects of the ListView, GridView, and waterfall streams by setting up LayoutManager, and can also set landscape and portrait display, add animation effects, and so on.

CardView to achieve the card effect.

Pull-down loading uses swiperefreshlayout, which is the official drop-down refresh control, with a simple and beautiful style that makes it widely used in projects. In the ointment is swiperefreshlayout missing pull-loading effect, today combined with recyclerview implementation of a support pull-down refresh with the pull-up load of swiperefreshlayout.

The case contains: Recyclerview basic use, Recyclerview pull-up refresh drop-down loading, Recyclerview multi-item layout, CardView Basic use and CardView pull-up refresh drop-down loading. (Note: The third case line display format in the simulator has a problem, mobile phone is not a problem, mobile phone test results better)

Layout:

< Android . Support.v7.widget.RecyclerView    android:id= "@+id/my_recycler_view"    android: ScrollBars= "vertical"    android:layout_width= "Match_parent"     Android:layout_height= "match_parent"/>

Activity:

 Public classRecyclerviewactivityextendsActivity {PrivateRecyclerview Recyclerview; PrivateList<string>Mdatas; PrivateRecyclerAdapter1 Adapter; @SuppressLint ("Inlinedapi") @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.LAYOUT.DEMO1);        InitData ();    Initview (); }    Private voidInitview () {Recyclerview=(Recyclerview) Findviewbyid (R.id.demo1_recycler); //set up layout management LayoutManagerLinearlayoutmanager manager=NewLinearlayoutmanager ( This); //Set Horizontal OR vertical//manager.setorientation (linearlayoutmanager.horizontal);Recyclerview.setlayoutmanager (manager); //(optional) If you can determine the height of each item is fixed, set this option to raise the performanceRecyclerview.sethasfixedsize (true); Adapter=NewRecyclerAdapter1 ( This, Mdatas);        Recyclerview.setadapter (adapter); Adapter.setonitemclicklistener (NewIonclicklistenter () {@Override Public voidOnitemclick (View view,intposition) {Toast.maketext (Getapplicationcontext (), Position+"---"+Mdatas.get (position), Toast.length_long). Show ();    }        }); }    Private voidInitData () {Mdatas=NewArraylist<string>();  for(inti = 0; I <= 50; i++) {Mdatas.add ("Item---" +i); }    }}

Adapter:

 Public classRecyclerAdapter1extendsRecyclerview.adapter<recycleradapter1.viewholder>{    Privatecontext Context; PrivateList<string>list;  PublicRecyclerAdapter1 (context context, list<string>list) {         This. context=context;  This. list=list; } @Override PublicViewholder Oncreateviewholder (viewgroup parent,intViewType) {View View= Layoutinflater.from (context). Inflate (R.layout.demo1_item,parent,false); Viewholder Viewholder=Newviewholder (view); returnViewholder; } @Override Public voidOnbindviewholder (Viewholder holder,intposition)    {Holder.textView.setText (List.get (position)); } @Override Public intGetItemCount () {returnlist.size (); }    PrivateIonclicklistenter onclicklistenter=NULL;  Public voidSetonitemclicklistener (Ionclicklistenter listener) { This. onclicklistenter=Listener; }     Public classViewholderextendsRecyclerview.viewholderImplementsview.onclicklistener{PrivateTextView TextView;  PublicViewholder (view view) {Super(view); TextView=(TextView) View.findviewbyid (R.ID.TEXTVIEW1); Textview.setonclicklistener ( This); } @Override Public voidOnClick (View v) {if(Onclicklistenter! =NULL) {Onclicklistenter.onitemclick (v,getposition ()); }        }    }}

Pull-up Refresh drop-down load layout

<Android. Support.v4.widget.SwipeRefreshLayout Android:id= "@+id/swiperefreshlayout"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">        <Android. Support.v7.widget.RecyclerView Android:id= "@+id/recyclerview"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">        </Android.support.v7.widget.RecyclerView></Android.support.v4.widget.SwipeRefreshLayout>

Pull-up Refresh drop-down loading activity

 Public classRecyclerviewpullactivityextendsActivity {PrivateSwiperefreshlayout swiperefreshlayout; PrivateMyrefreshrecyclerview Recyclerview;  Public StaticList<string> list=NewArraylist<string>(); PrivateRecyclerAdapter2 Adapter; PrivateHandler Mhandler =NewHandler (); @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.LAYOUT.DEMO2);    Initview (); }    Private voidInitview () {swiperefreshlayout=(swiperefreshlayout) Findviewbyid (r.id.swipe_layout); Recyclerview=(Myrefreshrecyclerview) Findviewbyid (R.id.recycler); Adapter=NewRecyclerAdapter2 (); Recyclerview.setlayoutmanager (NewLinearlayoutmanager (Getapplicationcontext ()));        Swiperefreshlayout.setcolorschemeresources (R.color.blue,r.color.green); Swiperefreshlayout.post (NewRunnable () {@Override Public voidrun () {swiperefreshlayout.setrefreshing (true);        }        }); Mhandler.postdelayed (NewRunnable () {@Override Public voidrun () {getData ();                Recyclerview.setadapter (adapter);                Adapter.notifydatasetchanged (); Swiperefreshlayout.setrefreshing (false); }        },1500); Swiperefreshlayout.setonrefreshlistener (NewSwiperefreshlayout.onrefreshlistener () {@Override Public voidOnrefresh () {mhandler.postdelayed (NewRunnable () {@Override Public voidrun () {getData ();                        Recyclerview.setadapter (adapter);                        Adapter.notifydatasetchanged (); Swiperefreshlayout.setrefreshing (false); }                }, 1500);        }        }); Recyclerview.setmyrefreshrecyclerviewlistener (NewMyrefreshrecyclerview.myrefreshrecyclerviewlistener () {@Override Public voidOnloadmore () {mhandler.postdelayed (NewRunnable () {@Override Public voidrun () {if(List.size () > 14) {Recyclerview.setloadmore (true); } Else {                            intRandomint =NewRandom (). Nextint (100); List.add ("Pull-up load Add Number:" +randomint);                            Adapter.notifydatasetchanged (); Recyclerview.setloadmore (false); }                    }                }, 1000);    }        }); }    Private voidGetData () {list.clear (); Random Random=NewRandom ();  while(List.size () < 12) {            intRandomint = Random.nextint (100);        List.add (string.valueof (randomint)); }    }}
To add a dependent library:
  compile' com.android.support:recyclerview-v7:23.2.1 '  Compile ' com.android.support:cardview-v7:23.2.1 '
because the code is too many, the complete code is not given, the source directly download canSOURCE Click to download

Android Recyclerview,cardview and corresponding pull-up refresh pull-down loading

Related Article

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.