Android – why does findlastcompletelyvisibleitemposition() return – 1?

I'm trying to use paging in fragments for endless recycler view. But the problems are as follows

>Findlastcompletlyvisibleitemposition() or findfirstcompletelyvisibleitemposition() only returns - 1. > if setnestedscrollingenabled (false), onscrollstatechanged does not work

Here is my code example`

        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);
            }
        });
    }

Edit the solution to the problem in the comments section

resolvent:

Look at lines 1732 through 1747 in linear layout manager

/**
 * 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);
}

No if child is null_ Position is - 1

What are you doing:

You are dialing the following number before setting up the adapter. There is no view available, so you have - 1

lastVisiblePosition = mLayoutManager.findLastVisibleItemPosition();
firstVisibleItemPosition = mLayoutManager.findFirstCompletelyVisibleItemPosition();

What should you do:

When the view of the adapter is fully populated, you can have findlastvisibleitemposition and findfirstcompletelyvisibleitemposition. Set your adapter first, not like

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));

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>