As the project requires, we need a table that can be scrolled horizontally and vertically. But also consider the display view of the large amount of data (rows). After a few days of research finally finished, made a demo. The map is as follows:
All right. Let us see what the idea is:
1. Scroll up and down directly using ListView to achieve.
2. Scroll around using Horizontalscrollview to handle scrolling. I write a class Myhscrollview inherits from it.
2.1. Each row (row) in the ListView is divided into two parts, a scrollable and scrollable area. For example, the first column of this demo, is static. All of the columns that follow can be scrolled.
2.2. I do not want to calculate the distance of scrolling, because I have to deal with the bounds, coordinates and so on. So I used the Ontouch event to handle it. So we have to understand the operating mechanism of Ontouch. Learn to ask Google it.
2.3. The column header (the line that displays the column name) is fixed and does not scroll up and down. But you can scroll around. And when it scrolls around, all rows of data (row) are scrolled around with it. Then we need to listen for a scrolling change message (event) for the column header (control) and broadcast the message to all data rows. After they receive the message, the data rows adjust their scroll bar positions to maintain the same scrolling distance from the column headers.
3. So the whole process is basically like this.
3.1, the Ontouch event (the drag event) that captures the column header (the container control, which contains the fixed and scrollable controls) is not processed. The control that is distributed to the scrollable part of the column head, which is a horizontalscrollview subclass that has a fixed effect when it receives these drag events
3.2. The Ontouch event that captures the data row area, which is actually the ListView control, is not processed, and is also distributed to the scrollable part of the column head control. In both steps, a small effect is achieved by clicking the table's head and body to move (scroll) rows.
3.3. I wrote a subclass of Horizontalscrollview, overloading the Onscrollchanged method, which executes after scrolling, equivalent to "scrolling event", and I wrote a class for the Observer (design pattern). Each time you scroll, you are notified to the observer. The observer then notifies its subscribers (those scrolling controls in rows that need to be scrolled at the same time).
3.4. When ListView creates a row, the rows are subscribed to by the observer at the previous step. When the message is received, adjust its scroll bar position to keep the scroll bar position consistent with the column header.
More code, no paste. Provides code downloads.
Source code download.