Android dropdown reload on load Smartrefreshlayout + Recyclerview

Source: Internet
Author: User
Tags addall

When it comes to Android refresh, it can be a bit of a struggle, finally found that there are ready-made controls, and very useful, here to record.

The original is 78781682, here is to see after, combined with their actual problems encountered in writing.

First introduce the package.

    drop-down box    implementation ' COM.ANDROID.SUPPORT:RECYCLERVIEW-V7:28.0.0-BETA01 '    implementation ' com.scwang.smartrefresh:smartrefreshlayout:1.0.3 '    implementation ' Com.scwang.smartrefresh: smartrefreshheader:1.0.3 '//Do not use special header, can not add this line
Recyclerview is for the data display, because in use, found using the ListView will error, but I see others use the success of, specifically I also lazy to check.
Recyclerview after the concrete in the study bar, anyway is a more useful thing is.
The first is the paging file Activity_main.xml
<com.scwang.smartrefresh.layout.smartrefreshlayout    xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools " Android:id= "@+id/refreshlayout" android:layout_width= "match_parent" android:layout_height= "match_parent" Tools: Context= ". Mainactivity "    ><android.support.v7.widget.recyclerview    android:id=" @+id/recyclerview "    Android:layout_width= "Match_parent"    android:layout_height= "match_parent"    android:overscrollmode= "never "    android:background=" #fff "/></com.scwang.smartrefresh.layout.smartrefreshlayout>

Next is the specific layout display file: Item.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" horizontal "android:layout_width=" Match_parent "android:layout_height=" 60DP "and roid:gravity= "center" > <imageview android:id= "@+id/iv_image" android:layout_width= "60DP" and roid:layout_height= "60DP" android:gravity= "center" android:background= "@mipmap/ic_launcher" ></imagevie w> <linearlayout android:layout_width= "match_parent" android:layout_height= "Match_parent" an droid:orientation= "vertical" > <textview android:id= "@+id/tv_title" android:layout_width = "Match_parent" android:layout_height= "30DP" android:text= "title" android:gravity= "Center "></TextView> <textview android:id=" @+id/tv_content "Android:layout_w Idth= "Match_parent" Androidoid:layout_height= "30DP" android:text= "content1231231231232323" android:gravity= "center" ></TextView> </LinearLayout></LinearLayout>

The Bean object that is displayed,

Package Com.example.administrator.shuaxin;public class Itembean {    int itemimage;    String Itemtitle;    String itemcontent;    Public Itembean (int itemimage, string itemtitle, String itemcontent) {        this.itemtitle = itemtitle;        This.itemcontent = itemcontent; this.itemimage = itemimage;}}      

Adapter Myadapter:

