2015-09-07 48 views
0

滾動視圖和滾動後的許多按鈕,並在滾動視圖從頂部開始返回到佈局時選擇按鈕。我希望滾動視圖從用戶停止的地方開始。請幫助我在Android的初學者,所以請簡單解釋。將滾動視圖恢復到Onresume上的位置

回答

0

在活動或片段被毀壞之前,您必須保存您的滾動視圖的位置。

您可以在onSaveInstanceState

//save value on onSaveInstanceState 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putIntArray("SCROLL_POSITION", 
      new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()}); 
} 

保存值,然後將其還原上onRestoreInstanceState

//Restore them on onRestoreInstanceState 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    final int[] position = savedInstanceState.getIntArray("SCROLL_POSITION"); 
    if(position != null) 
     mScrollView.post(new Runnable() { 
      public void run() { 
       mScrollView.scrollTo(position[0], position[1]); 
      } 
     }); 
} 

以上僅僅是一個例子詳細內容見THIS BLOGThis post on SO

0

最好的辦法就是在這裏描述:Link
在幾句話: 你要支持的方向變化,因此節省了滾動條的X和Y位置是沒有最好的解決方案。相反,您必須獲取頂部可見項的位置並滾動到該位置。

相關問題