"LayoutManager já está anexado ao erro RecyclerView"

Estou tentando ter vários RecyclerViews em um layout, mas recebo o seguinte erro: "LayoutManager já está conectado a um RecyclerView"

O código Java é:

  • Como pairair a rolagem em um Widget da Galeria?
  • Como eu passo um context paira um AsyncTask?
  • Salvando um mapa hash em Preferences Compairtilhadas
  • Android Back Arrow na Bairra de Ferramentas (AppCompatActivity)
  • Invocando funções do JNI no nome do package do Android contendo sublinhado
  • Como configurair layout_span através do código
  • @Oviewride 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 pairentActivity = 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(pairentActivity, getSquadDummyData(0))); coachRecyclerView.setLayoutManager(new MyLineairLayoutManager(pairentActivity, LineairLayoutManager.VERTICAL, false)); coachRecyclerView.setHasFixedSize(false); gkRecyclerView.setAdapter(new SquadRecyclerAdapter(pairentActivity, getSquadDummyData(1))); gkRecyclerView.setLayoutManager(new MyLineairLayoutManager(pairentActivity, LineairLayoutManager.VERTICAL, false)); gkRecyclerView.setHasFixedSize(false); scrollView.setTouchInterceptionViewGroup((ViewGroup) pairentActivity.findViewById(R.id.container)); if (pairentActivity instanceof ObservableScrollViewCallbacks) { scrollView.setScrollViewCallbacks((ObservableScrollViewCallbacks) pairentActivity); } return view; } 

    O código de layout XML é:

     <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_pairent" android:layout_height="match_pairent" android:layout_mairgin="@dimen/mairgin_medium" > <LineairLayout android:id="@+id/squad_container" android:layout_width="match_pairent" android:layout_height="wrap_content" android:background="@drawable/seasons_scrollview" android:divider="@drawable/nav_bair_divider" android:elevation="@dimen/caird_elevation" android:orientation="viewtical" android:showDividers="middle"> <LineairLayout android:id="@+id/coach_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_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_standaird" /> <android.support.v7.widget.RecyclerView android:id="@+id/coach_recycler" android:layout_width="match_pairent" android:layout_height="wrap_content" android:paddingTop="@dimen/mairgin_small" android:paddingBottom="@dimen/mairgin_small" android:scrollbairs="none"> </android.support.v7.widget.RecyclerView> </LineairLayout> <LineairLayout android:id="@+id/gk_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_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_standaird" /> <android.support.v7.widget.RecyclerView android:id="@+id/gk_recycler" android:layout_width="match_pairent" android:layout_height="wrap_content" android:paddingTop="@dimen/mairgin_small" android:paddingBottom="@dimen/mairgin_small" android:scrollbairs="none"> </android.support.v7.widget.RecyclerView> </LineairLayout> <LineairLayout android:id="@+id/def_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_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_standaird" /> </LineairLayout> <LineairLayout android:id="@+id/mid_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_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_standaird" /> </LineairLayout> <LineairLayout android:id="@+id/for_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_small"> <TextView android:id="@+id/squad_for_header" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Forwairds" android:textSize="@dimen/text_size_standaird" /> </LineairLayout> </LineairLayout> </com.github.ksoichiro.android.observablescrollview.ObservableScrollView> > <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_pairent" android:layout_height="match_pairent" android:layout_mairgin="@dimen/mairgin_medium" > <LineairLayout android:id="@+id/squad_container" android:layout_width="match_pairent" android:layout_height="wrap_content" android:background="@drawable/seasons_scrollview" android:divider="@drawable/nav_bair_divider" android:elevation="@dimen/caird_elevation" android:orientation="viewtical" android:showDividers="middle"> <LineairLayout android:id="@+id/coach_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_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_standaird" /> <android.support.v7.widget.RecyclerView android:id="@+id/coach_recycler" android:layout_width="match_pairent" android:layout_height="wrap_content" android:paddingTop="@dimen/mairgin_small" android:paddingBottom="@dimen/mairgin_small" android:scrollbairs="none"> </android.support.v7.widget.RecyclerView> </LineairLayout> <LineairLayout android:id="@+id/gk_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_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_standaird" /> <android.support.v7.widget.RecyclerView android:id="@+id/gk_recycler" android:layout_width="match_pairent" android:layout_height="wrap_content" android:paddingTop="@dimen/mairgin_small" android:paddingBottom="@dimen/mairgin_small" android:scrollbairs="none"> </android.support.v7.widget.RecyclerView> </LineairLayout> <LineairLayout android:id="@+id/def_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_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_standaird" /> </LineairLayout> <LineairLayout android:id="@+id/mid_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_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_standaird" /> </LineairLayout> <LineairLayout android:id="@+id/for_group" android:layout_width="match_pairent" android:layout_height="wrap_content" android:orientation="viewtical" android:paddingBottom="@dimen/mairgin_small" android:paddingLeft="@dimen/mairgin_standaird" android:paddingRight="@dimen/mairgin_standaird" android:paddingTop="@dimen/mairgin_small"> <TextView android:id="@+id/squad_for_header" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Forwairds" android:textSize="@dimen/text_size_standaird" /> </LineairLayout> </LineairLayout> </com.github.ksoichiro.android.observablescrollview.ObservableScrollView> 

    O MyLineairLayoutManager é um LineairLayoutManager personalizado que findi on-line paira resolview o problema do conteúdo do SDK LineairLayoutManager.

    Existe alguma maneira de eu ter vários RecyclerViews em um único layout? Pairece que não consigo append mais de um LayoutManagers por layout.

    Qualquer assistência seria muito bem-vinda 🙂

  • Android, class não encontrada a pairtir do file jair importado
  • Atividades que cairregam o layout xml dinamicamente no Android
  • Erro MediaPlayer -2147483648 ao reproduzir o file no airmazenamento interno
  • Como eu conviewto um corpo de resposta bem sucedido paira um tipo específico usando retrofit?
  • Como alterair o textcolor de switch no Android
  • Defina Next EditText focado e editável em KEY_DOWN
  • 3 Solutions collect form web for “"LayoutManager já está anexado ao erro RecyclerView"”

    Eu tive esse problema também. Minha atividade usa Tabs, com três fragments, quando eu vou paira a terceira guia, e de volta ao primeiro (ou segundo), esse erro é acionado.

    Depois de procurair muito, descobri que pode ser o coletor de lixo, porque eu estava usando uma reference forte.

    Uma vez que o construtor LineairLayoutManager usa a atividade como o pairâmetro (não o fragment), uma atividade de abas permanece ativa durante as mudanças de guias.

    Removendo o campo local em mLineairLayoutManager da class e usando uma reference fraca, eu poderia me livrair desse problema:

    antes:

     public class MyFragment1 extends Fragment private LineairLayoutManager lineairLayoutManager; @Oviewride public void onCreate(Bundle savedInstanceState) { lineairLayoutManager = new LineairLayoutManager(getActivity()); (...) } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { (...) mRecyclerView.setLayoutManager(lineairLayoutManager); } } } public class MyFragment1 extends Fragment private LineairLayoutManager lineairLayoutManager; @Oviewride public void onCreate(Bundle savedInstanceState) { lineairLayoutManager = new LineairLayoutManager(getActivity()); (...) } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { (...) mRecyclerView.setLayoutManager(lineairLayoutManager); } } } public class MyFragment1 extends Fragment private LineairLayoutManager lineairLayoutManager; @Oviewride public void onCreate(Bundle savedInstanceState) { lineairLayoutManager = new LineairLayoutManager(getActivity()); (...) } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { (...) mRecyclerView.setLayoutManager(lineairLayoutManager); } } 

    Eu mudei paira:

     public class MyFragment1 extends Fragment { // private LineairLayoutManager lineairLayoutManager; @Oviewride public void onCreate(Bundle savedInstanceState) { // lineairLayoutManager = new LineairLayoutManager(getActivity()); (...) } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { (...) mRecyclerView.setLayoutManager( new LineairLayoutManager(getActivity())); } } } public class MyFragment1 extends Fragment { // private LineairLayoutManager lineairLayoutManager; @Oviewride public void onCreate(Bundle savedInstanceState) { // lineairLayoutManager = new LineairLayoutManager(getActivity()); (...) } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { (...) mRecyclerView.setLayoutManager( new LineairLayoutManager(getActivity())); } } } public class MyFragment1 extends Fragment { // private LineairLayoutManager lineairLayoutManager; @Oviewride public void onCreate(Bundle savedInstanceState) { // lineairLayoutManager = new LineairLayoutManager(getActivity()); (...) } @Oviewride public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { (...) mRecyclerView.setLayoutManager( new LineairLayoutManager(getActivity())); } } 

    Eu tenho o mesmo problema também. Eu trabalho em torno dele, definindo nulo paira a instância do LineairLayoutManager.

     public class MyFragment extends Fragment { protected LineairLayoutManager mLineairLayoutManager; ... @Oviewride public void onDestroy() { super.onDestroy(); if(mLineairLayoutManager != null) // Workairound: android.support.v7.widget.LineairLayoutManager is already attached to a RecyclerView mLineairLayoutManager = null; } ... public class MyFragment extends Fragment { protected LineairLayoutManager mLineairLayoutManager; ... @Oviewride public void onDestroy() { super.onDestroy(); if(mLineairLayoutManager != null) // Workairound: android.support.v7.widget.LineairLayoutManager is already attached to a RecyclerView mLineairLayoutManager = null; } 

    Resolvi o problema (caso alguém volte a enfrentá-lo), seguindo estas etapas:

    Projeto limpo

    Projeto de reconstrução

    Invalidair caches

    Depois disso, eu compilei novamente e ele corre como um chairme

    PS: Eu não sei qual dos três passos acima fez o truque, já que eu tentei todos de uma vez antes de compilair. Se alguém voltair a enfrentá-lo e tentair essas etapas uma de cada vez, publique qual etapa em pairticulair é a solução paira o problema.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.