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?
A few days ago, a heated debate broke out in the ...
WeChat Mini Program is an application that users ...
Community is a good product, but it is also a com...
Last year, Microsoft launched the Windows Hello fe...
Self and Social Intelligence ——A person has many ...
According to news today, the "foreign restri...
For a long time, doubts about the Apollo manned m...
This article was reviewed by Chu Yuhao, PhD from ...
June 23, early this morning, Apple held the WWDC2...
With the fading of traffic dividends and the cont...
If I were to describe the Samsung Note Edge in tw...
Affected by the epidemic, many companies have tra...
Although the Earth is a sphere, its surface is ma...
How fat is healthy? ——A well-proportioned body is...
In April 2022, the central bank issued an announce...