java–findFirstVisibleItemPositions不适用于recycleview android

java–findFirstVisibleItemPositions不适用于recycleview android,第1张

概述我有这个代码:mRecycleView.setOnScrollListener(newRecyclerView.OnScrollListener(){@OverridepublicvoidonScrolled(RecyclerViewrecyclerView,intdx,intdy){super.onScrolled(recyclerView,dx,dy);int[]firstVisibleItemPositions=

我有这个代码:

mRecycleVIEw.setonScrollListener(new RecyclerVIEw.OnScrollListener() {    @OverrIDe    public voID onScrolled(RecyclerVIEw recyclerVIEw, int dx, int dy) {        super.onScrolled(recyclerVIEw, dx, dy);        int[] firstVisibleItempositions = new int[getResources().getInteger(R.integer.news_numbers_column)];         int pastVisiblesItems = ((StaggeredGrIDLayoutManager)getLayoutManager()).findFirstVisibleItempositions(firstVisibleItempositions)[0];    }});

当我滚动我的recyclevIEw时,我收到此错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'int androID.support.v7.Widget.OrIEntationHelper.getStartAfterpadding()' on a null object referenceat androID.support.v7.Widget.StaggeredGrIDLayoutManager$Span.findOneVisibleChild(StaggeredGrIDLayoutManager.java:2177)at androID.support.v7.Widget.StaggeredGrIDLayoutManager$Span.findFirstVisibleItemposition(StaggeredGrIDLayoutManager.java:2153)at androID.support.v7.Widget.StaggeredGrIDLayoutManager.findFirstVisibleItempositions(StaggeredGrIDLayoutManager.java:806)at info.lanouvelletribune.apps.androID.ui.fragments.NewsFragment.onScrollStateChanged(NewsFragment.java:90)at androID.support.v7.Widget.RecyclerVIEw.setScrollState(RecyclerVIEw.java:751)at androID.support.v7.Widget.RecyclerVIEw.onIntercepttouchEvent(RecyclerVIEw.java:1535)at com.github.ksoichiro.androID.observablescrollvIEw.ObservableRecyclerVIEw.onIntercepttouchEvent(ObservableRecyclerVIEw.java:188)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2060)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at androID.vIEw.VIEwGroup.dispatchtransformedtouchEvent(VIEwGroup.java:2431)at androID.vIEw.VIEwGroup.dispatchtouchEvent(VIEwGroup.java:2173)at com.androID.internal.policy.impl.PhoneWindow$DecorVIEw.superdispatchtouchEvent(PhoneWindow.java:2390)at com.androID.internal.policy.impl.PhoneWindow.superdispatchtouchEvent(PhoneWindow.java:1741)at androID.app.Activity.dispatchtouchEvent(Activity.java:2826)at com.androID.internal.policy.impl.PhoneWindow$DecorVIEw.dispatchtouchEvent(PhoneWindow.java:2351)at androID.vIEw.VIEw.dispatchPointerEvent(VIEw.java:8590)at androID.vIEw.VIEwRootImpl$VIEwPostImeinputStage.processpointerEvent(VIEwRootImpl.java:4098)at androID.vIEw.VIEwRootImpl$VIEwPostImeinputStage.onProcess(VIEwRootImpl.java:3964)at androID.vIEw.VIEwRootImpl$inputStage.deliver(VIEwRootImpl.java:3488)at androID.vIEw.VIEwRootImpl$inputStage.onDeliverToNext(VIEwRootImpl.java:3541)at androID.vIEw.VIEwRootImpl$inputStage.forward(VIEwRootImpl.java:3507)at androID.vIEw.VIEwRootImpl$inputStage.apply(VIEwRootImpl.java:3515)at androID.vIEw.VIEwRootImpl$inputStage.deliver(VIEwRootImpl.java:3488)at androID.vIEw.VIEwRootImpl$inputStage.onDeliverToNext(VIEwRootImpl.java:3541)at androID.vIEw.VIEwRootImpl$inputStage.forward(VIEwRootImpl.java:3507)at androID.vIEw.VIEwRootImpl$AsyncinputStage.forward(VIEwRootImpl.java:3617)at androID

如何处理此错误并获取第一个可见项目位置?

解决方法:

我用这个代码测试了它,它适用于我

int positionVIEw = ((linearlayoutmanager)recyclerVIEw.getLayoutManager()).findFirstVisibleItemposition();

我使用了循环视图参数.

总结

以上是内存溢出为你收集整理的java – findFirstVisibleItemPositions不适用于recycleview android全部内容,希望文章能够帮你解决java – findFirstVisibleItemPositions不适用于recycleview android所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1100187.html

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

发表评论

登录后才能评论

评论列表(0条)

保存