Android ListView 滾動的N種方法

來源:互聯網
上載者:User

標籤:android   io   ar   os   java   sp   div   on   art   

Android 裡面讓ListView滾動有N種方法,這兒列舉三種: 
我的需求是通過按鍵讓Listview滾動起來,當然這些按鍵不是通過Android標識介面傳輸過來的,所以不能通過監聽按鍵事件來實現此功能; 
一、方法一: 
也是最常用的方法: 

Java代碼  
  1. listView.setSelection(position);  


直接跳轉到指定的位置,可以是事件每觸發一次,執行一次,形成滾動效果; 


二、方法二: 

Java代碼  
  1. listView.smoothScrollBy(30, 200);  
  2. listView.smoothScrollToPosition(index);  


smoothScrollToPosition類似於setSelection,但setSelection沒有動畫效果,直接跳轉,smoothScrollToPosition是類似於手滑動,有滾動過程,從名字上就是平滑的意思; 

三、方法三: 
這個方法比較另類,一般用不到,那就是先讓listview擷取焦點,然後注入上下按鍵事件: 

Java代碼  
    1. void sendKey(final int key) {  
    2.         new Thread() {  
    3.             public void run() {  
    4.                 try {  
    5.                     Instrumentation inst = new Instrumentation();  
    6.                     inst.sendKeyDownUpSync(key);  
    7.                 } catch (Exception e) {  
    8.                     e.printStackTrace();  
    9.                 }  
    10.             }  
    11.         }.start();  
    12.     } 

Android ListView 滾動的N種方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.