Android's official pull-down refresh component SwipeRefreshLayout

Android's official pull-down refresh component SwipeRefreshLayout

1. Problem Description

In Android development, the most commonly used data refresh method is pull-to-refresh, and the third-party open source library PullToRefresh is the most commonly used to complete this function. Nowadays, Google can't help but launch its own pull-down component SwipeRefreshLayout. Let's analyze and learn how to use SwipeRefreshLayout through API documents and source code.

First look at the effect diagram:

2. Specific usage of SwipeRefreshLayout

Next, let's look at the specific usage of SwipeRefreshLayout. As the name implies, this component is a layout, but it should be noted that there can only be one direct child View in this layout. In fact, through the documentation, we can know that SwipeRefreshLayout is just an inheritance of ViewGroup.

Looking at the document, we can know that there is an interface in SwipRefreshLayout, through which we can listen to sliding gestures. In fact, the most important step in using this component is to implement the onRefresh method of this interface, and implement the data update operation in this method. As follows:

Methods in the interface:

1. setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener): Set the gesture sliding listener.

2. setProgressBackgroundColor(int colorRes): Set the background color of the progress circle.

3. setColorSchemeResources(int... colorResIds): sets the color of the progress animation.

4. setRefreshing(Boolean refreshing): sets the refreshing state of the component.

5. setSize(int size): Set the size of the progress circle. There are only two values: DEFAULT and LARGE

After figuring out the API, let's do the actual coding. First, make the layout. The specific contents are as follows:

  1. <?xml version= "1.0" encoding= "utf-8" ?>
  2. <android.support.v4.widget.SwipeRefreshLayout
  3. xmlns:android= "http://schemas.android.com/apk/res/android"  
  4. android:layout_width= "match_parent"  
  5. android:layout_height= "match_parent"  
  6. android:orientation= "vertical"  
  7. android:id= "@+id/swipeLayout" >
  8.       
  9. <ListView
  10. android:id= "@+id/mylist"  
  11. android:layout_width= "match_parent"  
  12. android:layout_height= "wrap_content" />
  13.      
  14. </android.support.v4.widget.SwipeRefreshLayout>

#p#

The core code of Activity is as follows:

  1. swipeRefreshLayout = (SwipeRefreshLayout)findViewById(R.id.swipeLayout);
  2.  
  3. swipeRefreshLayout.setColorSchemeResources(R.color.swipe_color_1,
  4. R.color.swipe_color_2,
  5. R.color.swipe_color_3,
  6. R.color.swipe_color_4);
  7. swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);;
  8. swipeRefreshLayout.setProgressBackgroundColor(R.color.swipe_background_color);
  9. //swipeRefreshLayout.setPadding(20, 20, 20, 20);  
  10. //swipeRefreshLayout.setProgressViewOffset(true, 100, 200);  
  11. //swipeRefreshLayout.setDistanceToTriggerSync(50);  
  12. swipeRefreshLayout.setProgressViewEndTarget( true , 100 );
  13. swipeRefreshLayout.setOnRefreshListener( new OnRefreshListener() {
  14. @Override  
  15. public   void onRefresh() {
  16. new Thread( new Runnable() {
  17. @Override  
  18. public   void run() {
  19. data.clear();
  20. for ( int i = 0 ; i < 20 ; i++) {
  21. data.add( "SwipeRefreshLayout pull down to refresh" +i);
  22. }
  23. try {
  24. Thread.sleep( 5000 );
  25. } catch (InterruptedException e) {
  26. e.printStackTrace();
  27. }
  28. mHandler.sendEmptyMessage( 1 );
  29. }
  30. }).start();
  31. }
  32. });
  33. //handler  
  34. private Handler mHandler = new Handler(){
  35. @Override  
  36. public   void handleMessage(Message msg) {
  37. super .handleMessage(msg);
  38. switch (msg.what) {
  39. case   1 :
  40.                  
  41. swipeRefreshLayout.setRefreshing( false );
  42. adapter.notifyDataSetChanged();
  43. //swipeRefreshLayout.setEnabled(false);  
  44. break ;
  45. default :
  46. break ;
  47. }
  48. }
  49. };

Through the above steps, we have implemented a simple pull-down refresh operation. On this basis, we can analyze and study how SwipeRefreshLayout is implemented.

Through the source code, we found two important properties in SwipeRefreshLayout:

private MaterialProgressDrawable mProgress;

private CircleImageView mCircleView;
These two properties are used to achieve the progress animation effect. In the createProgressView method, we see that mCircleView is finally added to SwipeRefreshLayout.

private void createProgressView() {
mCircleView = new CircleImageView(getContext(), CIRCLE_BG_LIGHT, CIRCLE_DIAMETER/2);
mProgress = new MaterialProgressDrawable(getContext(), this);
mProgress.setBackgroundColor(CIRCLE_BG_LIGHT);
mCircleView.setImageDrawable(mProgress);
mCircleView.setVisibility(View.GONE);
addView(mCircleView);
}

At the same time, we can also see that CirlceImageView inherits ImageView, and MaterialProgressDrawabel inherits Drawable. So far, we have understood how the progress animation is implemented. The specific details are not repeated in detail. You can check the source code by yourself

lass CircleImageView extends ImageView

class MaterialProgressDrawable extends Drawable implements Animatable

The specific drop-down function is mainly implemented in the onInterceptTouchEvent and onTouchEvent methods. I will not post the specific code here, you can check it yourself.

<<:  Cherish entrepreneurship and stay away from entrepreneurial streets

>>:  Why did eBay return to China together with JD.com?

Recommend

The core operations and optimization ideas of bidding promotion!

Bidding promotion is the most mainstream and comm...

Who is the king of volcanoes in China?

January 15, 2022 Tonga, a South Pacific island na...

How to do growth activities without spending money?

In the previous article, we shared the growth act...

PR secrets: a comprehensive guide to advanced editing

PR Secret Techniques: A Comprehensive Guide to Ad...

How much does it cost to develop a small moving program in Baise?

The launch of mini programs has brought convenien...

The truth behind two against ten: OV are not strong, and Xiaomi is not weak?

Recently, two anthropomorphic pictures circulated...

Can pushing Feiliu to go public revive NetQin’s stock price?

Because its stock price is undervalued, Qihoo 360...

How to use funnel analysis correctly to improve conversion?

Simple funnel tools can only tell you at which st...