好的,所以我想从我的RecycleView中获取scrollY属性(以查看我在CardList中滚动的位置。我有以下设置
首先,主要活动,其中包含一个SlidingTabs布局,其子元素为SwipeToFresh布局,以及两个选项卡片段的ViewPager,如下所示:
<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布局的主活动中获取RecycleView项的滚动位置。我需要执行此操作来解决有关SwipeToRefresh布局中的错误的变通方法,尝试从列表底部向上滚动会触发刷新调用:SwipeRefreshLayout + WebView,当滚动位置位于顶部时,但是如您所见,两种解决方案都需要访问同一类或活动中的“刷卡容器”和“ RecycleView”。
我尝试使用类似的东西 int scrollY = pager.getChildAt(pager.getCurrentItem()).getScrollY();
从理论上讲,它应该返回子项的位置(在本例中为RecycleView),但它仍返回0(添加的日志条目以在滚动状态发生变化时获取实时事件),就像if仍在返回的位置一样。 ViewPage适配器。
有人有想法吗?
如先前的评论所述:您可以定义一个简单的侦听器接口,托管活动将实现该接口,然后各个片段可以通过它们RecyclerView
(从它们自己的local OnScrollListener
)传递其滚动位置。
与片段进行通信的这一概念描述在这里更详细地一些示例代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句