Android listview implements the functions of pull-up loading, pull-down refresh and sliding deletion

Recently, the project needs a listview that can be deleted by sliding and with pull-up loading and pull-down refresh. After consulting some materials, most of them add the head and bottom views on the basis of swipemenulistview to expand the functions of pull-up loading and pull-down refresh, However, you need to manually draw the UI and handle some animation effects. It is not particularly convenient to use. Just when the pulltorefreshlibrary library is used in the project, you try to extend a pulltorefreshswipemenulistview class to meet the requirements. Let's take a look at the effects first:

Implementation steps

1、 Combining pulltorefresh with swipemenulistview

The pulltorefreshlibrary library contains many kinds of controls that can be used to pull up and load drop-down refresh, such as pulltorefreshlistview, pulltorefreshgridview and pulltorefreshscrollview. Their implementation methods are similar. In essence, the parent layout is added outside the control, and the head and bottom views of the control are added in the parent layout to realize the function of pull-up and load drop-down refresh, So it's very extensible. Copy a pulltorefreshlistview and deal with it

Find this part of the code and simply make the following modifications:

Why copy a pulltorefreshlistview instead of others? Because swipemenulistview inherits from listview (you can find it by name), it is more convenient to modify. Why modify this part of the code? Look down. Find a reference to this part of the code:

Is a method that inherits the implementation of the parent class. Open the method of the parent class:

Take a look at the comments, which roughly means that if you need a custom pull-up load drop-down refresh view, you can return the custom view. And the return value is t, a generic type, which is very clear. Take a look at the reference of this method, mrrefreshableview = createrefreshableview (context, attrs); From the naming, you can see that the final return of this method is the view that needs the pull-up load pull-down refresh function

I think it's almost OK. Let's try it and see the effect:

It seems that the effect is not very ideal. There are many problems. For example, you can pull up and down when sliding deletion, and there is no bottom view when pulling up. Next, we will solve these problems

2、 Solving sliding conflicts and related problems

Let's first solve the problem of why there is no bottom view. Take a look at the pulltorefreshswipemenullistview class and pulltorefreshlistview class modified by copy, and compare the difference, so that pulltorefreshswipemenullistview has no bottom view, and only one part is modified in pulltorefreshswipemenullistview, This is to replace the previously returned internallistview with swipemenullistview. Obviously, the problem occurs in the internallistview. Check the internallistview (it's easy to find. It's an internal class). You can see this part of the code:

The bottom view is added when the internallistview calls the setadapter method. What about the head view? Find the following code:

It is found that the header is added during initialization, but the bottom is not added (the part of... In the code). The internallistview adds the bottom in the setadapter method, and there is no relevant processing in the swipemenulistview, so add the code mreshableview. Addfooterview (mlvfooterloadingframe, false) at; Add the bottom to solve the problem that there is no bottom found before (of course, you can modify the swipemenulistview following the internallistview, but it is troublesome, so we won't do it here)

Next, we'll deal with the problem that pull-up and drop-down operations can be performed during sliding deletion, which is obviously a problem of sliding conflict. Think about it. When sliding deletion, we don't want to pull-up and drop-down operations. Pulltorefreshswipemenulistview itself is a ViewGroup, so when sliding deletion, we can mask the sliding events of the parent layout and open swipemenulistview, In the sliding event, do the following:

It's very simple. I won't explain more. For children's shoes distributed by clicking events, you can check the materials. In this way, a listview with pull-up loading, pull-down refreshing and sliding deletion is basically completed

3、 Complete code

PullToRefreshSwipeMenuListView:

SwipeMenuListView:

MainActivity:

activity_ main:

item_ adapter:

This article has been sorted into "Android pull-down refresh pull-up loading effect", welcome to study and study.

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
分享
二维码
< <上一篇
下一篇>>