实现EndlessRecyclerOnScrollListener时,向上滚动无穷大?

时间:2019-06-14 16:25:44

标签: java android

我想在Firebase数据库上映射EndlessRecyclerOnScrollListener。因此,我将此答案(问题)用于魁北克。但是使用此功能一切正常,但是每当我向下滚动(向上滚动)时,滚动就是无限的

Pagination in endless recycler view with firebase

@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PostRef=FirebaseDatabase.getInstance().getReference().child("Posts");

        newpost=new ArrayList<>();

        postList=(RecyclerView)findViewById(R.id.all_user_post_list);

        allPostAdapter = new AllPostAdapter(MainActivity.this,newpost);
        LinearLayoutManager layoutManager =new LinearLayoutManager(this);
        layoutManager.setReverseLayout(true);
        layoutManager.setStackFromEnd(true);
        postList.setLayoutManager(layoutManager);
        postList.setAdapter(allPostAdapter);
        postList.setOnScrollListener(new EndlessRecyclerOnScrollListener(
                layoutManager) {
            @Override
            public void onLoadMore(int current_page) {
                // do somthing...

                loadMoreData();

            }
        });
        loadData();
}

private void loadData() {
        PostRef.limitToFirst(TOTAL_ITEM_EACH_LOAD)
                .startAt(currentPage*TOTAL_ITEM_EACH_LOAD)
                .orderByChild("counter")
                .addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(!dataSnapshot.hasChildren()){
                            currentPage--;
                        }
                        for (DataSnapshot data : dataSnapshot.getChildren()) {
                            Post p = data.getValue(Post.class);
                            newpost.add(p);
                            allPostAdapter.notifyDataSetChanged();
                        }
                        mProgressBar.setVisibility(RecyclerView.GONE);
                    }


                    @Override public void onCancelled(DatabaseError databaseError) {
                        mProgressBar.setVisibility(RecyclerView.GONE);
                    }
                });
    }
    private void loadMoreData(){
        currentPage++;
        loadData();
        mProgressBar.setVisibility(RecyclerView.VISIBLE);
    }

0 个答案:

没有答案