In the development of Android, you often encounter the need to proactively set a ListItem location. The function that sets the position has
listview.setselection (int position)
Listview.setselectionfromtop (int position, int y);
which
Position refers to the index of the specified item in the ListView, noting that if a header exists, the index is counted from the header.
Y refers to the distance from the topmost edge to the visible range of the ListView.
Functions are now set according to their own needs.
The requirements for this encounter, the ListView requirements are shown from the bottom up, and when the cursor is updated, keep the original top item (excluding header) unchanged, and the new historical data will continue to show up above the original item.
The ListView is displayed from the bottom up, i.e.
[Java]View Plaincopy
- android:stackfrombottom="true"
However, the setting of this property does not affect the sort order of the index, that is, the index of item is incremented from top to bottom, and does not become incremented from the top down. Item with index 0 is the item (or header) at the top of the ListView.
Then when the cursor is updated, the first index of the original is changed. To keep it in place (R in the figure). The steps are as follows:
(1) Get the position of this piece in the new cursor (posiition)
(2) Gets the position of this bar in the ListView after replacing the cursor.
(4) Due to the scrollable properties of the ListView, we need to record the index of the first item that is visible before the cursor is replaced (listview.getfirstvisibleposition ())
(3) The case of distinguishing firstvisibleposition is 0 and greater than 0. The header, which is the loading in the figure, disappears after the new data comes out.
(4) When firstvisibleposition is 0 o'clock the actual point is the header, we want to keep the position unchanged is the position of the first (R) under the header. Then set Firstvisibleposition to 1 at this time
(5) When Firstvisibleposition is more than 0 o'clock, the actual point is item, but we need to set Firstvisibleposition to 0. *
(6) We obtain the corresponding item view according to Firstvisibleposition with the listview.getchildat (int position) function, and then according to the View.gettop () The function gets the distance to the top of the ListView Y.
So the two parameters required for listview.setselectionfromtop (int position, int y) position and y are all available.
* Note: listview.getchildat (int position), this position refers to the index in the visible item, which is very different from the position in the cursor. You can see that the Listview.getchildcount () function Gets the number that is less than or equal to the number in the cursor (regardless of the header). Although there may be 20 data, but the interface can only see 8, then the ChildCount is about 8. On the other hand, Firstvisibleposition takes out the index in the total number of bars and then considers the missing header, so the firstvisibleposition is 0 to set to 1, which is greater than 0 o'clock and set to 0.
The following code:
Code to invoke:
[Java]View Plaincopy
- int headercount = Mlistcontainer.getlistview (). Getheaderviewscount ();
- int firstvisiblepos = Mlistcontainer.getlistview (). Getfirstvisibleposition ();
- int newcursorposition = Getpositioninnewcursor (Cursor.getcount (), firstvisiblepos);
- int OffsetY = getoffsety (cursor, firstvisiblepos, newcursorposition);
- Madapter.changecursor (cursor);
- Muprefreshlayout.setvisibility (View.gone);
- Mlistcontainer.getlistview (). Setselectionfromtop (Newcursorposition + headercount, OffsetY);
Getpositioninnewcursor function:
[Java]View Plaincopy
- Private int getpositioninnewcursor (int newcursorcount, int firstvisiblepos) {
- if (firstvisiblepos = = 0) {
- Firstvisiblepos + = 1;
- }
- int headercount = Mlistcontainer.getlistview (). Getheaderviewscount ();
- int newcursorpos = Newcursorcount-madapter.getcount () + Firstvisiblepos-headercount;
- return newcursorpos;
- }
Getoffsety function:
[Java]View Plaincopy
- Private int getoffsety (cursor cursor, int firstvisiblepos, int newcursorposition) {
- int y;
- View Firstvisibleitem = null;
- if (firstvisiblepos = = 0) {
- Firstvisibleitem = Mlistcontainer.getlistview (). Getchildat (1);
- }else{
- Firstvisibleitem = Mlistcontainer.getlistview (). Getchildat (0);
- }
- y = Firstvisibleitem.gettop ();
- View Timeview = Firstvisibleitem.findviewbyid (R.id.time_text_view);
- if (timeview! = null && timeview.getvisibility () = = view.visible) {
- Cursor CurItem = (cursor) madapter.getitem (newcursorposition);
- Cursor Preitem = (cursor) madapter.getitem (newcursorposition- 1);
- if (CurItem! = Null | | Preitem! = null) {
- Long Curtimestamp = Curitem.getlong (MESSAGESPROJECTION.JEDI_CREATE_DATE_INDX);
- Long Pretimestamp = Preitem.getlong (MESSAGESPROJECTION.JEDI_CREATE_DATE_INDX);
- if (Math.Abs (curtimestamp-pretimestamp) <= show_time_stamp_ten_mins) {
- Layoutparams param = (linearlayout.layoutparams) mtimeview.getlayoutparams ();
- Y + = Mtimeview.getheight () + Param.topmargin + param.bottommargin;
- }
- }
- }
- return y;
- }
There is a section in the getoffsety to calculate the height of the timestamp of the code, do not care to skip it yourself. Because querying the historical data may cause the original timestamp to no longer show the timestamp after the refresh (merged with the previous one into a period of time), so the height is also counted in.
- An View.measure method in Android
- The next Java syntax if ((Boolean) NULL) will report a null pointer exception java.lang.NullPointerException
-
Top
-
3
-
Step
How the Android get started in the ListView for precise positioning