6

Wenn ich RecyclerView innen NestedScrollView setzen dann onBindViewHolder wird für alle Reihe Aufruf wie sage ich Liste haben, die eine Größe von 30 hat dann onBindViewHolder für alle 30 Zeilen auf einmal aufgerufen wird, auch ohneRecyclerView innen NestedScrollView onBindViewHolder für alle getItemCount Größe Aufruf

Scrollen
RecyclerView list; 
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); 
     list.setLayoutManager(layoutManager); 
     layoutManager.setAutoMeasureEnabled(true); 
     list.setNestedScrollingEnabled(false); 
     list.addItemDecoration(new VerticalSpaceItemDecoration(5)); 
     list.setAdapter(adapter); 

mein xml ist

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" 
    android:scrollbars="none" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/grey"> 
    <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler_views" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/info" 
      android:layout_marginBottom="5dp" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:textAlignment="center" 

      android:visibility="visible" 
      /> 

aber wenn ich NestedScrollView entferne es funktioniert richtig.

+0

Haben Sie irgendeine Lösung für dieses Problem gefunden, Dies ist definitiv im Zusammenhang mit RecylerView in NestedScrollView –

Antwort

2

Ich gehe davon aus, dass Sie versuchen, etwas mit AppBarLayout zu tun, seit Sie appbar_scrolling_view_behavior verwenden.

Wenn dies der Fall ist, können Sie RecyclerView als direktes Kind von CoordinatorLayout verwenden und Unterstützung für AppBarLayout Scrollen ohne Verschachtelung von RecyclerView innerhalb von NestedScrollView.

starten: RecyclerView innerhalb CoordinatorLayout (mit AppBarLayout und CollapsingToolbarLayout):

<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:layout_scrollFlags="scroll"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="80dp" 
       android:background="#55FF00FF" 
       app:layout_collapseMode="none"/> 

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

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" /> 
</android.support.design.widget.CoordinatorLayout> 

Und in Ihrer Aktivität oder Custom:

RecyclerView list; 
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext()); 
list.setLayoutManager(layoutManager); 
list.addItemDecoration(new VerticalSpaceItemDecoration(5)); 
list.setAdapter(adapter); 
1

Aber Sie setzen android: layout_height für NestedScrollView zu wrap_content - hier ist es standardmäßig Null (weil es keinen Inhalt für ihn bei der Zeitpunkt der Erklärung). Als nächstes setzen Sie für RecyclerView android: layout_height auf match_parent - was im Moment 0 ist. So haben alle Ihre Artikel 0 Höhe.

So haben Sie eine solche Situation. Lösung: Verwenden Sie die obige Lösung von @dkarmazi https://stackoverflow.com/a/37558761/3546306 oder versuchen Sie, Parameter android: layout_height Werte zu ändern.

3

Problem verursacht für das Höhenproblem.

1) Bearbeiten Sie die NestedScrollView & RecyclerView, wie folgend:

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    ...... 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
     ....... 
     <android.support.v7.widget.RecyclerView 
      android:id="@+id/recycler_views" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      ..... 
      /> 

2) Stellen Sie sicher, dass Sie 'com.android.support:recyclerview-v7:23.2.1' zusammengestellt

+0

Haben Sie es schon probiert? Beispiel: "Log.d (...)" in "onBindViewHolder" einfügen. Ich denke, das ist nicht der Grund – BNK

1

Es ist right.Because Sie eine ScrollView verwenden. ScrollView ist nicht recyclebar wie RecyclerView oder ListView. Es wird zeigen alle Ansicht enthält diese aus Bildschirm auf einmal. Sie sollten stattdessen ein anderes Layout verwenden.