android項目中記錄ListView滾動停止位置與設定顯示位置

來源:互聯網
上載者:User

標籤:

在項目中經常使用到listView控制項,當想記錄滾動停止時的記錄,當點擊載入新的資料,從屬記錄的位置開始顯示的操作怎麼實現尼?分為如下步驟

1.記錄位置代碼


[java] view plaincopy


  1. //聲明記錄停止滾動時候,可見的位置  

  2. private int stop_position;  

[java] view plaincopy


  1. @Override  

  2.     public void onScrollStateChanged(AbsListView view, int scrollState) {  

  3.         // TODO Auto-generated method stub  

  4.         // 判斷是否是最後一行,並且停止滾動  

  5.         if (isLastRow && scrollState == OnScrollListener.SCROLL_STATE_IDLE) {  

  6.             //擷取可見位置  

  7.             stop_position = lv_categories.getFirstVisiblePosition();  

  8.             // 判斷是否是最後一頁  

  9.             if (pageInfo.getNowPage() != pageInfo.getCountPage()) {  

  10.                 // 查詢下一頁資料  

  11.                 categoryManager.queryObjects(pageInfo.getNowPage() + 1);  

  12.             }  

  13.             // 記得把標識改為false  

  14.             isLastRow = false;  

  15.         }  

  16.   

  17.     }  

  18.   

  19.     @Override  

  20.     public void onScroll(AbsListView view, int firstVisibleItem,  

  21.             int visibleItemCount, int totalItemCount) {  

  22.   

  23.         if ((firstVisibleItem + visibleItemCount) >= totalItemCount) {  

  24.             isLastRow = true;  

  25.         }  

  26.   

  27.     }  




2.重新開始位置


[java] view plaincopy


  1. // 設定listView控制項的適配器  

  2. lv_categories.setAdapter(adapter);  

  3. //設定listView開始的可見位置  

  4. lv_categories.setSelection(stop_position);  



android項目中記錄ListView滾動停止位置與設定顯示位置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.