가능한 중복 : 나는 다른를 실행하여 (다른 활동에 갈 때 내 활동 내의 ListView의 위치를 유지할 수있는 방법
Maintain/Save/Restore scroll position when returning to a ListView의 ListView의 위치를 유지하는 방법
의도)를 확인한 다음 다시 돌아옵니다 (뒤로 버튼 누름)?
감사합니다.
가능한 중복 : 나는 다른를 실행하여 (다른 활동에 갈 때 내 활동 내의 ListView의 위치를 유지할 수있는 방법
Maintain/Save/Restore scroll position when returning to a ListView의 ListView의 위치를 유지하는 방법
의도)를 확인한 다음 다시 돌아옵니다 (뒤로 버튼 누름)?
감사합니다.
@Override
protected void onPause()
{
// Save scroll position
SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
SharedPreferences.Editor editor = preferences.edit();
int scroll = mListView.getScrollY();
editor.put("ScrollValue", scroll);
editor.commit();
}
@Override
protected void onResume()
{
// Get the scroll position
SharedPreferences preferences = context.getSharedPreferences("SCROLL", 0);
int scroll = preferences.getInt("ScrollView", 0);
mListView.scrollTo(0, scroll);
}
당신은 스크롤 위치를 저장하고 onCreate
또는 onRestoreInstanceState
중 하나가 복원하는 데 사용할 onSaveInstanceState
를 사용해야합니다.
나는 당신의 방법을 시도했다. mListView.getSelectedItemPosition(); 나에게 -1과 mListView를 돌려 주라.getScrollY()는 0을 제공합니다. 내 테스트를 수행 할 때 명확하게 ListView로 스크롤합니다. 왜 그것이 작동하지 않는가? – michael
내 길을 시험하기 위해 당신은 무엇을 했습니까? – clahey
내가했다 : protected void onSaveInstanceState (번들 outState) { super.onSaveInstanceState (outState); int scroll = mListView.getScrollY(); System.out.println ("scrollY"+ scroll); } – michael
선언 전역 변수 :
int index = 0;
ListView list;
하고 ListView
onCreate()
에 대한 참조합니다 onResume()
에서
list = (ListView) findViewById(R.id.my_list);
다음을,에서이 줄을 추가 끝 :
,210list.setSelectionFromTop(index, 0);
마지막으로, onPause
에, 끝에 다음 줄을 추가 :
index = list.getFirstVisiblePosition();
이 ListView.getScrollY()를 사용하면 스크롤 위치를 복원 잘 작동하지 않습니다.
그것은 전체보기의 스크롤 양을 언급하고 Android: ListView.getScrollY() - does it work?
을보기 때문에 거의 항상 0
그것은이 값이 너무 나에게 대부분의 시간을 일이 될 것입니다 ListView.setSelection()이있는 ListView.getFirstVisiblePosition()이보다 안정적으로 작동합니다.
간단합니까 ....
@Override
protected void onPause()
{
index = listView.getFirstVisiblePosition();
// store index using shared preferences
}
와 ..
@Override
public void onResume() {
super.onResume();
// get index from shared preferences
if(listView != null){
if(listView.getCount() > index)
listView.setSelectionFromTop(index, 0);
else
listView.setSelectionFromTop(0, 0);
}
코드에 대한 설명이 도움이되었을 수 있습니다. –
내가 'getSharedPreferences'를 사용하는 경우는, 그것이 디스크에 유지됩니다 의미합니까? 즉, 전화를 재부팅 한 후에도 값이 유지됩니까? 내가 그걸 원치 않는다면? 전화를 재부팅하면 0부터 시작됩니까? – michael
종료를 초과하여 계속 유지됩니다. 그 값을 원하지 않으면 멤버 변수에 값을 쓸 수 있고 그것이 지속될 수 있기를 바랍니다. 응용 프로그램에서 가비지 수집되지 않으면합니다. 실수를하고 싶은 쪽을 결정하면됩니다. – CaseyB
필자는 onPause의 6 행에서 editor.putInt ("ScrollValue", scroll)를 읽어야한다고 생각합니다. – Das