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

后端 未结 2 1616
醉酒成梦
醉酒成梦 2021-01-05 12:12

This seems a question with many answers already; however, I can\'t find a common approach to this. I\'m trying to add data to a ListView when the bottom is reac

2条回答
  •  感情败类
    2021-01-05 12:54

    Here's some code for my suggested third approach, which I use in my own projects. I use the adapter's getView method to detect when the end of the list has been reached.

    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
    }
    

提交回复
热议问题