onScrolled 在没有用户滚动的情况下被调用(Recycler View 无休止滚动)

onScrolled being called without the user scrolling (Recycler View endless scroll)

尝试了所有相关问题,但没有成功:(

我正在使用 Recycler View 构建无限滚动。

问题: onScrolled 方法总是在用户不滚动屏幕的情况下被调用。

Android 文档指南对 onScrolled 方法进行了描述:

Case 1) Callback method to be invoked when the RecyclerView has been scrolled. This will be called after the scroll has completed.

Case 2) This callback will also be called if visible item range changes after a layout calculation. In that case, dx and dy will be 0.

由于案例 2,我的 onScrolled 方法在用户未滚动的情况下被调用。我在屏幕上记录了 dy,结果是 dy=0 调用了 onScrolled。这发生在第二种情况。

如果我使用:

if (dy > 0){
    // The user is scrolling down
}

事实证明,无论我滚动 Recycler View 多少次,我的方法 onScrolled 都没有被调用。

我的代码

RecyclerAdapterSmallVideos.java

public class  RecyclerAdapterSmallVideos extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<VideosBigSize> videosBigSizeList;
    private Context mContext;

    final int VIEW_TYPE_ITEM = 0, VIEW_TYPE_LOADING = 1;
    ILoadMore loadMore;
    boolean isLoading;
    int visibleThreshold = 5;
    int lastVisibleItem, totalItemCount;

    public RecyclerAdapterSmallVideos(RecyclerView recyclerView, Context context, List<VideosBigSize> videosBigSizeList) {
        this.videosBigSizeList = videosBigSizeList;
        this.mContext = context;

        final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                totalItemCount = linearLayoutManager.getItemCount();
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
                if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {

                    if (loadMore != null) {

                        // This is running without the user scrolling the Recycler View. 
                        loadMore.onLoadMore();
                    }
                    isLoading = true;

                }

            }
        });


    }


    @Override
    public int getItemViewType(int position) {
        return videosBigSizeList.get(position) == null ? VIEW_TYPE_LOADING: VIEW_TYPE_ITEM;
    }


    public void setLoadMore(ILoadMore loadMore) {
        this.loadMore = loadMore;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        if (viewType == VIEW_TYPE_ITEM) {

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_small_video, parent, false);
            return new ViewHolderSmallVideos(view);
        } else if (viewType == VIEW_TYPE_LOADING) {

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_loading, parent, false);
            return new ViewHolderLoading(view);
        }
        return null;
    }


    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {

        /*########### Case 1) The holder is a instance off ViewHolderSmallVideos (Items) ###########*/
        if (holder instanceof ViewHolderSmallVideos) {

            Log.i("Debugging", "onBindViewHolder: Binding instance of ViewHolder SmallVideos: " + holder.getAdapterPosition());

            VideosBigSize singleVideoBigSize = videosBigSizeList.get(position);

            ViewHolderSmallVideos viewHolderSmallVideos = (ViewHolderSmallVideos) holder;

            // Step 1) Images
            GlideApp.with(viewHolderSmallVideos.thumbNail.getContext())
                    .load(singleVideoBigSize.getThumnail_url())
                    .centerCrop()
                    .into(viewHolderSmallVideos.thumbNail); // Video Thumbnail

            // Step 2) Determine the Max Length for each TextView
            setTextMaxLength(viewHolderSmallVideos.videoTitle, viewHolderSmallVideos.channelName, mContext);

            // Step 3) Texts
            viewHolderSmallVideos.videoTitle.setText(singleVideoBigSize.getVideo_title());
            viewHolderSmallVideos.channelName.setText(singleVideoBigSize.getChannel_name());
            viewHolderSmallVideos.videoViews.setText(singleVideoBigSize.getVideo_views());
            viewHolderSmallVideos.videoUploadDate.setText(singleVideoBigSize.getVideo_upload_date());
            viewHolderSmallVideos.videoDuration.setText(singleVideoBigSize.getVideo_duration());

            // Step 4) Handle clicks
            viewHolderSmallVideos.constraintLayoutEachItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, String.valueOf(holder.getAdapterPosition()), Toast.LENGTH_SHORT).show();
                }
            });
        }

        /*########### Case 2) The holder is a instance off ViewHolderLoading (progressBar) ###########*/
        else if (holder instanceof ViewHolderLoading) {

            Log.i("Debugging", "onBindViewHolder: Binding instance of ViewHolder Loading: " + holder.getAdapterPosition());

            ViewHolderLoading viewHolderLoading = (ViewHolderLoading) holder;
            viewHolderLoading.progressBar.setIndeterminate(true);

        }

    }

    /**
     * Get the number of items in the adapter
     * @return the number of items in the adapter
     */
    @Override
    public int getItemCount() { return videosBigSizeList.size(); }


    public void setLoaded() {
        isLoading = false;
    }

    /**
     * Determine a maximum number of characters for the Title and Channel Name
     * If they are too big and the width of the screen is too small, the text
     * will get bigger than the thumbnail and then the layout will be disturbed.
     * @param videoTitle the title of the video
     * @param channelName the name of the channel who uploaded the video
     * @param context the activity context
     */
    private void setTextMaxLength(TextView videoTitle, TextView channelName, Context context) {

        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);

        if (windowManager != null) {

            // Get the current width of the screen in DP
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
            float dpWidth = displayMetrics.widthPixels / displayMetrics.density;

            // 1) If the device is 420dp or less;
            // 2) Limit the number of characters for the title and channel name
            if (dpWidth <= 420) {
                videoTitle.setFilters(new InputFilter[]{new InputFilter.LengthFilter(35)});
                channelName.setFilters(new InputFilter[]{new InputFilter.LengthFilter(25)});
            }
        }

    }

}

