2017-08-06 93 views
2

我用recyclerview一個nestedscrollview裏面如下內:recyclerview nestedscrollview addOnScrollListener(無盡scrolllistener)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView 
    android:id="@+id/mainScrollView" 
    android:layout_marginBottom="?attr/actionBarSize" 
    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"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent"> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="fill_parent" 
     android:overScrollMode="never" 
     android:layout_height="250dp" 
     android:focusableInTouchMode="true"/> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/VerticalRV" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp"/> 

</LinearLayout> 

</android.support.v4.widget.NestedScrollView> 

的問題是,當我想給聽衆(onScrollListener)設置到該回收來看,它不反正工作。我也調試了這段代碼,但它甚至沒有發現這個事件。以下是java代碼:

recyclerView.setNestedScrollingEnabled(false); 
recyclerView.setHasFixedSize(true); 
recyclerView.setLayoutManager(layoutManager); 
MyAdapter adapter = new MyAdapter(verticalShownData, this.getActivity()); 
recyclerView.setAdapter(adapter); 

recyclerView.addOnScrollListener(new HideShowScrollListener() { 
    @Override 
    public void onHide() { 
      animateCallback.animateHide(); 
    } 

    @Override 
    public void onShow() { 
      animateCallback.animateShow(); 
    } 
}); 

我該如何讓這個監聽器工作? 在此先感謝。

+0

將視圖尋呼機放在appbar的collapsingtoolbarlayout中,移除nestedscrollview並將此標誌添加到回收視圖app:layout_behavior =「@ string/appbar_scrolling_view_behavior」 – uguboz

+0

我不使用appbar,如代碼中所示。無論如何,你能否提供一些代碼給我一些線索? @uguboz –

+0

我現在沒有安裝android studio,但您可以按照https://antonioleiva.com/collapsing-toolbar-layout/教程獲得相同的結果。 – uguboz

回答

0

幾天後,我終於想出了一個解決方案。

解決方案是刪除NestedScrollView,而是使用RecyclerView標題。因此,RecyclerView的第一個元素是ViewPager,其餘的是其他對象。

我認爲使用NestedScrollView作爲RecyclerView的父項並不是一個好的做法。由於NestedScrollView強迫其孩子滿載,這違反了RecyclerView的概念。

該解決方案的靈感來自@ hister的回答this thread

相關問題