Detailed analysis of Android swipemenulistview framework

At the weekend, I specially sorted out the knowledge materials of Android swipemenuslistview. The following is the sorting content:

Swipemenulistview

A swipe menu for ListView.-- A very good sliding menu open source project.

Demo

1、 Introduction

I've seen the custom view and event distribution for a long time. I want to find a project to practice.. Just confirm what you have learned.

I found this project on GitHub: swipemenulistview, which is really good. It is very enlightening for event distribution and custom view. Although there are some minor defects, it will be explained later. Students who want to know how to realize the sliding menu, this article is absolutely helpful to you. It analyzes each file in detail from the macro and micro perspective.

Project address: https://github.com/baoyongzhang/SwipeMenuListView/tree/b00e0fe8c2b8d6f08607bfe2ab390c7cee8df274 Version: b00e0fe its use is very simple. It only needs three steps. You can understand it on GitHub without occupying space. This paper only analyzes the principle. In addition, if you look at the code and feel different from me, and it's difficult, you can see what I Annotated: http://download.csdn.net/detail/jycboy/9667699

Look at two pictures first: have a general understanding

1. The following figure shows the view hierarchy:

In the above figure: swipemenulayout is the layout of items in listview, which is divided into left and right parts. One part is the normally displayed contentview and the other part is the sliding menuview; The swipemenuview that slides out inherits from the LinearLayout. When adding a view, it is added horizontally, and multiple views can be added horizontally.

2. The following figure is the class diagram structure:

The above is the calling relationship between classes, and the main functions of classes are indicated next to classes.

2、 Source code analysis

Swipemenu and swipemenuitem are entity classes that define properties and setter and getter methods. Just take a look. Basically, the comments of the source code are very clear.

2.1 swipemenuview: the comments in the code are very clear

**Swipemenuview is the view displayed during sliding. See its constructor swipemenuview (swipemenumenu, swipemenulistview listview); Traverse items: menu getMenuItems(); Call the addItem method to add an item to the swipemenuview.

In the addItem method: each item is a LinearLayout.

2.2 SwipeMenuLayout:

The code of this class is a little long. Let's divide it into three parts, paste only the core code, and take a look at the rest.

As can be seen from the init method above, swipemenulayout consists of two parts: the user's item view and menu view. The operation of sliding the finger is completed through the simpleongesturelistener.

The main methods above are onswape and swipe. The main logic is: onswape is an API exposed to external calls,

OnSwipe is called in the onTouchEvent event handling method of SwipeMenuListView. The swipe is to slide both mcontentview and mmenunview by DIS distance.

The onmeasure and onlayout methods above are frequently rewritten in the user-defined view. In onmeasure, the size of the view is measured. Here, the width type is set to unspecified, which can be expanded infinitely. Onlayout is where the view is placed in the parent layout after the size of the view is measured. The code can see that the menuview is hidden on the left (or right) according to the sliding direction.

2.3 SwipeMenuAdapter

2.4 core class: swipemenulistview,

This code is very long. You need to be patient when looking at it.

The most important logic in this class is about the judgment and distribution of events, when to intercept events, and what operations different events correspond to. If you are not clear about the distribution of events, you can find relevant blogs on the Internet or see my follow-up blog. It should be these two days.

Here we analyze the event distribution logic of swipemenulistview: the core is the processing of item click events and sliding events in swipemenulistview. When sliding, swipemenulistview intercepts events and handles them by itself. Remember this logic and you can see it at a glance from the code. The following is an event distribution flow chart I drew:

A touch event is a sequence of events: action_ DOWN->ACTION_ MOVE->.... ACTION_ MOVE->ACTION_ UP. With action_ Start with down and start with action_ End of up.

Below is my printing process: (add log to the code)

3、 Existing problems

1. If you run the next framework, you will find a problem:

When an item in the listview has slid away, it is assumed to be Item1; At this time, slide another item and call it Item2;

In this case, Item1 will not close and Item2 will not open.

This effect is not good. I have modified this problem in the code. I have indicated the specific code.

2. This is the following code: the action of ontouchevent (motionevent EV) in swipemenullistview_ In down, this code will never be executed, because ontouchevent and onintercepttouchevent correspond to a motionevent.

Mtouchposition = = oldpos is always equal.

I have modified this problem in the code. It has been submitted to the original author on GitHub.

Thank you for reading, hope to help you, thank you for your support to this site!

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