2015-05-29 1 views
13

ich mehrere RecyclerViews in einem Layout zu haben, ich versuche, aber ich bekomme die folgende Fehlermeldung: „Layoutmanager bereits zu einem RecyclerView angebracht ist“Fehler „Layoutmanager ist bereits zu einem RecyclerView angebracht“

Der Java-Code ist:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_squad, container, false); 

    Activity parentActivity = getActivity(); 
    final ObservableScrollView scrollView = (ObservableScrollView) view.findViewById(R.id.squad_scrollview); 

    final RecyclerView gkRecyclerView = (RecyclerView) view.findViewById(R.id.gk_recycler); 
    final RecyclerView coachRecyclerView = (RecyclerView) view.findViewById(R.id.coach_recycler); 

    coachRecyclerView.setAdapter(new SquadRecyclerAdapter(parentActivity, getSquadDummyData(0))); 
    coachRecyclerView.setLayoutManager(new MyLinearLayoutManager(parentActivity, LinearLayoutManager.VERTICAL, false)); 
    coachRecyclerView.setHasFixedSize(false); 

    gkRecyclerView.setAdapter(new SquadRecyclerAdapter(parentActivity, getSquadDummyData(1))); 
    gkRecyclerView.setLayoutManager(new MyLinearLayoutManager(parentActivity, LinearLayoutManager.VERTICAL, false)); 
    gkRecyclerView.setHasFixedSize(false); 

    scrollView.setTouchInterceptionViewGroup((ViewGroup) parentActivity.findViewById(R.id.container)); 

    if (parentActivity instanceof ObservableScrollViewCallbacks) { 
     scrollView.setScrollViewCallbacks((ObservableScrollViewCallbacks) parentActivity); 
    } 

    return view; 
} 

Der XML-Layout-Code ist:

<com.github.ksoichiro.android.observablescrollview.ObservableScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/squad_scrollview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="@dimen/margin_medium" 
    > 

    <LinearLayout 
     android:id="@+id/squad_container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/seasons_scrollview" 
     android:divider="@drawable/nav_bar_divider" 
     android:elevation="@dimen/card_elevation" 
     android:orientation="vertical" 
     android:showDividers="middle"> 

     <LinearLayout 
      android:id="@+id/coach_group" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:paddingBottom="@dimen/margin_small" 
      android:paddingLeft="@dimen/margin_standard" 
      android:paddingRight="@dimen/margin_standard" 
      android:paddingTop="@dimen/margin_small"> 

      <TextView 
       android:id="@+id/squad_coach_header" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Coach" 
       android:textSize="@dimen/text_size_standard" /> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/coach_recycler" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="@dimen/margin_small" 
       android:paddingBottom="@dimen/margin_small" 
       android:scrollbars="none"> 

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

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/gk_group" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:paddingBottom="@dimen/margin_small" 
      android:paddingLeft="@dimen/margin_standard" 
      android:paddingRight="@dimen/margin_standard" 
      android:paddingTop="@dimen/margin_small"> 

      <TextView 
       android:id="@+id/squad_gk_header" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Goalkeepers" 
       android:textSize="@dimen/text_size_standard" /> 

      <android.support.v7.widget.RecyclerView 
       android:id="@+id/gk_recycler" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:paddingTop="@dimen/margin_small" 
       android:paddingBottom="@dimen/margin_small" 
       android:scrollbars="none"> 

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

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/def_group" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:paddingBottom="@dimen/margin_small" 
      android:paddingLeft="@dimen/margin_standard" 
      android:paddingRight="@dimen/margin_standard" 
      android:paddingTop="@dimen/margin_small"> 

      <TextView 
       android:id="@+id/squad_def_header" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Defense" 
       android:textSize="@dimen/text_size_standard" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/mid_group" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:paddingBottom="@dimen/margin_small" 
      android:paddingLeft="@dimen/margin_standard" 
      android:paddingRight="@dimen/margin_standard" 
      android:paddingTop="@dimen/margin_small"> 

      <TextView 
       android:id="@+id/squad_mid_header" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Midfielders" 
       android:textSize="@dimen/text_size_standard" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/for_group" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:paddingBottom="@dimen/margin_small" 
      android:paddingLeft="@dimen/margin_standard" 
      android:paddingRight="@dimen/margin_standard" 
      android:paddingTop="@dimen/margin_small"> 

      <TextView 
       android:id="@+id/squad_for_header" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Forwards" 
       android:textSize="@dimen/text_size_standard" /> 

     </LinearLayout> 


    </LinearLayout> 


