Android uses pulltorefresh to complete listview drop-down refresh and left slide delete functions

The refresh brush function under listview is believed to be familiar to ape friends engaged in Android development, including the implementation effect of Google's own son swiperefreshlayout, which can also be seen on some apps (but I don't like the official refresh effect). This article will lead some new Android friends or friends who love to share together to simply realize the drop-down refresh and left sliding deletion effects of listview.

1、 The main contents of this paper are as follows:

Use pulltorefresh to complete listview pull-down and pull-up refresh; Expand pulltorefresh to perfectly realize the left sliding deletion effect of listview; Note: pulltorefresh in this article is not a complete open source library. I have deleted some unnecessary and unrelated classes. It looks more streamlined and easier to understand. Attach pulltorefresh source code library download address: http://download.csdn.net/detail/jaynm/9670737

2、 First look at the effect:

1. Listview pull-down refresh and pull-up load more:

2. Listview pull-down refresh, pull-up load more, left slide delete:

3、 Implementation code:

1. Implement listview drop-down refresh:

As for the pulltorrefreshbase class, I have modified the source code. If the code is too long, it will not be posted here. I can download the demo and read it carefully, mainly to see how to apply it to my project:

Are the above operations simple enough to complete listview pull-down refresh and pull-up load more.

The XML layout file is also very simple. You only need to reference the address of pulltorrefreshlistview:

In this way, we have completed the pull-down refresh and pull-up loading of a listview list. Personally, I think the pulltorefresh library is still very powerful.

1. Implement listview drop-down refresh, left slide delete:

be careful:

a. Here, override listview to generate swipemenulistview, so it is still a listview list control;

b. Since you need to slide left, you must judge the gesture sliding operation in the ontouchevent () method;

c. The conflicts of pull-down, pull-up and left sliding events need to be considered;

d. You need to consider the left sliding deletion event. Only one item can be deleted at a time;

mOnSwipeListener. onSwipeStart(mTouchPosition); Used to record the current gesture state as motionevent ACTION_ Mark when move starts sliding to the left, monswipelistener onSwipeEnd(mTouchPosition); Used to record the current gesture state as motionevent ACTION_ The mark at the end of up sliding to the left is mainly used to obtain the current operation status during the callback of the activity interface. In this case, we can solve the third problem above according to the two callback methods of onswapestart() and onswapeend() (we need to consider the conflict of pull-down, pull-up and left sliding events)

refreshlistview. setPullRefreshEnabled(false); The method is the event I defined in pulltorrefreshbase to support pull-down refresh operation. We can set it according to onswapestart() and onswapeend() methods.

In this way, we have perfectly solved the above three points of attention, so it is easy to realize the left sliding deletion of listview.

Let's take the above precautions D, which requires some effort on the event distribution mechanism: when we slide the delete button of Itema to the left and slide itemb again, we can't let it also appear. We have to close the delete state of Itema first, which is a reasonable operation. Therefore, we deal with the intercepted event in the method:

OK, the above is all the event handling codes in the swipemenulistview class. Next, we can reference the swipemenulistview defined in the activity to realize the effects of listview drop-down refresh, pull-up loading and left sliding deletion.

4、 Introduction to some common attributes:

Pull to refresh can also define some attributes in XML:

Ptrrefreshableviewbackground sets the background color of the entire mpullrefreshlistview

Ptrheaderbackground sets the background color of the drop-down header or the pull-up footer

Ptrheadertextcolor is used to set the color of text in header and footer

Ptrheadersubtextcolor is used to set the color of the last refresh time in header and footer

If ptrshowindicator is true, icons will appear in mpullrefreshlistview, in the upper right corner and lower right corner, which is very interesting.

Ptrheadertextappearance and ptrsubheadertextappearance respectively set the font type and color in the pull-up header or pull-up footer

Ptrrotatedrawablewhilepulling when the animation is set to rotate, the drop-down is whether to rotate.

Summary: in fact, it is not difficult to refresh the listview. We may have often seen such components before: xlistview. This component should be seen by many people when they first learn Android. This is the listview drop-down refresh defined and written by many people. There is no problem to realize the function, but I always think the effect experience is too poor. Pulltorefresh drop-down refresh has been used in the project. Well, that's all for today's sharing. You can leave a message to discuss, learn and communicate where you don't understand! Share your own it resource library QQ group: 459756676 is mainly to help beginners in the IT industry share video learning materials. As long as you are an it lover, you can enter the common learning!

The above is what Xiaobian introduced to you. Android uses pulltorefresh to complete the listview drop-down refresh and left slide delete functions. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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