Android teaches you to create a unique pull-up, pull-down, refresh and load framework

In fact, as early as last July, my friends in the group let me share this. But I was not very skilled at that time. There are bugs in the code or something. Not written. Now I feel almost finished. Just write it out for everyone to see and discuss and solve problems together.

When it comes to refresh loading, the first thing we think of is swiperefreshlayout, super swipererefreshlayout, xrecyclerview and so on. Anyway, it's much older. I'm still the same as before. No matter what we use, we need to know its principle.

Start building the framework

What was the first thing you thought about the implementation of refresh loading? Use swiperefresh and add a different type at the bottom of recyclerview? Or event interception? It must be event interception. Although swiperefreshlayout is very popular now, it can be seen by many apps. But when you encounter that kind of pit, you have no choice but to use your own company logo. Yes.. Well, I feel that I have offended many companies. Regardless of him, let's continue.

Let's take a look at our renderings first:

Old fellow iron, nothing wrong. Let me introduce how to implement it.

Pull down refresh

First, we give the following parameters, which will be used later:

Since it is refresh, our scrolling must be before the parent view. So we need to write the X and Y values we need to change in the onnestedprescroll method.

We need to use the parent view to intercept it.

We need to judge whether the value of Dy is greater than 0, because greater than 0 is a refresh operation and less than 0 is a load operation. Then we need to determine whether the recyclerview is vertical rather than horizontal.

Pull up loading

As mentioned above, in the onnestedprescoll method, judging dy < 0 is the loading operation. So to sum up, the code becomes like this:

Finally, we need to perform the following operations after the sub view sliding:

In fact, the two main methods have been solved, and the others are nothing. Here, I put out the functions of the eight interfaces of nestedscrolling and the user-defined recyclerview. It has been changed for your reference. I hope everyone can achieve their own refresh and load. Say goodbye to swiperefreshlayout.

Add header and footer

Here, we refer to the add header view and add footer view provided by listview. The code is as follows:

Implementation of several interfaces

Custom recyclerview

Since you wrote your own refresh loading framework, you can't put a recyclerview in the custom layout. How troublesome. Customize one, put it directly in it, and then put a header and footer respectively. There is no need to write a layout every time a page is refreshed. Three layouts solve the refresh and loading of the whole project. Don't say much. The code is as follows:

In this way, we have implemented our own refresh loading framework. I have uploaded the code: swpullrecyclerlayout_ jb51.rar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>