Horizontal and vertical scrolling ListView developed by android (fixed column headers)

Source: Internet
Author: User

To meet project requirements, we need a table that can be horizontally rolled and vertically rolled. In addition, the presentation view of large data volumes (rows) must be considered. After a few days of research, I finally made a demonstration. The textures are as follows:

Okay. Let's look at the ideas:

1. Use listView to scroll up or down directly.

2. HorizontalScrollView is used for left-side scrolling to process scrolling. I write a class MyHScrollView that inherits from it.

2.1. Each row in ListView is divided into two parts: Non-scrolling and scrollable areas. For example, the first column of this demo is static. All the following columns can be rolled.

2.2. I don't want to calculate the rolling distance by myself, because I have to deal with cross-border operations, coordinates, and so on. So I use the OnTouch event to handle it. Therefore, we must understand the OnTouch operating mechanism. Google it.

2.3. The column header (the row in which the column name is displayed) is fixed and does not scroll up or down. But it can scroll left and right. In addition, when it is scrolling between the left and right, all data rows must be rolled along with them. Then we need to listen to the rolling change message (event) of the column header (control) and broadcast the message to all data rows. After receiving the message, the data row adjusts its own scroll bar position to maintain the same rolling distance as the column header.

3. The entire process is basically like this.

3.1. Capture the onTouch event (drag event) of the column header (container control, including the fixed and scrollable control), which is not processed. The control is distributed to the "control of the scroll part in the column header", which is a subclass of HorizontalScrollView. When it receives these drag events, it produces a fixed effect.

3.2. Capture the OnTouch event of the controls in the Data row area (this control is actually the ListView control). If it is not processed, it is also distributed to the "control of the scroll part in the column header ". In these two steps, a small effect is achieved. You can move (scroll) rows in the header and body of the table cell.

3.3. I wrote a subclass of HorizontalScrollView and loaded the onScrollChanged method. This method is executed after scrolling, which is equivalent to "rolling events". I wrote a observer (design pattern) class. After each scroll, the notification is sent to the observer. The observer then notifies its subscriber (those that need to scroll simultaneously in the row ).

3.4. When the ListView creates a row, make these rows subscribe to the observer in the previous step. After receiving the message, adjust the position of the scroll bar to be consistent with that of the column header.

If there are too many codes, you will not post them. Provides code download.

Download the source code.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.