ProfileFragment.java

private void setupRecyclerView(final List<VideosBigSize> userAllVideosList) {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        final RecyclerAdapterSmallVideos adapterSmallVideos = new RecyclerAdapterSmallVideos(mRecyclerView, mContext, userAllVideosList);
        mRecyclerView.setAdapter(adapterSmallVideos);

        // Set Load more event
        adapterSmallVideos.setLoadMore(new ILoadMore() {
            @Override
            public void onLoadMore() {
                if (userAllVideosList.size() <= 50) {
                    mRecyclerView.post(new Runnable() {
                        @Override
                        public void run() {
                            userAllVideosList.add(null); 
                            adapterSmallVideos.notifyItemInserted(userAllVideosList.size() - 1);

                        }
                    });
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            userAllVideosList.remove(userAllVideosList.size() - 1);
                            adapterSmallVideos.notifyItemRemoved(userAllVideosList.size()); 

                            // Random more data
                            int index = userAllVideosList.size();
                            int end = index + 10;
                            for (int i = index; i < end; i++) {
                                // Add more data to userAllVideosList...
                                adapterSmallVideos.notifyItemInserted(i);

                            }

                            adapterSmallVideos.setLoaded();

                        }
                    }, 2000);
                }
                // Loaded everything already
                else {
                    Toast.makeText(mContext, "Load data completed!", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

我的 ProfileFragment 布局

fragment_profile.xml

重要的是要注意:

1) 我的 Recycler View 位于 NesteScrollView 内,因为我需要 hide/show 工具栏。请记住我设置了 mRecyclerView.setNestedScrollingEnabled(false) 因为它会触发 NestedScrollView;

2) 除了回收站视图,我的布局中还有更多内容。

你应该使用 Paging Library 来做到这一点,click to see the best tutorial:

And the Github source code of the tutorial

原来因为分页库是一个新特性,所以我只找到了 Kotlin 教程。然后我发现这篇可能是Java写的最好的一篇。真的很感谢作者,折腾了整整一个月