android 檢測ListView滾動到的位置


ListView滾動

1.要用到一個監聽事件是:setOnScrollListener();

API解釋是:

Set the listener that will receive notifications every time the list scrolls.

Parameters:l the scroll listener

2.它里面包含了兩個方法:
一個是:onScrollStateChanged(AbsListView view, int scrollState),用來表明ListView是什么狀態(靜止,滑動);
scrollState 為OnScrollListener.SCROLL_STATE_IDLE:表明ListView是在靜止狀態
      為OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:表明在滑動且手指沒有離開屏幕.
另外一個是:onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount)在滾動的時候獲得一些參數:
通過一個小例子來說明:
 1 public class MainActivity extends Activity {
 2     private ListView listView;
 3 
 4     @Override
 5     protected void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.activity_main);
 8         listView = (ListView) findViewById(R.id.lsitView);
 9         List<String> list = new ArrayList<String>();
10         for (int i = 0; i < 20; i++) {
11             list.add("這是第" + i + "個");
12 
13         }
14         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
15                 android.R.layout.simple_list_item_single_choice, list);
16         listView.setAdapter(adapter);
17         listView.setOnScrollListener(new ListView.OnScrollListener() {
18 
19             @Override
20             public void onScrollStateChanged(AbsListView view, int scrollState) {
21                 switch (scrollState) {
22 
23                 case OnScrollListener.SCROLL_STATE_IDLE: // 沒有滾動的時候
24                     System.out.println("最后顯示的listview的位置是--->>"
25                             + listView.getLastVisiblePosition());
26                     if (listView.getLastVisiblePosition() == (listView
27                             .getCount() - 1)) {
28                         Toast.makeText(MainActivity.this, "滑到listView底了",
29                                 Toast.LENGTH_SHORT).show();
30 
31                     }
32 
33                     if (listView.getFirstVisiblePosition() == 0) {
34                         Toast.makeText(MainActivity.this, "listView頂頭了",
35                                 Toast.LENGTH_SHORT).show();
36                     }
37 
38                     break;
39 
40                 case OnScrollListener.SCROLL_STATE_FLING:
41                     System.out.println("SCROLL_STATE_FLING");
42                     break;
43 
44                 case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
45                     System.out.println("SCROLL_STATE_TOUCH_SCROLL"
46                             + "能看到的最后一個顯示的位置--->>"
47                             + listView.getLastVisiblePosition());
48                     break;
49                 default:
50 
51                     break;
52 
53                 }
54 
55             }
56 
57             /**
58              * view The view whose scroll state is being reported
59              * firstVisibleItem the index of the first visible cell (ignore if
60              * visibleItemCount == 0) visibleItemCount the number of visible
61              * cells totalItemCount the number of items in the list adaptor
62              */
63             @Override
64             public void onScroll(AbsListView view, int firstVisibleItem,
65                     int visibleItemCount, int totalItemCount) {
66 
67             }
68         });
69 
70     }
71 }

源碼下載地址:
源碼


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM