The location in the screen where the current focus is captured in Android View.getlocationonscreen

Source: Internet
Author: User

View view = Getcurrentfocus ();//Gets the view where the current focus is.

Java code

    1. Final int[] location = new int[2];
    2. View.getlocationonscreen (location);
[Java]View Plaincopy
    1. Final int[] location = new int[2];
    2. View.getlocationonscreen (location);


This will give you the X, Y value of the view in the global coordinate system (note that this value is calculated from the top of the screen, that is, the height of the notification bar is included)//Gets the absolute coordinates in the current screen

Java code
    1. location[0] x coordinate
    2. location[1] y-coordinate
[Java]View Plaincopy
    1. location[0] x coordinate
    2. location[1] y-coordinate


Application that we can use to record the last time the ListView was scrolled there

First we need a global variable that records the current scrolling position:

Java code
    1. Private Float oldlisty =-1;
[Java]View Plaincopy
    1. Private Float oldlisty =-1;


Then get oldlisty in the Onitemclick () or Onitemlongclick () event of the ListView:


Java code
  1. Lstview.setonitemclicklistener (new Onitemclicklistener ()
  2. {
  3. public void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3)
  4. {
  5. int pos[] = {-1,-1}; //Save an array of current coordinates
  6. Arg1.getlocationonscreen (Pos); //Gets the position of the selected Item in the screen, with the upper-left corner as the origin (0, 0) .
  7. Oldlisty = (float) pos[1]; //We'll just take the Y-coordinate.
  8. }
  9. });
[Java]View Plaincopy
  1. Lstview.setonitemclicklistener (new Onitemclicklistener ()
  2. {
  3. public void Onitemclick (adapterview<?> arg0, View arg1, int arg2, long arg3)
  4. {
  5. int pos[] = {-1,-1}; //Save an array of current coordinates
  6. Arg1.getlocationonscreen (Pos); //Gets the position of the selected Item in the screen, with the upper-left corner as the origin (0, 0) .
  7. Oldlisty = (float) pos[1]; //We'll just take the Y-coordinate.
  8. }
  9. });


The last thing to do is to restore the previous position after Setadapter ():

Java code
    1. ...
    2. Lstview.setadapter (adapter); //rebind adapter
    3. Lstview.setselectionfromtop (index, (int) oldlisty); //Restore the location just now

The location in the screen where the current focus is captured in Android View.getlocationonscreen

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.