簡體   English   中英

將滾動視圖恢復到Onresume上的某個位置

[英]Resume a scroll view to a position on Onresume

我在布局上有一個滾動視圖,在滾動並選擇一個按鈕后,當它返回到布局時,有許多按鈕,滾動視圖從頂部開始。 我希望滾動視圖從用戶停止的地方開始。 請幫助是Android的初學者,所以請簡要說明。

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

您可以在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 BLOGSO上的這篇文章

最佳解決方案的描述如下: 鏈接
簡而言之:您必須支持方向更改,因此保存滾動條的X和Y位置並不是最佳解決方案。 相反,您必須獲取頂部可見項目的位置並滾動到該位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM