Android - OnScrollListener for Staggered Grid View

后端 未结 4 1024
陌清茗
陌清茗 2020-12-18 15:51

Please excuse my English, I\'m French...

So, I\'ve got a question for my Android App. I\'ve to integrate a grid view as Pinterest style. I found this lib : Staggered

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-18 16:42

    UPDATE:
    After testing,
    Calling notifyDataSetChanged() for StaggeredGridView Adapter case to reset the scroll position to 0 (Start of the Views).
    Instead use PinterestLikeAdapterView


    I have add loadMoreListener to the StaggeredGridView
    steps:
    1- add a loadmore Interface and booelan isLoading (at the top of the class)

    public interface OnLoadMoreListener {
        public boolean onLoadMore();
    }
    
    private boolean mIsLoading;
    private OnLoadMoreListener mLoadMoreListener;
    

    2- find trackMotionScroll function, in the else of deltaY > 0
    and after the up = false; add

    if (overhang <= 80) { // 80 is how nearest to the end. ZERO mean the end of list
        // scrolling down and almost reach the end of list
        if (false == mIsLoading && null != mLoadMoreListener) {
            if (false == mLoadMoreListener.onLoadMore()) {
                mIsLoading = true;
                Log.d(TAG, "loadMore");
            }
        }
    }
    

    3- add setListener() and loadComplated() functions

    public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
        this.mLoadMoreListener = onLoadMoreListener;
    }
    
    public void loadMoreCompleated() {
        mIsLoading = false;
    }
    

    HOW to use:
    1- define a loadMoreListener in your Activity/Fragment

    private StaggeredGridView.OnLoadMoreListener loadMoreListener = new StaggeredGridView.OnLoadMoreListener() {
    
        @Override
        public boolean onLoadMore() {
            loading.setVisibility(View.VISIBLE);
            // load more data from internet (not in the UI thread)
    
            return true; // true if you have more data to load, false you dont have more data to load 
        }
    };
    

    2- when you done data loading and add it to the adapter call

    private void doneLoading() {
        gridView.loadMoreCompleated();
        loading.setVisibility(View.GONE);
    }
    

提交回复
热议问题