PackageCom.example.administrator.shuaxin;importAndroid.support.v7.widget.recyclerview;importAndroid.view.layoutinflater;importAndroid.view.view;importAndroid.view.viewgroup;importAndroid.widget.imageview;importAndroid.widget.textview;importJava.util.list;public class Myadapter extends recyclerview.adapter<myadapter.viewholder>{Private list<itembean>Mlist; Static Class Viewholder extendsrecyclerview.viewholder{View MyView; ImageView ImageView; TextView title; TextView content; PublicViewholder (View Itemview) {Super(Itemview); MyView =Itemview; ImageView =(ImageView) Itemview.findviewbyid (r.id.iv_image); title =(TextView) Itemview.findviewbyid (r.id.tv_title); Content =(TextView) Itemview.findviewbyid (r.id.tv_content); }} public Myadapter (list<itembean>List) {this.mlist =List } @Override Public Viewholder oncreateviewholder (viewgroup parent, intViewType) {View view = Layoutinflater.from (Parent.getcontext ()). Inflate (R.layout.item,null); final Viewholder holder = new viewholder (view); return holder;}//Bind data to the control @Override public void Onbindviewholder (viewholder holder, int position) {Itembean Bean = mlist.get (position); Holder.imageView.setBackgroundResource (bean.itemimage); Holder.title.setText (Bean.itemtitle); Holder.content.setText (bean.itemcontent); } @Override public int GetItemCount () {return mlist.size ();}//The following two methods are provided to the page to refresh and load when the public void add is called (LIST<ITEMB Ean> addmessagelist) {//Add data int position = mlist.size (); Mlist.addall (position, addmessagelist); notifyiteminserted (position); } public void Refresh (list<itembean> newlist) {//Refresh Data Mlist.removeall (mlist); Mlist.addall (NewList); Notifydatasetchanged (); }}

Then the activity file.

PackageCom.example.administrator.shuaxin;importAndroid.support.v7.app.appcompatactivity;importAndroid.os.bundle;importAndroid.support.v7.widget.linearlayoutmanager;importAndroid.support.v7.widget.recyclerview;importCom.scwang.smartrefresh.header.materialheader;importCom.scwang.smartrefresh.layout.api.refreshlayout;importCom.scwang.smartrefresh.layout.footer.classicsfooter;importCom.scwang.smartrefresh.layout.header.classicsheader;importCom.scwang.smartrefresh.layout.listener.onloadmorelistener;importCom.scwang.smartrefresh.layout.listener.onrefreshlistener;importJava.util.arraylist;importJava.util.date;importJava.util.List; public class Mainactivity extendsappcompatactivity {Private list<itembean>List PrivateMyadapter Myadapter; PrivateRecyclerview Recyclerview; Refreshlayout refreshlayout; @Override protected voidOnCreate (Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Refreshlayout =(refreshlayout) Findviewbyid (r.id.refreshlayout); Initial data loadInitdate (); Refresh Data LoadSetpullrefresher (); } private voidInitdate () {list = new arraylist<itembean>(); for (int i=0;i<20;i++) {List.add (newItembean (R.mipmap.ic_launcher, "inittitle" +I, new Date (). toString () + "")); } myadapter = newMyadapter (list); Recyclerview =(Recyclerview) Findviewbyid (R.id.recyclerview); Linearlayoutmanager LayoutManager = new Linearlayoutmanager (this);//Vertical Linear layoutRecyclerview.setlayoutmanager (LayoutManager); Recyclerview.setadapter (Myadapter); } private voidSetpullrefresher () {//Set header to Materialheader//set header to classicsfooter more classic style refreshlayout.setrefreshheader (new Classicsheader (This)); Set Footer to Classic style refreshlayout.setrefreshfooter (new Classicsfooter (this)); Refreshlayout.setonrefreshlistener (NewOnrefreshlistener () {@Override public voidOnrefresh (Refreshlayout refreshlayout) {///Where to perform a pull-up refresh (network request, update UI, etc.)//analog network request to the data arraylist<itembean> NewList = new Arraylist<itembean>(); for (int i=0;i<20;i++) {newlist.add (new Itembean (R.mipmap.ic_launcher, "newtitle" +I, new Date () . toString () + "" )); } myadapter.refresh (NewList); Refreshlayout.finishrefresh (2000/*,false*/);//Do not pass time immediately stop flush incoming false indicates refresh failed }); Refreshlayout.setonloadmorelistener (New Onloadmorelistener () {@Override public void Onloadmore ( Refreshlayout refreshlayout) {//analog network request to data arraylist<itembean> NewList = new arraylist<itembean>(); for (int i=0;i<20;i++) {newlist.add (new Itembean (R.mipmap.ic_launcher, "addtitle" +I, new Date (). ToString () + "" )); } myadapter.add (NewList); Here to perform the specific operation of the drop-down load (network request, update UI, etc.) Refreshlayout.finishloadmore (2000/*,false*/);//Do not pass the time immediately stop flush incoming false indicates load failed }); } }

Look at the effect.

Android dropdown Refresh on load smartrefreshlayout + 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.