0

好的,所以我試圖從我的RecycleView中獲取scrollY屬性(查看我在CardList中滾動的位置。以下設置使用SwipeToRefresh佈局的ViewPager適配器從RecycleView中獲取scrollY屬性

首先,主要的活動,裏面有一個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佈局中的一個錯誤,其中嘗試從列表底部向上滾動將觸發刷新調用:SwipeRefreshLayout + WebView when scroll position is at top,但正如您所看到的,這兩個解決方案都需要訪問Swipe Container和RecycleView,在同一個班級或活動中。

我嘗試使用類似int scrollY = pager.getChildAt(pager.getCurrentItem()).getScrollY();

這理論上應該返回子項的位置(在這種情況下,RecycleView),但它仍然返回0(添加日誌條目來獲得實時事件時滾動狀態有變化),就像如果仍然返回ViewPage適配器的位置。

任何人都有一個ideea?

+1

您可以定義一個簡單的監聽器接口,主機活動實現,然後單個片段可以通過它們傳遞其RecyclerView的滾動位置。這個概念[在這裏更詳細地描述了一些示例代碼](http://developer.android.com/training/basics/fragments/communicating.html)。 –

+0

你是對的,你的組合是答案+一些修改正是我需要的。看,它不是'getScrollY();'返回列表中的位置,但RecycleView的偵聽器'OnScrollListener',然後我可以覆蓋'public void onScrolled(RecyclerView recycleList,int dx,int dy)'方法在它裏面,這個'int position =((LinearLayoutManager)recycleList.getLayoutManager())。findFirstVisibleItemPosition();'。位置將返回當前的滾動位置,並結合您的答案,我應該能夠將其納入我的主要活動。非常感謝。 – Alex

+0

當你有時間時,你可以添加評論作爲答案,所以我可以將問題標記爲已回答,以便獲得聲望點。再次感謝。 – Alex

回答

1

根據之前的評論:您可以定義一個簡單的監聽器接口,主機活動實現並通過它單個片段可以通過其RecyclerView的滾動位置(從他們自己的本地OnScrollListener)。

這個與片段通信的概念被描述爲here in more detail with some sample code‌​

相關問題