2016-02-08 108 views
5

我使用內循環機裏面擺放嵌套滾動視圖內的多個視圖的持有人,有一個在onBindViewHolder(),因爲嵌套滾動,getItemViewType(的回收站視圖的自然行爲)的變化所有項目都被稱爲內onBindViewHolder ()當啓動回收適配器,例如我有20個項目意味着在正常情況下只有三個項目啓動時調用,但在嵌套滾動視圖的情況下,所有20個視圖創建第一次加載。RecyclerView內嵌嵌入的滾動視圖?

XML文件

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/light_gray_vd"> 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:contentScrim="@color/primaryColor" 
      app:expandedTitleMarginEnd="16dp" 
      app:expandedTitleMarginStart="48dp" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:fitsSystemWindows="true" 
       app:layout_collapseMode="parallax"> 

       <include 
        android:id="@+id/inc_gallery" 
        layout="@layout/proj_galery_new" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 

      </FrameLayout> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" /> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar1" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:layout_gravity="top" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

       <include 
        android:id="@+id/toolbar_header_view" 
        layout="@layout/header_view" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:visibility="gone" /> 
      </android.support.v7.widget.Toolbar> 

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

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

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/vde_mv_scroll_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <FrameLayout 
      android:id="@+id/rlCollapseScroll" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <android.support.v7.widget.RecyclerView 
       android:id="@+id/view_recycler" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"> 

      </android.support.v7.widget.RecyclerView> 



     </FrameLayout> 
    </android.support.v4.widget.NestedScrollView> 
</android.support.design.widget.CoordinatorLayout> 
+0

Recyclerview內滾動型是不是一個好主意。我假設你給了Recyclerview固定的高度。我認爲問題出在哪裏。 – Hein

+0

@ Hein我只用了包裝內容,但是嵌套滾動視圖分配給回收商的全部空間 – appukrb

+0

你能說出版面文件嗎? – Hein

回答

2

刪除NestedScrollViewframeLayout,並設置app:layout_behavior="@string/appbar_scrolling_view_behavior"recycleView 這樣的:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/light_gray_vd"> 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:contentScrim="@color/primaryColor" 
      app:expandedTitleMarginEnd="16dp" 
      app:expandedTitleMarginStart="48dp" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:fitsSystemWindows="true" 
       app:layout_collapseMode="parallax"> 

       <include 
        android:id="@+id/inc_gallery" 
        layout="@layout/proj_galery_new" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 

      </FrameLayout> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" /> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar1" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       android:layout_gravity="top" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

       <include 
        android:id="@+id/toolbar_header_view" 
        layout="@layout/header_view" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:visibility="gone" /> 
      </android.support.v7.widget.Toolbar> 

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

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

    <android.support.v7.widget.RecyclerView 
       android:id="@+id/view_recycler" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior" 
       android:scrollbars="vertical"> 

      </android.support.v7.widget.RecyclerView> 

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

的問題既是RecyclerviewNestedScrollview是滾動的。通過刪除一個,你應該能夠達到你想要的效果。

因此,請儘量使用Recyclerview而不是將其放入NestedScrollView。就像這樣:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main_content" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/light_gray_vd" 
    > 
    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
     > 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:contentScrim="@color/primaryColor" 
     app:expandedTitleMarginEnd="16dp" 
     app:expandedTitleMarginStart="48dp" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     > 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:fitsSystemWindows="true" 
      app:layout_collapseMode="parallax" 
      > 

     <include 
      android:id="@+id/inc_gallery" 
      layout="@layout/proj_galery_new" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      /> 

     </FrameLayout> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      /> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar1" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:layout_gravity="top" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
      > 

     <include 
      android:id="@+id/toolbar_header_view" 
      layout="@layout/header_view" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:visibility="gone" 
      /> 
     </android.support.v7.widget.Toolbar> 

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

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/view_recycler" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 
</android.support.design.widget.CoordinatorLayout> 

您也可以參考this example或閱讀本article from Code path about how to handle scroll with CoordinatorLayout

CheeseSquare from Chris Bane也有不錯的CoordinatorLayout例如

0

Android支持庫23.2.0更新後,我也有同樣的問題。

設置RecyclerViewandroid:layout_heightmatch_parent

和禁用新的自動測量功能(read more):

LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(), 
    LinearLayoutManager.VERTICAL, false); 
layoutManager.setAutoMeasureEnabled(false); 
recyclerView.setLayoutManager(layoutManager); 
+0

OnBindViewHolder()不調用,視圖不呈現 – GvSharma