- 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);