</com.github.ksoichiro.android.observablescrollview.ObservableScrollView> 

die MyLinearLayoutManager ein benutzerdefinierter LinearLayoutManager ist ich online gefunden, um die Wrap-Inhalt iss zu lösen Ue des SDK LinearLayoutManager.

Gibt es eine Möglichkeit, mehrere RecyclerViews in einem einzigen Layout zu haben? Es scheint, dass ich nicht mehr als einen LayoutManager pro Layout anhängen kann.

Jede mögliche Unterstützung sehr willkommen :)

+0

Können Sie mit der Quelle dieses LayoutManagers aktualisieren? – Eddnav

+0

Ich habe versucht, den SDK LinearLayoutManager aber immer noch den gleichen Fehler. Es ist also nicht der benutzerdefinierte LayoutManager das Problem. – TheoK

Antwort

43

Ich hatte dieses Problem auch. Meine Aktivität verwendet Registerkarten mit drei Fragmenten. Wenn ich zur dritten Registerkarte gehe und zurück zur ersten (oder zweiten), wird dieser Fehler ausgelöst.

Nachdem ich viel gesucht habe, habe ich herausgefunden, dass der Müllsammler sein könnte, weil ich eine starke Referenz verwendet habe.

Da der Konstruktor LinearLayoutManager die Aktivität als Parameter (nicht das Fragment) verwendet, bleibt eine Tabs-Aktivität während Tabulatoränderungen aktiv.

das lokale Feld in mLinearLayoutManager aus der Klasse entfernen, und mit einem schwachen Verweise, konnte ich dieses Problem loszuwerden:

vor:

public class MyFragment1 extends Fragment 
    private LinearLayoutManager linearLayoutManager; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     linearLayoutManager = new LinearLayoutManager(getActivity()); 
     (...) 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
     (...) 
     mRecyclerView.setLayoutManager(linearLayoutManager); 
    } 
} 

ich geändert:

public class MyFragment1 extends Fragment { 
    // private LinearLayoutManager linearLayoutManager; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     // linearLayoutManager = new LinearLayoutManager(getActivity()); 
     (...) 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     (...) 
     mRecyclerView.setLayoutManager(
      new LinearLayoutManager(getActivity())); 
    } 
} 
+1

sollte die richtige Antwort sein, danke! – dangalg

+1

Nicht nur es funktioniert, ich habe es auch geschafft, das Layout auf der Grundlage der Benutzerauswahl zu ändern, während das Fragment angezeigt wird, nur durch Klicken auf eine Schaltfläche. – Zvi

+1

Vielen Dank! – Muhammad

-5

ich das Problem gelöst (falls jemand steht sie wieder) mit den folgenden Schritten:

Rein Projekt

Projekt neu

Ungültige Caches

Danach habe ich wieder zusammengestellt und es wie ein Zauber

PS laufen: Ich weiß nicht, welche eine der drei Stufen über den Trick tat, da ich sie alle auf einmal versucht, vor dem Kompilieren. Wenn sich jemand erneut damit konfrontiert und diese Schritte nacheinander ausführt, posten Sie bitte, welcher Schritt speziell die Lösung des Problems ist.

+0

Funktionierte nicht für mich, aber die akzeptierte Antwort funktionierte – Zvi

+0

Es macht keinen Sinn, dies die akzeptierte Antwort des Problems mit IllegalArgumentException zu sein. @WilliamK Antwort kann Ihnen helfen, das Problem loszuwerden. – Sniper

+0

@Sniper, da es vielen Benutzern geholfen hat WilliamK's Antwort Ich änderte die angenommene Antwort – TheoK

0

Ich habe das gleiche Problem. Ich umgehe es, indem ich null auf die LinearLayoutManager-Instanz setze.

public class MyFragment extends Fragment { 
protected LinearLayoutManager mLinearLayoutManager; 
... 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if(mLinearLayoutManager != null) // Workaround: android.support.v7.widget.LinearLayoutManager is already attached to a RecyclerView 
     mLinearLayoutManager = null; 
}