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

Lily SEO Training: What are the aspects of search engine friendliness?

First: Directory Structure A clear and concise di...

How to do it on Xianyu when there is no source of goods? Read this article

Today’s content is mainly focused on the basics -...

The Living Room Conspiracy Behind Mango TV’s “WeChat Payment”

As competition in the Internet TV industry contin...

Are they all cactus fruits? Why do dragon fruits come in so many colors?

Audit expert: Shen Tingting PhD in Botany Dragon ...

Why is the image quality on Android not as high as on iOS?

I often see people asking: "Why are the pict...

Promotion and operation rules of the SMS industry in 2015

Introduction to the operating rules of the SMS in...

His and Her Feast, Traveling Through 3,400 Years

Produced by: Science Popularization China Author:...

Private domain operation and drainage methods

Many people think that attracting traffic by bait...

Can you eat sweet potato vermicelli mixed with cassava flour?

Audit expert: Wang Kang Director of the National ...

The 5th China (Shenyang) International Mobile Phone Expo is in full swing

The 2014 China (Shenyang) International Mobile Ph...

A collection of 13 essential tools for new media operations!

Thirteen categories of operation auxiliary tools ...

The whole network is taking on the challenge? Urgent reminder!

The girls in high buns and green clothes walked s...