从带有滑动刷新布局的 ViewPager 适配器中带有卡片列表的 RecyclerView 获取 scrollY 属性
Getting scrollY atribute from an RecycleView with CardList inside a ViewPager adapter with SwipeToRefresh layout
好的,所以我正在尝试从我的 RecycleView 中获取 scrollY 属性(以查看我在 CardList 中滚动的位置。我有以下设置
首先是主 activity ,它包含一个 SlidingTabs 布局,它有一个 SwipeToFresh 布局,因为他 child 和一个 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>
现在,我正在尝试从定义了 SwipeToRefresh 布局的主要 activity 获取 RecycleView 项目的滚动位置。我需要执行此操作以实现有关 SwipeToRefresh 布局中错误的解决方法,其中尝试从列表底部向上滚动将触发刷新调用:SwipeRefreshLayout + WebView when scroll position is at top,但如您所见,这两种解决方案都需要访问权限到 Swipe Container 和 RecycleView,在相同的 class 或 activity.
我尝试使用 int scrollY = pager.getChildAt(pager.getCurrentItem()).getScrollY();
理论上应该 return child 项(在本例中为 RecycleView)的位置,但它仍然 returns 0(添加日志条目以获取 real-time 滚动状态发生变化时的事件),就像仍在 return ViewPage 适配器的位置一样。
有人有想法吗?
根据之前的评论:您可以定义一个简单的侦听器接口,托管 activity 实现,然后各个片段可以通过该接口传递它们 RecyclerView
的滚动位置(从它们自己的本地OnScrollListener
).
描述了与片段通信的概念here in more detail with some sample code。
好的,所以我正在尝试从我的 RecycleView 中获取 scrollY 属性(以查看我在 CardList 中滚动的位置。我有以下设置
首先是主 activity ,它包含一个 SlidingTabs 布局,它有一个 SwipeToFresh 布局,因为他 child 和一个 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>
现在,我正在尝试从定义了 SwipeToRefresh 布局的主要 activity 获取 RecycleView 项目的滚动位置。我需要执行此操作以实现有关 SwipeToRefresh 布局中错误的解决方法,其中尝试从列表底部向上滚动将触发刷新调用:SwipeRefreshLayout + WebView when scroll position is at top,但如您所见,这两种解决方案都需要访问权限到 Swipe Container 和 RecycleView,在相同的 class 或 activity.
我尝试使用 int scrollY = pager.getChildAt(pager.getCurrentItem()).getScrollY();
理论上应该 return child 项(在本例中为 RecycleView)的位置,但它仍然 returns 0(添加日志条目以获取 real-time 滚动状态发生变化时的事件),就像仍在 return ViewPage 适配器的位置一样。
有人有想法吗?
根据之前的评论:您可以定义一个简单的侦听器接口,托管 activity 实现,然后各个片段可以通过该接口传递它们 RecyclerView
的滚动位置(从它们自己的本地OnScrollListener
).
描述了与片段通信的概念here in more detail with some sample code。