Android 当滚动停止时,如何获取Recyclerview项?
我在Scrollview中有一个Recyclerview,我想在滚动停止的地方存储Recyclerview的项目详细信息 我有一个xml,比如Android 当滚动停止时,如何获取Recyclerview项?,android,android-recyclerview,android-scrollview,Android,Android Recyclerview,Android Scrollview,我在Scrollview中有一个Recyclerview,我想在滚动停止的地方存储Recyclerview的项目详细信息 我有一个xml,比如 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/tile_view_pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="gone"
android:layout_marginRight="2dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/news_feed_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
我为recyclerview添加了ScrollListener。但当我使用父Scrollview进行活动时,没有调用recyclerview的ScrollListener。
我尝试将父scrollview更改为NestedScrollView,然后recyclerview一次作为一个项目滚动,但我希望可以自由滚动。比如滚动我们的联系人
问题:当我滚动时,父滚动视图被滚动,我对我的recyclerview没有任何控制权。因此,一旦滚动停止,就很难确定哪项recyclerview可见。
基本上,我希望在不更改XML的情况下编码。尝试
NestedScrollView
而不是scrollview
如下所示
private int overallXScrol = 0;
//...
mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
overallXScroll = overallXScroll + dx;
Log.i("check","overall->" + overallXScroll);
}
});
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="@+id/tile_view_pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="gone"
android:layout_marginRight="2dp"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/news_feed_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
我想存储已停止滚动的Recyclerview的项目详细信息
当滚动停止时,滚动状态转换为空闲状态。但是,空闲状态是滚动之前和之后的状态。因此,您希望在设置状态后过渡到空闲状态
这样做:
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
boolean scrolled;
int[] displayedPositions;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == RecyclerView.SCROLL_STATE_SETTLING) {
scrolled = true;
} else if (newState == RecyclerView.SCROLL_STATE_IDLE && scrolled) {
scrolled = false;
int[] into = new int[//number of spans];
displayedPositions = ((StaggeredGridLayoutManager) recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPositions(into)
}
}
}
我找到了解决办法。只是我们需要改变布局。我使用CoordinatorLayout作为父视图。现在我可以使用recyclerview的onScrollListener了
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/White">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/tile_view_pager"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginEnd="2dp"
android:visibility="gone"/>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/news_feed_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
您可以尝试获取回收视图的可见项。
?“它不工作”并不能很好地描述正在发生的事情。上面的代码是做什么的?在滚动变量之前,它是否保存了第一个完全可见的项目?感谢您的识别,我已经编辑了这个问题,希望现在问题清楚了。我已经测试过,此代码永远不会执行。它不会执行recyclerview的滚动侦听器,因为我们有父scrollview.add此行:recycler\u view.setNestedScrollingEnabled(false);我已经编辑了我的答案,试一下。希望它能帮助你不工作,当recyclerview正在填充时,onScrolled将只调用一次。我希望每一个卷轴都被捕获。我试过你的方法。它不起作用。请查看此链接:RecyclerView.OnScrollListener没有被调用,因为我正在使用ScrollView作为父视图。请使用NestedScrollView
而不是ScrollView
,并将android:nestedScrollingEnabled=“false”
添加到RecyclerView
的XML中。然而,这将破坏视图回收。