为什么 findLastCompletelyVisibleItemPosition() 返回 -1?

Sau*_*abh 6 android android-recyclerview

我正在尝试使用片段中的分页来制作无休止的回收器视图。
但面临的问题如下。

  1. findLastCompletlyVisibleItemPosition() 或 findFirstCompletelyVisibleItemPosition() 仅返回 -1。
  2. 如果 setNestedScrollingEnabled(false),则 onScrollStateChanged 不起作用

下面是我的代码示例。`

        recyclerView = (RecyclerView) view.findViewById(R.id.recycler1);

        //recyclerView.setNestedScrollingEnabled(false);                                              // Smooth Scrolling
         mLayoutManager = new LinearLayoutManager(getActivity());

        lastVisiblePosition = mLayoutManager.findLastVisibleItemPosition();
        firstVisibleItemPosition = mLayoutManager.findFirstCompletelyVisibleItemPosition();
        Log.i("sandi", String.valueOf(firstVisibleItemPosition));
        load = new LoadAdapter(getActivity(), grid_list);
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(load);
        mProgressDialog.dismiss();

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                int threshold =1;
                int count = recyclerView.getChildCount();



                Log.i("sand", String.valueOf(count));

                if (newState == SCROLL_STATE_IDLE ) {
                    if (lastVisiblePosition >= count
                            - threshold) {
                        // Execute LoadMoreDataTask AsyncTask

                        Log.i("sand","stopped");
                        new LoadMoreDataTask().execute();
                    }
                } else {
                    Log.i("sand","not stopped");
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)

编辑
此问题的解决方案在评论部分。

Har*_*shi 6

查看第 1732 至 1747 行 LinearLayoutManager

/**
 * Returns the adapter position of the last fully visible view. This position does not include
 * adapter changes that were dispatched after the last layout pass.
 * <p>
 * Note that bounds check is only performed in the current orientation. That means, if
 * LayoutManager is horizontal, it will only check the view's left and right edges.
 *
 * @return The adapter position of the last fully visible view or
 * {@link RecyclerView#NO_POSITION} if there aren't any visible items.
 * @see #findLastVisibleItemPosition()
 * @see #findFirstCompletelyVisibleItemPosition()
 */

public int findLastCompletelyVisibleItemPosition() {
    final View child = findOneVisibleChild(getChildCount() - 1, -1, true, false);
    return child == null ? NO_POSITION : getPosition(child);
}
Run Code Online (Sandbox Code Playgroud)

在这里,如果child为空,您将拥有NO_POSITION-1。

你在做什么:

您在设置适配器之前在下面调用。没有可用的视图,因此您有 -1。

lastVisiblePosition = mLayoutManager.findLastVisibleItemPosition();
firstVisibleItemPosition = mLayoutManager.findFirstCompletelyVisibleItemPosition();
Run Code Online (Sandbox Code Playgroud)

你应该做什么:

你可以findLastVisibleItemPositionfindFirstCompletelyVisibleItemPosition当你的适配器的观点是完全填入。首先设置你的适配器而不是你的位置

load = new LoadAdapter(getActivity(), grid_list);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(load);
mProgressDialog.dismiss();
// adapter is set now you may have your positions
lastVisiblePosition = mLayoutManager.findLastVisibleItemPosition();
firstVisibleItemPosition = mLayoutManager.findFirstCompletelyVisibleItemPosition();
Log.i("sandi", String.valueOf(firstVisibleItemPosition));
Run Code Online (Sandbox Code Playgroud)