3

I am building an app having three RecyclerView in one fragment to show horizontal list of items. i created a LinearLayoutManager object and set it to all three RecyclerView . but it crashes app, saying one LinearLayoutManager can attached to only one RecyclerView .why can't i attach to all although i need the same Properties. code is ..

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        recViewTopSell.setLayoutManager(llm);
        recViewBrands.setLayoutManager(llm);
        recViewCategory.setLayoutManager(llm);

error at

 recViewBrands.setLayoutManager(llm);
            recViewCategory.setLayoutManager(llm);
Adnan Ali
  • 792
  • 1
  • 8
  • 21
  • You can not attach the same layout manager to multiple recycler views. – David Medenjak Apr 21 '16 at 10:27
  • why ? that i am asking why cant i attach. it is just an object with properties and i want the same properties object for other RecyclerView. so why i have to make other objects – Adnan Ali Apr 21 '16 at 10:32

2 Answers2

0

No it can't be reused like that. The LayoutManager, LinearLayoutManager in your case, contains state specific to RecyclerView it is used with.

If there is a lot of setup involved for the three different LayoutMangers, consider a createLayoutManager() method to call three times instead.

Mattias Isegran Bergander
  • 11,811
  • 2
  • 41
  • 49
-1

Following Mattias answer do this:

    recViewTopSell.setLayoutManager(newLLM());
    recViewBrands.setLayoutManager(newLLM());
    recViewCategory.setLayoutManager(newLLM());

and then:

    private LinearLayoutManager newLLM() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        return linearLayoutManager;
    }
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64