This article illustrates how Android obtains the current view's coordinates in the screen. Share to everyone for your reference. Specifically as follows:
Final int[] location = new Int[2];
View.getlocationonscreen (location);
This allows you to get the X,y value of the view in the global coordinate system (note that this value is to be counted from the top of the screen, which means that the height of the notification bar is included)
Gets the absolute coordinate
location[0] x coordinate
location[1 y coordinate within the current screen
Applications that we can use to record the last time ListView scroll up there
First we need a global variable that records the current scrolling position:
Copy Code code as follows:
Private float oldlisty =-1;
Then get oldlisty in the Onitemclick () or Onitemlongclick () event of ListView:
Lstview.setonitemclicklistener (New Onitemclicklistener ()
{public
void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3)
{
int pos[] = {1,-1};///////////////////////////////////
The position of the selected Item in the screen, with the upper-left corner as the origin (0, 0)
oldlisty = (float) pos[1];//We'll just take the Y-coordinate.
}
);
The last thing to do is to restore the previous position after Setadapter ():
...
Lstview.setadapter (adapter); Rebind adapter
lstview.setselectionfromtop (index, (int) oldlisty);//Resume the position just now
I hope this article will help you with your Android program.