Android app uses Recyclerview to implement pull and drop-down refresh methods _android

Source: Internet
Author: User

About Recyclerview

Recyclerview was introduced since Android 5.0, and ListView inherited Abslistview, and Recyclerview directly inherits ViewGroup and implements Scrollingview and Nestedscrollingchild interface, Recyclerview compared to ListView, is a radical change.
Recyclerview is more advanced and flexible than ListView, and for many views it is a container that can be reused and scrolled effectively. Use it when the data changes dynamically.
Recyclerview is easy to use because it provides:
1, it provides a layoutmanager for the location of the item
2, for the operation of the item to provide a default animations
3, you can also flexibly define the widget's custom layout manager and animation

Implement pull and drop flushes
Layout file:

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

Reference this layout in the activity and initialize it.

@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
 
 Setcontentview (R.layout.activity_main); 
 Mswiperefreshwidget = (swiperefreshlayout) Findviewbyid (r.id.swipe_refresh_widget); Mrecyclerview = (Recyclerview) Findviewbyid (Android. 
 
 R.id.list); 
 Mswiperefreshwidget.setcolorscheme (R.color.color1, R.color.color2, R.color.color3, R.color.color4); 
 
 Mswiperefreshwidget.setonrefreshlistener (this); This is for the first time you go to the page. Displays the Load progress bar Mswiperefreshwidget.setprogressviewoffset (False, 0, (int) typedvalue. Applydimension (Typ 
 
 Edvalue.complex_unit_dip, Getresources (), Getdisplaymetrics ())); 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 ()) {  Mswiperefreshwidget.setrefreshing (TRUE); 
  Here in the real project, please replace the network request data code, SendRequest ... handler.sendemptymessagedelayed (0, 3000); @Override public void onscrolled (recyclerview recyclerview, int dx, int dy) {super.onscrolled (Recyclerv 
  Iew, DX, dy); 
  Lastvisibleitem = Mlayoutmanager.findlastvisibleitemposition (); 
 
 } 
 
 }); 
 Mrecyclerview.sethasfixedsize (TRUE); 
 Mlayoutmanager = new Linearlayoutmanager (this); 
 Mrecyclerview.setlayoutmanager (Mlayoutmanager); 
 
 Mrecyclerview.setitemanimator (New Defaultitemanimator ()); 
 adapter = new Sampleadapter (); 
 
 Mrecyclerview.setadapter (adapter); 
 Here in the real project, please replace the network request data code, SendRequest ... handler.sendemptymessagedelayed (0, 3000); 

 }

Swiperefreshlayout API to NOTE:
1, Setonrefreshlistener (Onrefreshlistener Listener) set down to monitor, when the user Drop-down will go to perform the callback
2, setcolorschemecolors (int ... colors) set the progress bar color change, can set up to 4 colors
3, Setprogressviewoffset (Boolean scale, int start, int end) Adjust the progress bar distance from the top of the screen
4. The setrefreshing (Boolean refreshing) setting Swiperefreshlayout is currently in a refresh state, typically set to true when the data is requested, and set to False when the data is loaded into view.

Implementation of Recyclerview:

The first one, the Drop-down refresh and the pull refresh are all swiperefreshlayout with the progress bar

Mrecyclerview = (Recyclerview) Findviewbyid (Android. 
R.id.list); 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 ()) {Mswiper 
   Efreshwidget.setrefreshing (TRUE); 
  Here in the real project, please replace the network request data code, SendRequest ... handler.sendemptymessagedelayed (0, 3000); @Override public void onscrolled (recyclerview recyclerview, int dx, int dy) {super.onscrolled (Recyclerv 
  Iew, DX, dy); 
  Lastvisibleitem = Mlayoutmanager.findlastvisibleitemposition (); 
 
 } 
 }); 
 Mrecyclerview.sethasfixedsize (TRUE); 
 Mlayoutmanager = new Linearlayoutmanager (this); 
 Mrecyclerview.setlayoutmanager (Mlayoutmanager); 
 
 Mrecyclerview.setitemanimator (New Defaultitemanimator ()); 
 adapter = new Sampleadapter (); MReCyclerview.setadapter (adapter); 

 

The second implementation Drop-down refresh with Swiperefreshlayout with the progress bar, pull refresh with similar listview refresh hint "loading" and other information.

We can also add a similar Footerview item to Recyclerview.
We implement in adapter:

public class Sampleadapter extends recyclerview.adapter<viewholder> {private list<integer> List; 
 private static final int type_item = 0; 
 
 private static final int type_footer = 1; 
 Public list<integer> GetList () {return List; 
 Public Sampleadapter () {list = new arraylist<integer> (); 
 The count of//Recyclerview is set to the total number of data bars + 1 (footerview) @Override public int GetItemCount () {return list.size () + 1; @Override public int Getitemviewtype (int position) {//last item set to Footerview if (position + 1 = GetItemCount 
 ()) {return type_footer; 
 else {return type_item; @Override public void Onbindviewholder (Viewholder holder, final int position) {if (Holder instanceof ITEMVI 
 Ewholder) {((itemviewholder) holder). Textview.settext (string.valueof (list. get (position))); @Override public Viewholder Oncreateviewholder (viewgroup parent, int viewtype) {if (ViewType = Type_item) {View vieW = Layoutinflater.from (Parent.getcontext ()). Inflate (r.layout.list_item_text, NULL); 
  View.setlayoutparams (New Layoutparams (Layoutparams.match_parent, layoutparams.wrap_content)); 
 Return to New Itemviewholder (view); }//Type = = Type_footer returns Footerview else if (ViewType = = Type_footer) {View view = Layoutinflater.from (parent.ge 
  Tcontext ()). Inflate (R.layout.footerview, NULL); 
  View.setlayoutparams (New Layoutparams (Layoutparams.match_parent, layoutparams.wrap_content)); 
 Return to New Footerviewholder (view); 
 return null; 
 Class Footerviewholder extends Viewholder {public Footerviewholder (view) {super (view); 
 
 } class Itemviewholder extends Viewholder {TextView TextView; 
  Public Itemviewholder (view view) {super (view); 
 TextView = (TextView) View.findviewbyid (R.id.text); 

 } 
 } 
}

This allows us to do some processing for the layout of the Footerview, such as prompting "load,", "loaded", and so on. A little more flexible

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.