簡體   English   中英

檢測ListView何時到達底部 - onScroll()或onScrollStateChanged()?

[英]Detect when ListView has reached the bottom - onScroll() or onScrollStateChanged()?

這似乎是一個有很多答案的問題; 但是,我找不到一個通用的方法。 我正在嘗試在到達底部時向ListView添加數據; 使用AsyncTask從Internet檢索數據。 ListView已經附加了一個適配器。

因此,為了找到實現這一目標的好方法,我得出了兩種不同的方法。

  1. 第一個, onScrollStateChanged()方法,基本上與頁面相關。 但是,它使用的參數在實際API中沒有對應關系。 同時, 鏈接使用正確的API,但我不知道是否以正確的方式。 我嘗試了兩個鏈接中的第一個,但它有點像meh。 調試應用程序, diff值變化很大,我不知道如何正確地插入表達式。 另外,我不知道如何修復我可以開始檢索數據的偏移量; 我的意思是,我想執行代碼而不是在我即將到達底部時,而是在我到達之前。 此外,即使我們滾動到頂部,有時它也會被調用。
  2. 第二個是onScroll()方法,在答案中使用,或者以不同的方式在代碼中使用。 我嘗試調整兩個代碼中的最后一個,但它會導致很多問題,即使我們沒有到達列表的底部,也會加載數據。

那么,最好的方法是什么? 何時以及為什么我更喜歡其中一個? 在我的情況下,我應該使用哪兩個?

這是我建議的第三種方法的一些代碼,我在自己的項目中使用它。 我使用適配器的getView方法來檢測何時到達列表的末尾。

public View getView(int position, View convertView, ViewGroup parent) {
    // handle recycling/creating/initializing view
    if(reachedEndOfList(position)) loadMoreData();
    return convertView;
}

private boolean reachedEndOfList(int position) {
    // can check if close or exactly at the end
    return position == getSize() - 1;
}

private void loadMoreData() {
    // Perhaps set flag to indicate you're loading and check flag before proceeding with AsyncTask or whatever
}

您可以使用適配器檢測列表視圖何時滾動到底部,因為@darnmason在上面接受的答案中已完成,但我發現有時當列表滾動得非常快時,getView方法可能無法完成處理最后一個位置在適配器的最后...也許是因為它仍然呈現一些較早的位置。

當我滾動到列表底部有時無法渲染時,這種惱人的效果導致一個淡入視圖的按鈕。

這是具有煩人效果的解決方案,原則上類似於@darnmason的解決方案:

public abstract class MyAdapter extends BaseAdapter {

public View getView(int position, View convertView, ViewGroup parent) {

//your code for getView here...


    if(position == this.getCount() - 1){
                onScrollToBottom(position);
            }
    else{
                onScrollAwayFromBottom(position);
             }
   return convertView;
}


    public abstract void onScrollToBottom(int bottomIndex);
    public abstract void onScrollAwayFromBottom(int currentIndex);


}

此解決方案檢測列表何時滾動到底部以及何時滾動遠離底部。

要消除惱人的效果,只需修改如下:

public abstract class MyAdapter extends BaseAdapter {

public View getView(int position, View convertView, ViewGroup parent) {

//your code for getView here...


    if(position == this.getCount() - 1){
                onScrollToBottom(position);
            }
    else{
           AdapterView adapterView = (AdapterView) parent;
                int count = adapterView.getCount();
         if(adapterView.getLastVisiblePosition() == count - 1){
//The adapter was faking it, it is already at the bottom!
                    onScrollToBottom(count - 1);
          }
         else {
//Honestly! The adapter is truly not at the bottom.
                    onScrollAwayFromBottom(position);
               }
             }
   return convertView;
}


    public abstract void onScrollToBottom(int bottomIndex);
    public abstract void onScrollAwayFromBottom(int currentIndex);


}

現在像往常一樣調用適配器,如下所示:

MyAdapter adapter = new MyAdapter(){

  @Override
            public void onScrollToBottom(int bottomIndex) {
/*loadMore is a button that fades into view when you are not at the bottom of the list so you can tap and load more data*/
                    loadMore.show();

            }

            @Override
            public void onScrollAwayFromBottom(int currentIndex) {
/*loadMore is a button that fades out of view when you are not at the bottom of the list*/
                  loadMore.hide();

            }

}

以這種方式實現時,適配器在檢測列表何時滾動到底部時非常有效。

所需要的只是列表中的一點合作!

暫無
暫無

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

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