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

How to make Tik Tok videos popular? Tips and methods for shooting Tik Tok videos

Many friends have encountered this situation: the...

Imitation of Meituan city selection interface

Source code introduction: MeiTuanLocateCity imita...

How to write an operational activity planning and promotion plan?

The article draws on the event operation in Teach...

The Economist: Why did Terry Gou buy Sharp at a high price?

The Economist magazine website recently published...

Why should we protect rare species?

A penguin wanders around the empty parking lot of...

2019 Tik Tok promotion and operation strategy!

20% of the videos account for 80% of the views, b...

5 aspects to understand about short video operations!

Where there are users, there is a market; where t...

How to plan a marketing campaign that can create multiple values?

The World Cup craze has just passed, and problems...