In Android, use Recyclerview to implement drop-down refresh and pull-load _android

Source: Internet
Author: User
Tags static class recyclerview android

Recommended reading: Ways to add headers and footer using Recyclerview

The use of Recyclerview HelloWorld

Recyclerview is a newly added SDK in the Android L version to replace ListView, which is more flexible and fungible than ListView. This article describes how to add a drop-down refresh and pull load for Recyclerview, the past in the ListView to add Drop-down refresh and pull loading is very convenient to use Addheaderview and Addfooterview, The Recyclerview refresh also needs to be achieved by adding head or foot to the list. This article will not tell you in detail how to do a drop-down refresh and load effect, but how to quickly use the existing well-known open source library for Recyclerview to achieve the following effect.

First to show you the effect of the picture, interested friends please continue to read the full text.

This drop-down refresh is not achieved by adding headerview to the list, but in the recyclerview outside a layer viewgroup and this viewgroup support Drop-down refresh, you can smart implementation of a number of child view Drop-down Refresh, This is the famous Android-ultra-pull-to-refresh Open Source Library, the author Liaohuqiu, but unfortunately it does not support pull loading, this article is based on this library to expand and achieve the pull load effect of open Source Library, The Oberra effect is achieved by adding Footerview,github addresses in Recyclerview: Https://github.com/Chanven/CommonPullToRefresh thanks to the developers who are active in the open source community.

Specific steps to implement: (Development tools: Android Studio)

1:gradle Configuration Build.gradle

Optionally, download the library source code copy into the project at the above address
compile ' com.chanven.lib:cptr:1.0.0 ' 
//Recyclerview and CardView
Compile ' com.android.support:recyclerview-v7:23.1.1 '
compile ' com.android.support:cardview-v7:23.1.1 '

2: Main layout file activity_main.xml inside a recyclerview nested is the support of the Drop-down refresh ViewGroup

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Match_parent "android:layout_height=" Match_parent "> < Com.chanven.lib.cptr.PtrClassicFrameLayout android:id= "@+id/rotate_header_list_view_frame" xmlns:cube_ptr= "http
://schemas.android.com/apk/res-auto "android:layout_width=" match_parent "android:layout_height=" Match_parent "
Android:background= "#e1e1e1" cube_ptr:ptr_duration_to_close= "cube_ptr:ptr_duration_to_close_header=" 1000 " Cube_ptr:ptr_keep_header_when_refresh= "true" Cube_ptr:ptr_pull_to_fresh= "false" Cube_ptr:ptr_ratio_of_header_ Height_to_refresh= "1.2" cube_ptr:ptr_resistance= "1.7" > <android.support.v7.widget.recyclerview android: Background= "#ffffff" xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent "android:layout_height=" match_parent "android:id=" @+id/rv_list "/> </ Com.chanven.lib.cptr.ptrclassicframelayout> </lineaRlayout> 

3: List item layout rv_item.xml outside a CardView card-type container, inside a TextView

<?xml version= "1.0" encoding= "Utf-8"?> <android.support.v7.widget.cardview xmlns:card_view=
" Http://schemas.android.com/apk/res-auto "
xmlns:android=" Http://schemas.android.com/apk/res/android
" Android:layout_width= "Match_parent"
android:layout_height= "wrap_content"
android:layout_margin= "8DP"
android:id= "@+id/cv_item"
android:foreground= "Android:attr/selectableitembackground"
Card_view : cardcornerradius= "4DP"
card_view:cardelevation= "4DP"
>
<linearlayout
android:layout _width= "Match_parent"
android:layout_height= "wrap_content" >
<textview
android:layout_ Width= "Wrap_content"
android:layout_height= "wrap_content"
android:id= "@+id/tv_item_text
" android:text= "Test"
android:layout_margin= "8DP"
/>
</LinearLayout>
</ Android.support.v7.widget.cardview>

4:recyclerview adapter Rvadapter.java

