使用SwipeToRefresh布局的ViewPager适配器从一个带有CardList的RecycleView中获取滚



好的,所以我试图从我的RecycleView中获得滚动属性(以查看我在CardList中滚动的位置)。我有以下设置

首先是主活动,它容纳了一个SlidingTabs布局,它有一个SwipeToFresh布局作为子布局和一个ViewPager用于标签的2个片段,如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.popal.soul.MovieListActivity">
<com.example.popal.soul.SlidingTabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:background="@color/ColorPrimary"/>

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipeContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_weight="1">
    </android.support.v4.view.ViewPager>
</android.support.v4.widget.SwipeRefreshLayout>

</LinearLayout>

然后我有一个相同的布局的每个片段,像这样,与RecycleView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/cardList"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

最后是RecycleView的每个卡片的布局(我不认为这很重要,但无论如何我都会添加它)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="4dp"
android:layout_margin="5dp">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/selectableItemBackground">
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/bkg_card"
        android:text="Movie Title"
        android:gravity="center_vertical"
        android:textColor="@android:color/white"
        android:textSize="14dp"/>
    <ImageView
        android:elevation="5dp"
        android:scaleType="fitCenter"
        android:layout_width="100dp"
        android:layout_height="150dp"
        android:id="@+id/imageViewPoster"
        android:text="Poster"
        android:gravity="center_vertical"
        android:layout_below="@id/title"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"/>
    <ImageView
        android:id="@+id/imageViewFanart"
        android:scaleType="fitCenter"
        android:layout_alignBottom="@+id/imageViewPoster"
        android:layout_toEndOf="@+id/imageViewPoster"
        android:layout_width= "235dp"
        android:layout_height="122dp"
        android:layout_marginRight="5dp"/>
 </RelativeLayout>
 </android.support.v7.widget.CardView>

现在,我正试图从主活动中获得RecycleView项目的滚动位置,其中定义了SwipeToRefresh布局。我需要这样做,以实现这个解决方案关于在SwipeToRefresh布局的错误,其中试图从列表的底部向上滚动将触发刷新调用:swiperrefreshlayout + WebView当滚动位置在顶部,但正如你所看到的,这两种解决方案都需要访问滑动容器和回收视图,在同一个类或活动。

我尝试使用int scrollY = pager.getChildAt(pager.getCurrentItem()).getScrollY();

理论上应该返回子项目的位置(在本例中是RecycleView),但它仍然返回0(添加日志条目以在滚动状态发生变化时获得实时事件),就像它仍然返回ViewPage适配器的位置一样。

有谁知道吗?

根据前面的评论:你可以定义一个简单的监听器接口,宿主活动实现,通过它,单个片段可以传递它们的RecyclerView的滚动位置(从它们自己的本地OnScrollListener)。

这里用一些示例代码更详细地描述了与片段通信的概念。

最新更新