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:
#p# The core code of Activity is as follows:
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; private void createProgressView() { 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?
First: Directory Structure A clear and concise di...
Today’s content is mainly focused on the basics -...
As competition in the Internet TV industry contin...
Audit expert: Shen Tingting PhD in Botany Dragon ...
I often see people asking: "Why are the pict...
Introduction to the operating rules of the SMS in...
Produced by: Science Popularization China Author:...
The ten pounds lost in the summer have not been l...
Ancient and famous trees are called "green n...
Many people think that attracting traffic by bait...
Audit expert: Wang Kang Director of the National ...
Although the problem of 400 telephone numbers bei...
The 2014 China (Shenyang) International Mobile Ph...
Thirteen categories of operation auxiliary tools ...
The girls in high buns and green clothes walked s...