Android gets the current view in the screen coordinates method _android

Source: Internet
Author: User

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.

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.