1

I'm using a ViewPager in which I have a RecyclerView, in this RecyclerView I want to change the LayoutManager from List to Grid.

I've implemented this code:

 void setLManager(boolean managerL){
    GridLayoutManager glManager = new GridLayoutManager(viewPager.getContext(), 2);
      if (mData.size() % 2 != 0) {
          final int item = mData.size() - 1;
          glManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
              @Override
              public int getSpanSize(int position) {
                  return position == item ? 2 : 1;
              }
          });
      }
    LinearLayoutManager lLManager = new LinearLayoutManager(viewPager.getContext());

    recycler.setLayoutManager(managerL ? lLManager : glManager);
    sAdapter.notifyDataSetChanged();
    recycler.setAdapter(sAdapter);
}

On the RecyclerView's onCreateViewHolder I have the following code:

@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View v;

        int layout = 0;

        if(viewType == Util.LIST){

            layout = R.layout.item_list;

        }else {

            layout = R.layout.item_grid;

        }
        v = LayoutInflater.from(mContext).inflate(layout, parent, false);
        viewHolder = new RecyclerView.ViewHolder(v);
        return viewHolder; 
}

I make the change from List to Grid through a FloatingButton, changing the value of "managerL" (this is a boolean variable) so, when I press the button, the layouts change (from R.layout.item_list to R.layout_grid and vice versa), but the layout manager still showing the LinearLayoutManager.

I'd like to know, why isn't my RecyclerViewManager changing?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
DevW
  • 33
  • 5
  • I think once the adapter has made a ViewHolder, you cannot replace the view. You need to replace the entire adapter. Edit: You might get away with just setting the same adapter again! – charles-allen Nov 13 '19 at 16:27
  • Hi, thank you for answering, Can you give me an example, please? – DevW Nov 13 '19 at 16:37
  • Do you put `recycler.setLayoutManager(managerL ? lLManager : glManager);` in your FloatingButton clickEvent ? – GHH Nov 13 '19 at 16:45
  • Is your 1st block of code in `onCreate`? – charles-allen Nov 13 '19 at 16:45
  • @huang12345 yes, I put my recycler.setLayoutManager(managerL ? lLManager : glManager); in a function and this function is called in the FloatingButton clickEvent, this is my floatingButton code: – DevW Nov 13 '19 at 17:04
  • fbListGrid.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { v.setActivated(!v.isActivated()); switchViewList = !switchViewList; sAdapter.toggleItemViewType(); setLManager(switchViewList); } }); – DevW Nov 13 '19 at 17:07
  • I think you need either 2 adapters (1 for each view type) or for simplicity 2 RVs (that you show/hide). I don't think there's any way to tell 1 adapter to remake the Views/ViewHolders. – charles-allen Nov 13 '19 at 18:00

1 Answers1

0

I found what was the problem I had in my code and with this, I found a solution, the problem was in the adapter of my ViewPager, and I found the solution in this answer: https://stackoverflow.com/a/18710611

The reason why my RecyclerView didn't change from LayoutManager, was that in my ViewPager adapter it showed the RecyclerView layout with a LayoutInflater, and for some reason this made my RecyclerView not make the change from list to grid, in the link I left above, there is a better way to create the adapter for the ViewPager and it was the one that worked for me.

DevW
  • 33
  • 5