ListView load more on scroll bottom

喜欢而已 提交于 2019-12-04 11:16:16

Instead of using OnScrollListener, you can compare position to list data size and load more items accordingly within your adapter class.

public View getView( int position, View convertView, ViewGroup parent){
  if(position == getCount()-1){
     // load new items here. you can do a for loop here, if you'd like to add multiple items.
     addMoreItems(newItem);
  }

  // rest of the getView implementation
}

And you also need to update the following methods:

public void addMoreItems(FeedItem newItem) {
   listData.add(newItem); // since you want to add this item at the end of the list
   notifyDataSetChanged();
}

@Override
public int getCount(){
   return listData.size();
}

@Override
public Object getItem(int position){
    return listData.get(position);
}

And don't create a new adapter when you want to update your listView, like you did in your update method. If you do that, it'll get rid of the previous content. Instead, just call addMoreItems method of your adapter, it'll do the trick for you.

        @Override
        public void onScrollStateChanged(int scrollState) {
            // TODO Auto-generated method stub

            if(scrollState==RecyclerView.SCROLL_STATE_IDLE){

                  visibleItemCount = mLayoutManager.getChildCount();
                  totalItemCount = mLayoutManager.getItemCount();
                  firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();


                  if (loading) {
                   if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                   }
                  }

                  if (!loading  && (totalItemCount - visibleThreshold) <= (firstVisibleItem + visibleItemCount)) {

                      Log.v("scroll","Last Item Wow !");

                      if(footerView.getVisibility()!=View.VISIBLE){
                          footerView.setVisibility(View.VISIBLE);
                      }

                        refreshExpListViewData();
                  }
            }

        }

A very simple way to implement "endless scrolling" is to use the EndlessAdapter library by commonsguy.

https://github.com/commonsguy/cwac-endless

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!