- final int[] location = new int[2];
- view.getLocationOnScreen(location);
- final int[] location = new int[2];
- view.getLocationOnScreen(location);
这样就能够获得该视图在全局坐标系中的x,y值,(注意这个值是要从屏幕顶端算起,也就是索包括了通知栏的高度)//获取在当前屏幕内的绝对坐标 java
- location[0] x坐标
- location[1] y坐标
- location[0] x坐标
- location[1] y坐标
应用 ,咱们能够用来记录上一次listview滚动到了那里
首先咱们须要一个记录当前滚动位置的全局变量: 数组
- private float OldListY = -1;
- private float OldListY = -1;
而后在 listView 的 onItemClick() 或 onItemLongClick() 事件中获取 OldListY:
spa
- lstView.setOnItemClickListener(new OnItemClickListener()
- {
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
- {
- int Pos[] = { -1, -1 };
- arg1.getLocationOnScreen(Pos);
- OldListY = (float) Pos[1];
- }
- });
- lstView.setOnItemClickListener(new OnItemClickListener()
- {
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
- {
- int Pos[] = { -1, -1 };
- arg1.getLocationOnScreen(Pos);
- OldListY = (float) Pos[1];
- }
- });
最后要作的就是在 setAdapter() 后恢复先前的位置: .net
- ...
- lstView.setAdapter(adapter);
- lstView.setSelectionFromTop(index, (int) OldListY);