2015-10-11 98 views
6

我在滾動NestedScrollView內的RecyclerView,CollapsingToolbarLayout旁邊有一些問題。
問題是RecyclerView不滾動和CollapsingToolbarLayout不展開/摺疊。 這裏是XML:RecyclerView內NestedScrollView佈局不滾動

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawerLayout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layoutDirection="rtl" 
tools:context=".MainActivity"> 

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/rootLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="256dp" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsingToolbarLayout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:elevation="12dp" 
      app:contentScrim="?attr/colorPrimary" 
      app:expandedTitleMarginEnd="64dp" 
      app:expandedTitleTextAppearance="@color/trans" 
      app:layout_scrollFlags="scroll|enterAlwaysCollapsed"> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scaleType="centerCrop" 
       android:src="@drawable/header" /> 

      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:scaleType="centerCrop" 
       android:src="@drawable/header_basij" /> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:minHeight="?attr/actionBarSize" 
       android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     </android.support.design.widget.CollapsingToolbarLayout> 

    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fadeScrollbars="true" 
     android:fillViewport="true" 
     android:scrollbarDefaultDelayBeforeFade="1000" 
     android:scrollbarFadeDuration="2000" 
     android:scrollbars="vertical" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <LinearLayout 
      android:id="@+id/containerLayout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <pl.droidsonroids.gif.GifImageView 
       android:id="@+id/pbar" 
       android:layout_width="100dp" 
       android:layout_height="30dp" 
       android:layout_gravity="center" 
       android:layout_marginTop="8dp" 
       android:background="@color/trans" 
       android:src="@drawable/loader" 
       /> 

      <LinearLayout 
       android:id="@+id/interneterror" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="20dp" 
       android:orientation="vertical" 
       android:visibility="visible"> 

       <TextView 
        android:id="@+id/errorhandler" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center" 
        android:text="خطا در اتصال به اینترنت" 
        android:textColor="#212121" /> 

       <Button 
        android:id="@+id/tryagainme" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center" 
        android:text="سعی دوباره" 
        android:textColor="@color/colorPrimary" /> 

      </LinearLayout> 

      <android.support.v7.widget.RecyclerView 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:id="@+id/recyclerView" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:fadeScrollbars="true" 
       android:layoutDirection="rtl" 
       android:scrollbars="vertical" 
       tools:context=".MainActivity"/> 

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

</android.support.design.widget.CoordinatorLayout> 

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:layoutDirection="rtl" 
    app:headerLayout="@layout/navigation_header" 
    app:itemIconTint="@color/nav_item_icon_tint_color" 
    app:itemTextColor="@color/nav_item_text_color" 
    app:menu="@menu/navigation_menu" /> 

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

感謝您的幫助。

+0

的可能的複製[如何使用RecyclerView裏面NestedScrollView?](http://stackoverflow.com/questions/31000081/how-使用recyclerview-nestedscrollview) –

回答

4

你需要調用「recyclerView.setNestedScrollingEnabled(false)

+0

這是正確的答案 – Sree

+0

@Ram'recyclerView.setNestedScrollingEnabled(false)'只能在api level 21及更高版本中工作。因此不適合較低的apis –

0

的原因是因爲NestedScrollView不調用AppBarLayout.Behavour類當它從recyclerview接收滾動事件。即在r​​ecyclerview中發生滾動時,recyclerview將調度/傳遞滾動進度至NestedScrollViewNestedScrollView收到滾動事件,但什麼都不做。

裏面nestedscrollview類

@Override 
public void onNestedpreScroll(View target,int dx, int dy, int[] consumed){ 
     //Do nothing 
} 

爲了克服這個問題,讓appbarlayout滾動recyclerview時展開/摺疊,只需創建一個擴展NestedScrollView自定義類,並在此改變它通知appbarlayout上述方法和調用方法dispatchNestedPreScroll()滾動事件並使其響應。

public class CustomNestedScrollView extends NestedScrollView{ 

    public CustomNestedScrollView(Context context) { 
    super(context); 
    } 

    public CustomNestedScrollView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public CustomNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    } 

    @Override 
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed){ 
    dispatchNestedPreScroll(dx,dy,consumed,null); 
    } 
} 

然後使用這個類在layout.xml

<com.my.package.CustomNestedScrollView 
    android:id="@+id/scroll" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

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

    /* 
     CONTENT 
    */ 

</LinearLayout> 
+0

這對我來說不起作用! 這是複雜的(至少對我來說) 此外這似乎是一個過度編碼。我解決了這個問題,但沒有做到這一點。 我鼓勵你n位讀者找到更簡單的方法。因爲有! – eRaisedToX

相關問題