public class Rvadapter extends recyclerview.adapter<recyclerview.viewholder> {private Layoutinflater
Mlayoutinflater;
Private context Mcontext;
Private List <String> Mtitle; Public Rvadapter (Context context,list<string> title) {mcontext=context; mtitle=title; mlayoutinflater=
Layoutinflater.from (context); }//Custom Viewholder, holding all interface elements for each item public static class Normalviewholder extends Recyclerview.viewholder {TextView
Mtextview;
CardView Mcardview; Public Normalviewholder (View Itemview) {super (Itemview); mtextview= (TextView) Itemview.findviewbyid (r.id.tv_item_
Text);
mcardview= (CardView) Itemview.findviewbyid (R.id.cv_item); In this method we create a viewholder and return, Viewholder must have a constructor with a view, which is the root layout of our item, where we use the layout of the custom item; @Override Public Normalviewholder Oncreateviewholder (viewgroup parent, int viewtype) {return new Normalviewholder (
Mlayoutinflater.inflate (R.layout.rv_item,parent,false)); @Override public void Onbindviewholder (recyclerview.viewholder holder, int positION) {Normalviewholder Viewholder = (normalviewholder) holder; Viewholder.mTextView.setText (mtitle.get (position)); /Gets the number of data @Override public int getitemcount () {return mtitle==null? 0:mtitle.size ();}}

5: Implementation of the most important activity, Mainactivity.java, code meaning has been commented

public class Mainactivity extends Appcompatactivity {private Recyclerview Mrecyclerview;//Support dropdown refresh ViewGroup private ptrc
Lassicframelayout Mptrframe;
List Data Private list<string> title = new Arraylist<> ();
Recyclerview Custom Adapter Private Rvadapter adapter;
Add headers and footer to the package class private RECYCLERADAPTERWITHHF madapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Mrecyclerview = (Recyclerview) Findviewbyid (r.id.rv_list);
Linearlayoutmanager LayoutManager = new Linearlayoutmanager (this);
Layoutmanager.setorientation (linearlayoutmanager.vertical);
Mrecyclerview.setlayoutmanager (LayoutManager);
adapter = new Rvadapter (mainactivity.this, title);
Madapter = new RECYCLERADAPTERWITHHF (adapter);
Mrecyclerview.setadapter (Madapter);
Mptrframe = (ptrclassicframelayout) Findviewbyid (r.id.rotate_header_list_view_frame);
Dropdown Refresh Support Time Mptrframe.setlastupdatetimerelateobject (this); Dropdown refresh some settings details referenceFile Mptrframe.setresistance (1.7f);
Mptrframe.setratioofheaderheighttorefresh (1.2f);
Mptrframe.setdurationtoclose (200);
Mptrframe.setdurationtocloseheader (1000);
Default is False Mptrframe.setpulltorefresh (false);
Default is True Mptrframe.setkeepheaderwhenrefresh (true); Enter the activity automatically drop-down refresh mptrframe.postdelayed (new Runnable () {@Override public void run () {Mptrframe.autorefresh ();}}, 1
00);
Drop-down Refresh Mptrframe.setptrhandler (new Ptrdefaulthandler () {@Override public void Onrefreshbegin (Ptrframelayout frame) {
Title.clear (); Analog data for (int i = 0; I <= 5; i++) {Title.add (string.valueof (i));}//Simulate networked delay update list new Handler (). postdelayed (New Runna BLE () {public void run () {madapter.notifydatasetchanged (); Mptrframe.refreshcomplete (); Mptrframe.setloadmoreenable (
true);
}, 1000);
}
}); Pull load Mptrframe.setonloadmorelistener (new Onloadmorelistener () {@Override public void Loadmore () {//Analog networking deferred update data new Han Dler (). postdelayed (New Runnable () {@Override public void run () {//Impersonation data for (iNT i = 0; I <= 5;
i++) {Title.add (string.valueof (i));} madapter.notifydatasetchanged ();
Mptrframe.loadmorecomplete (TRUE);
Toast.maketext (Mainactivity.this, "load more complete", Toast.length_short). Show ();
}, 1000);
}
}); }
}

The above content is small set to introduce the Android using Recyclerview to achieve Drop-down refresh and loading tutorial, I hope to help!

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.