首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計疑難解答

PullToRefreshGridView 快速滾動至頂部的實作方法

     編輯:程式設計疑難解答

問題

使用 PullToRefreshGridView 時,需要透過按鈕點擊實現「回到頂部」功能,但 PullToRefreshGridView 本身沒有直接暴露 smoothScrollTo 方法。

解決方案

PullToRefreshGridView 內部包裝了原生 GridView,可透過 getRefreshableView() 取得內部 GridView 實例,再呼叫其 smoothScrollToPosition(0) 方法。

程式碼範例

// 在按鈕點擊事件中
Button btnTop = findViewById(R.id.btn_top);
PullToRefreshGridView ptrGrid = findViewById(R.id.ptr_grid);

btnTop.setOnClickListener(v -> {
    // 取得內部 GridView 並平滑滾動至位置 0
    ptrGrid.getRefreshableView().smoothScrollToPosition(0);
});

補充說明

  • getRefreshableView() 回傳型別為 GridView,支援所有 AbsListView 的滾動方法。
  • 若需瞬間跳轉無動畫,可用 setSelection(0)。
  • 同樣適用於 PullToRefreshListView、PullToRefreshScrollView 等元件。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved