| First, the problem description |
In Android development, the most used data Refresh method is the drop-down refresh, and to complete this function we use most of the third-party open Source Library Pulltorefresh. Today, Google is also tempted to launch its own drop-down component swiperefreshlayout, below we through the API documentation and source code to analyze how to use the Swiperefreshlayout.
Look first:
| Second, the specific use of swiperefreshlayout |
Let's look at the specific usage of swiperefreshlayout, as the name implies, this component is a layout, but note that there can only be one direct child view within this layout. In fact, through the documentation we can know that swiperefreshlayout just inherited viewgroup.
Viewing the document, we can know that there is an interface in the Swiprefreshlayout, through which we can listen to the swipe gesture, in fact, the most important step of using this component is to implement the Onrefresh method of this interface, in this method to implement the data update operation. As follows:
Method in the interface:
In addition to the Onrefreshlistener interface, there are some other important methods in Swiprefreshlayout, as follows:
2, Setprogressbackgroundcolor (int colorres): Sets the background color of the progress circle.
3, setcolorschemeresources (int ... colorresids): Sets the color of the progress animation.
4, setrefreshing (Boolean refreshing): Sets the brushing state of the component.
5, setSize (int size): Sets the size of the progress circle, only two values: default, LARGE
Figure out the API, we do the actual code below, first of all to do the layout, the specific content is as follows:
<?XML version= "1.0" encoding= "Utf-8"?><Android.support.v4.widget.SwipeRefreshLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:id= "@+id/swipelayout" > <ListViewAndroid:id= "@+id/mylist"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"/> </Android.support.v4.widget.SwipeRefreshLayout>
The activity core code is as follows:
Swiperefreshlayout =(swiperefreshlayout) Findviewbyid (r.id.swipelayout); Swiperefreshlayout.setcolorschemeresources (r.color.swipe_color_1, r.color.swipe_color_2, R.c Olor.swipe_color_3, r.color.swipe_color_4); Swiperefreshlayout.setsize (Swiperefreshlayout.large); Swiperefreshlayout.setprogressbackgroundcolor (R.color.swipe_background_color); //swiperefreshlayout.setpadding (20, 20, 20, 20); //Swiperefreshlayout.setprogressviewoffset (True, 100, 200); //Swiperefreshlayout.setdistancetotriggersync (+);Swiperefreshlayout.setprogressviewendtarget (true, 100); Swiperefreshlayout.setonrefreshlistener (NewOnrefreshlistener () {@Override Public voidOnrefresh () {NewThread (NewRunnable () {@Override Public voidrun () {data.clear (); for(inti=0;i<20;i++) {Data.add ("Swiperefreshlayout drop-down refresh" +i); } Try{Thread.Sleep (5000); } Catch(interruptedexception e) {e.printstacktrace (); } mhandler.sendemptymessage (1); }}). Start (); } }); //Handler PrivateHandler Mhandler =NewHandler () {@Override Public voidhandlemessage (Message msg) {Super. Handlemessage (msg); Switch(msg.what) { Case1: Swiperefreshlayout.setrefreshing (false); Adapter.notifydatasetchanged (); //swiperefreshlayout.setenabled (false); Break; default: Break; } } };
With the above steps, we have implemented a simple pull-down refresh operation, and on this basis, we can analyze how swiperefreshlayout is implemented.
Through the source we find two important attributes in Swiperefreshlayout:
Private Materialprogressdrawable mprogress;
Private Circleimageview Mcircleview;
These two properties are used to animate the progress, and in method Createprogressview, we see Mcircleview finally added to the swiperefreshlayout.
Private void Createprogressview () { new Circleimageview (GetContext (), Circle_bg_light, CIRCLE_DIAMETER/2 ); New This ); Mprogress.setbackgroundcolor (circle_bg_light); Mcircleview.setimagedrawable (mprogress); Mcircleview.setvisibility (view.gone); AddView (Mcircleview); }
At the same time we can also see Cirlceimageview inherited Imageview,materialprogressdrawabel inherited drawable, so we also understand the progress of the animation is how to achieve, the specific details do not do too much to repeat, You can view the source code yourself.
Class Circleimageview extends ImageView
Class Materialprogressdrawable extends drawable implements Animatable
The implementation of the specific pull-down function is mainly done in the Onintercepttouchevent, Ontouchevent method, where I do not paste the specific code out, you can see for yourself.
For students who want to know more, you can download the complete project and run it yourself! If you have a question, you can discuss it.
Android official drop-down refresh component Swiperefreshlayout