Gold field
Drop-down refreshes are a relatively common effect, and Android 5.0 does not provide a similar control before the app, which is primarily used by third-party libraries, such as Pulltorefresh,actionbar-pulltorefresh. Right now the project needs to deal with the Android 5.0 material Design part of the thing, incidentally learn this part.
A general introduction;
- Swiperefreshlayout is a drop-down refresh control (Android-support-v4.jar) for Google in the Support V4 19.1 version of the library update
- Currently only supports drop-down refreshes, no pull-up loading is supported for more operations (self-scaling required)
- As an official self-contained control, the relative ability to ensure a relatively good versatility and style (this does not include a variety of home-customized system L)
- Swiperefreshlayout inherited from Viewgroup,viewgroup can contain other different controls, so the UI is relatively easy to customize.
- Easy to use, easy to implement Google Now refresh effect
The Swiperefreshlayout layout can currently contain only one child layout, using a listening mechanism to notify the Refresh event. For example, when a user uses a drop-down gesture, swiperefreshlayout triggers Onrefreshlistener, and the refresh event is processed in the Onrefresh () method. You can call Setrefreshing (false) when you need to end the refresh. If you want to disable gestures and progress animations, call SetEnabled (false).
Let's take a look at how it's used in general:
1. layout file (sample code)
<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " xmlns:tools=" Http://schemas.android.com/tools " android:layout_width=" Match_parent " android:layout_height= "Match_parent" > <android.support.v4.widget.swiperefreshlayout android:id= " @+id/id_explore_swipe_ly " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android:background= "#ffffff" > <listview android:id= "@+id/id_listview" android:layout_ Width= "Match_parent" android:layout_height= "match_parent" > </ListView> </ Android.support.v4.widget.swiperefreshlayout></framelayout>
2.java Logic Code:
You first need to implement the Swiperefreshlayout.onrefreshlistener interface and then override the method Onrefresh ():
@Overridepublic void Onrefresh () { new Handler (). postdelayed (New Runnable () { @Override public void Run () { //Sets whether the swiperefreshlayout is currently in a refreshed state, usually set to true when the data is requested, and set to False when the data is loaded into view. mswiperefreshlayout.setrefreshing (FALSE); } }, 3000);}
Now we initialize the control:
public void Initswiperefreshparameters () { //sets the color change of the progress bar, can be set up to a maximum of 4 colors mswiperefreshlayout.setcolorschemeresources (Android. R.color.holo_green_dark, Android. R.color.holo_green_light, Android. R.color.holo_orange_light, Android. R.color.holo_red_light); Set the pull-down listener to execute the callback Mswiperefreshlayout.setonrefreshlistener (this) when the user pulls down ; Adjust the progress bar distance from the top of the screen Mswiperefreshlayout.setprogressviewoffset (false, 0, (int) typedvalue.applydimension ( Typedvalue.complex_unit_dip, Getresources () getdisplaymetrics ());}
The above is the basic usage, now to roughly introduce the custom support pull-up section, demo diagram:
Figure 1 Pull-up loading effect
First implement the Swiperefreshlayout rewrite:
public class Myswiperefreshlayout extends Swiperefreshlayout implements Onscrolllistener {private int mtouchslop; Private ListView Mlistview; Private Onloadlistener Monloadlistener; Private View Mlistviewfooter; private int mydown; private int mlasty; Private Boolean isloading = false; Public Myswiperefreshlayout (Context context) {This (context, NULL); } public Myswiperefreshlayout (context context, AttributeSet Attrs) {Super (context, attrs); Mtouchslop = Viewconfiguration.get (context). Getscaledtouchslop (); Mlistviewfooter = Layoutinflater.from (context). Inflate (R.layout.listview_footer, NULL, FALSE); } @Override protected void OnLayout (Boolean changed, int left, int top, int. right, int bottom) {Super.onlayo UT (changed, left, top, right, bottom); Initializes the ListView object if (Mlistview = = null) {Getlistview (); }} private void Getlistview () {int childs = Getchildcount (); if (Childs > 0) {View Childview = getchildat (0); if (Childview instanceof listview) {Mlistview = (listview) Childview; Set the scroll listener to the ListView, making it possible to automatically load the Mlistview.setonscrolllistener (this) in the case of scrolling; }}} public void Setlistview (ListView list) {this.mlistview = list; Setloading (TRUE); This.mListView.setOnScrollListener (this); } @Override public boolean dispatchtouchevent (Motionevent event) {Final int action = Event.getaction (); Switch (action) {Case MotionEvent.ACTION_DOWN:mYDown = (int) Event.getrawy (); Break Case MotionEvent.ACTION_MOVE:mLastY = (int) Event.getrawy (); Break Case MotionEvent.ACTION_UP:if (Canload ()) {loaddata (); } break; Default:break; } Return Super.dispatchtouchevent (event); }/** * @ Method Description: Whether more can be loaded, the condition is at the bottom, the ListView is not loaded, and is a pull-up operation. */Private Boolean canload () {return isbottom () &&!isloading && Ispullup (); }/** * @ Method Description: Determines whether to the bottom */private Boolean isbottom () {if (Mlistview! = null && mlistview.ge Tadapter () = null) {return mlistview.getlastvisibleposition () = = (Mlistview.getadapter (). GetCount ()-1); } return false; }/** * @ Method Description: Is the pull-up operation */private Boolean Ispullup () {return (mydown-mlasty) >= mtouchslop; }/** * @ Method Description: If it is at the bottom and is a pull-up operation. Then execute the OnLoad method */private void LoadData () {if (Monloadlistener! = Nu ll) {monloadlistener.onload (); }//Set state setloading (TRUE); }/** * @ Method Description: Set refresh */public void setloading (Boolean loading) {isloading = loading; if (Mlistview! = null && Mlistview.getfooTerviewscount () > 0) mlistview.removefooterview (mlistviewfooter); if (isloading) {if (Mlistview! = null && mlistview.getfooterviewscount () <= 0) mlist View.addfooterview (Mlistviewfooter); } else {mydown = 0; mlasty = 0; }} public void Setonloadlistener (Onloadlistener loadlistener) {monloadlistener = Loadlistener; } @Override public void onscrollstatechanged (Abslistview view, int. scrollstate) {} @Override public void O Nscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {if (Isfastdoubleclick (1 (xx)) return; Scroll to the bottom can also load more isloading = false; if (Canload ()) {loaddata (); }} public View Getmlistviewfooter () {return mlistviewfooter; }/** * @ class Description: Load more listeners */public static interface Onloadlistener {public void onLoad (); } PRIvate Static long Lastclicktime; public static Boolean Isfastdoubleclick (long times) {Long time = System.currenttimemillis (); Long TimeD = Time-lastclicktime; if (0 < TimeD && TimeD < times) {return true; } lastclicktime = time; return false; }}
Mainactivity.java
public class Mainactivity extends Activity {private Mydapter adapter; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); adapter = new Mydapter (); Get Refreshlayout instance final myswiperefreshlayout Myrefreshlistview = (myswiperefreshlayout) Findviewbyid (R.id.swipe_ Layout); Gets the ListView instance of the ListView ListView = (ListView) Findviewbyid (R.id.listview); Myrefreshlistview.setlistview (ListView); Listview.setadapter (adapter); Set the color change of the progress bar, you can set up to 4 colors myrefreshlistview.setcolorschemeresources (Android. R.color.holo_green_dark, Android. R.color.holo_green_light, Android. R.color.holo_orange_light, Android. R.color.holo_red_light); Set the drop-down Refresh listener Myrefreshlistview.setonrefreshlistener (new Onrefreshlistener () {@Override publi c void Onrefresh () {Toast.maketext (Mainactivity.this, "refrEsh ", Toast.length_short). Show (); Myrefreshlistview.postdelayed (New Runnable () {@Override public void run () { Call the method end Refresh Myrefreshlistview.setrefreshing (false) after the update is complete; Adapter.getdata (). Clear (); Simulate some data for (int i = 0; i < i++) {adapter.adddata ("Liu Hhh" + i); }}}, 1000); } }); Load Listener Myrefreshlistview.setonloadlistener (new Onloadlistener () {@Override public void OnL Oad () {myrefreshlistview.postdelayed (new Runnable () {@Override Publ The method Adapter.adddata (new Date ()) is called when the IC Void run () {//Is loaded. toGMTString ()); Adapter.notifydatasetchanged (); Myrefreshlistview.setloading (FALSE); }}, 1500); } }); } class Mydapter extends Baseadapter {list<string> datas = new arraylist<string> (); Public Mydapter () {///simulates some data for (int i = 0; i <; i++) {Datas.add ("item-" + i); }} public void SetData (list<string> data) {This.datas = data; Notifydatasetchanged (); } public void AddData (String str) {this.datas.add (str); Notifydatasetchanged (); } public list<string> GetData () {return datas; } @Override public int getcount () {return datas.size (); } @Override public Object getItem (int position) {return datas.get (position); } @Override public long getitemid (int position) {return position; } @Override public View getView (int position, View Convertview, ViewGroup parent) {if (convert View = = null) {Convertview = MainActivity.this.getLayoutInflater (). Inflate (R.layout.item, NULL); } TextView TV = (TextView) Convertview.findviewbyid (R.id.text); Tv.settext (Datas.get (position)); return convertview; } }}
Listview_footer:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:background=" @color/very_ Light_gray "android:gravity=" center "android:layout_gravity=" Center_horizontal "android:paddingbottom=" 10dip " android:paddingtop= "10dip" > <progressbar android:id= "@+id/pull_to_refresh_load_progress" style= "@a Ndroid:style/widget.progressbar.small.inverse "android:layout_width=" wrap_content "android:layout_height=" WR Ap_content "android:layout_centerhorizontal=" true "android:layout_centervertical=" true "Android:indet Erminate= "true" android:paddingright= "10DP"/> <textview android:id= "@+id/pull_to_refresh_loadmore_ Text "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:gravity=" Cente R_vertical "Android:layout_torightof= "@+id/pull_to_refresh_load_progress" android:paddingtop= "5dip" android:text= "@string/loading" Android:textappearance= "? Android:attr/textappearancemedium" android:textcolor= "@android: Color/darker_gray" Android:textsize= "14SP" android:textstyle= "bold"/></relativelayout>
Main.xml
<?xml version= "1.0" encoding= "Utf-8"? ><com.example.demo.myswiperefreshlayout xmlns:android= "/http Schemas.android.com/apk/res/android " android:id=" @+id/swipe_layout " android:layout_width=" match_parent " android:layout_height=" match_parent "> <listview android:id=" @+id/listview " android: Layout_width= "Match_parent" android:layout_height= "match_parent" > </listview></ Com.example.demo.myswiperefreshlayout>
Item.xml:
<?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 " android:o rientation= "vertical" > <textview android:id= "@+id/text" android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:gravity= "left" android:padding= "10DP" android: text= "Wo lai le" android:background= "@color/very_light_gray"/> <view android:layout_width= " Match_parent " android:layout_height=" 1DP " android:background=" @android: Color/white "/></ Linearlayout>
Reference links
Http://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html
All rights reserved, reprint must indicate the author (Jintian) and source (original)
Android 5.0 Swiperefreshlayout