2015-12-07 28 views
1

我從Sq-lite db.填充列表視圖現在我的問題是,當我點擊列表視圖項目轉到下一頁,然後回到列表視圖,我想列表滾動到相同的點這是以前的任何想法如何做到這一點。如何保持ListView的位置,而回到以前的ListView?

這是我在

的onCreate(list-view法)

private void populateList() { 
    descArray.clear(); 
    List<All_Post> allDesc = dbhelper.getAllDescriptions(); 
    for (All_Post all_Post : allDesc) 
    { 
     String strInspectorname = all_Post.getStringInspectorname(); 
     Log.e("strInspectorname "," = " + strInspectorname); 
     descArray.add(all_Post); 
    } 

    if (adapter == null) 
    { 
     adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray); 
     listView.setAdapter(adapter); 
    } 
    else if (adapter != null) { 

     adapter.notifyDataSetChanged(); 
     adapter = new AllPostAdapter(this, R.layout.allpostlist, descArray); 
     listView.setAdapter(adapter); 
    } 
} 

我試過這個鏈接Maintain/Save/Restore scroll position when returning to a ListView,ListView控件去alwyas最後位置。

回答

0

按照下面的步驟,這是在點擊數據庫/共享偏好某處

  1. Store中的第一個列表視圖的位置。
  2. 移至您的第二個列表視圖。
  3. 當你回到你的第一個listview只是簡單的獲取你的第一個listview位置的存儲值。

4. 對於直接滾動:

getListView().setSelection(YOUR_POSITION); 

對於平滑滾動:

getListView().smoothScrollToPosition(YOUR_POSITION); 

希望它會幫助你。

2

試試這個

// Save the ListView state (= includes scroll position) as a Parceble 
Parcelable state = listView.onSaveInstanceState(); 

// e.g. set new items 
listView.setAdapter(adapter); 

// Restore previous state (including selected item index and scroll position) 
listView.onRestoreInstanceState(state); 
0

纔去下一個頁面只是存儲列表視圖的滾動位置通過調用getScrollY/getScrollX取決於你的列表視圖方向。當你再次回到這個頁面通過調用setScrollX/setScrollY來恢復存儲位置

+0

我使用你的代碼行,但listview總是滾動的位置是0 –

+1

你可以嘗試從http://stackoverflow.com/questions/3014089解決方案/維持保存-恢復渦卷器位置時,返回到一個-列表視圖?RQ = 1 – Neo

相關問題