android-为什么findLastCompletelyVisibleItemPosition()返回-1?

android-为什么findLastCompletelyVisibleItemPosition()返回-1?,第1张

概述我正在尝试使用片段中的分页进行无休止的回收者视图. 但是面临的问题如下.>findLastCompletlyVisibleItemPosition()或findFirstCompletelyVisibleItemPosition()仅返回-1.>如果setNestedScrollingEnabled(false),则onScrollStateChanged不起作用以下是我的代码示例.`

我正在尝试使用片段中的分页进行无休止的回收者视图.
 但是面临的问题如下.

> findLastCompletlyVisibleItemposition()或findFirstCompletelyVisibleItemposition()仅返回-1.
>如果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);            }        });    }

编辑
该问题的解决方案在评论部分.

解决方法:

在linearlayoutmanager中查看第1732至1747行

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

如果child为null,则NO_position为-1.

你在做什么:

在设置适配器之前,您正在拨打以下电话.没有可用的视图,因此您有-1.

lastVisibleposition = mLayoutManager.findLastVisibleItemposition();firstVisibleItemposition = mLayoutManager.findFirstCompletelyVisibleItemposition();

您应该做什么:

当适配器的视图完全填充时,可以具有findLastVisibleItemposition和findFirstCompletelyVisibleItemposition.首先设置您的适配器,而不要像

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 positionslastVisibleposition = mLayoutManager.findLastVisibleItemposition();firstVisibleItemposition = mLayoutManager.findFirstCompletelyVisibleItemposition();Log.i("sandi", String.valueOf(firstVisibleItemposition));
总结

以上是内存溢出为你收集整理的android-为什么findLastCompletelyVisibleItemPosition()返回-1?全部内容,希望文章能够帮你解决android-为什么findLastCompletelyVisibleItemPosition()返回-1?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1120911.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存