编程语言
首页 > 编程语言> > java – RecyclerView反向无限滚动

java – RecyclerView反向无限滚动

作者:互联网

我正在创建一个聊天应用程序,我正在尝试以相反的方式实现RecyclerView的无限滚动,例如从底部开始向上滚动,当达到顶部时,加载更多.

当用户打开聊天屏幕时,应用程序会获取最后20条消息并默认滚动到底部.我想在用户向上滚动时从服务器获取更多消息.

以下是我正在测试的通用无限滚动代码:

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

        layoutManager = new LinearLayoutManager(this);
        recyclerView =  (RecyclerView)findViewById(R.id.recyclerview);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);


        adapter = new RecyclerViewAdapter<String, ItemViewHolder>(String.class,R.layout.item,ItemViewHolder.class,list) {
            @Override
            protected void populateViewHolder(ItemViewHolder viewHolder, String model, int position) {
                if(model != null){
                    viewHolder.textView.setText(model);
                }
            }
        };
        recyclerView.setAdapter(adapter);
        recyclerView.setOnScrollListener(new EndlessReverseRecyclerViewScrollListener(layoutManager) {
            @Override
            public void onl oadMore(int page, int totalItemsCount, RecyclerView view) {
                loadMore();
            }
        });
        loadInit();
    }

    private void loadInit(){
        for(int x = 0; x < 20;x++){
            list.add(list.size()+": "+getRandomString(8));
            adapter.notifyDataSetChanged();
        }
        layoutManager.scrollToPosition(list.size() -1);
    }

    private void loadMore(){
        for(int x = 0; x < 20;x++){
            list.add(list.size()+": "+getRandomString(8));
            adapter.notifyDataSetChanged();
        }
    }

无休止的滚动课程

public EndlessReverseRecyclerViewScrollListener(LinearLayoutManager layoutManager) {
        this.layoutManager = layoutManager;
    }

    // This happens many times a second during a scroll, so be wary of the code you place here.
    // We are given a few useful parameters to help us work out if we need to load some more data,
    // but first we check if we are waiting for the previous load to finish.
    @Override
    public void onScrolled(RecyclerView view, int dx, int dy) {
        int lastVisibleItemPosition = 0;
        int totalItemCount = layoutManager.getItemCount();



        if (layoutManager instanceof StaggeredGridLayoutManager) {
            int[] lastVisibleItemPositions = ((StaggeredGridLayoutManager) layoutManager).findLastVisibleItemPositions(null);
            // get maximum element within the list
            lastVisibleItemPosition = getLastVisibleItem(lastVisibleItemPositions);
        } else if (layoutManager instanceof GridLayoutManager) {
            lastVisibleItemPosition = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
        } else if (layoutManager instanceof LinearLayoutManager) {
            lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
        }

        Log.d(TAG," *** last visible: "+lastVisibleItemPosition);
        Log.d(TAG," *** total: "+totalItemCount);
        // If the total item count is zero and the previous isn't, assume the
        // list is invalidated and should be reset back to initial state
        if (totalItemCount < previousTotalItemCount) {
            this.currentPage = this.startingPageIndex;
            this.previousTotalItemCount = totalItemCount;
            if (totalItemCount == 0) {
                this.loading = true;
            }
        }
        // If it’s still loading, we check to see if the dataset count has
        // changed, if so we conclude it has finished loading and update the current page
        // number and total item count.
        if (loading && (totalItemCount > previousTotalItemCount)) {
            loading = false;
            previousTotalItemCount = totalItemCount;
        }

        // If it isn’t currently loading, we check to see if we have breached
        // the visibleThreshold and need to reload more data.
        // If we do need to reload some more data, we execute onl oadMore to fetch the data.
        // threshold should reflect how many total columns there are too
        if (!loading && (lastVisibleItemPosition + visibleThreshold) > totalItemCount) {
            currentPage++;
            onl oadMore(currentPage, totalItemCount, view);
            loading = true;
        }
    }

    // Call this method whenever performing new searches
    public void resetState() {
        this.currentPage = this.startingPageIndex;
        this.previousTotalItemCount = 0;
        this.loading = true;
    }

    // Defines the process for actually loading more data based on page
    public abstract void onl oadMore(int page, int totalItemsCount, RecyclerView view);

这适用于从上到下滚动,有人可以协助我如何使其从下到上工作.谢谢

附: ReverseLayout不是我正在寻找的解决方案,因为它只是颠倒了我的项目的顺序,而这不是我想要实现的目标.

解决方法:

我相信你正在寻找这种方法

LinearLayoutManager.setReverseLayout(真)

如果您不希望您的物品从底部堆叠另外设置

LinearLayoutManager的setStackFromEnd(boolean)为false

标签:java,android,android-recyclerview,recyclerview-layout
来源: https://codeday.me/bug/20190611/1216564.html