简体   繁体   English

如何将 RecyclerView LayoutManager 从 List 更改为 Grid?

[英]How to change RecyclerView LayoutManager from List to Grid?

I'm using a ViewPager in wich I have a RecyclerView, in this RecyclerView I want to change the LayoutManager from List to Grid.我正在使用 ViewPager,我有一个 RecyclerView,在这个 RecyclerView 中,我想将 LayoutManager 从 List 更改为 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:在 RecyclerView 的 onCreateViewHolder 我有以下代码:

@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 viceverse), but the layout manager still showing the LinearLayoutManager. 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 viceverse ),但布局管理器仍显示 LinearLayoutManager。

I'd like to know, Why isn't my RecyclerViewManager changing?我想知道,为什么我的 RecyclerViewManager 没有改变?

I think you need to set the LayoutManager AND Adapter again after each click:我认为您需要在每次单击后再次设置LayoutManager AND Adapter

private LinearLayoutManager lLManager;
private GridLayoutManager glManager;
private RecyclerView.Adapter sAdapter = ...

@Override
public View onCreateView(...) {
    View viewPager = ...

    lLManager = new LinearLayoutManager(viewPager.getContext());
    glManager = new GridLayoutManager(viewPager.getContext(), 2);
    if (mData.size() % 2 != 0) { ... }

    setupRecycler();
    viewPager.findViewById(R.id.myFab).setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
            setupRecycler();
        }
    });
}

private void setupRecycler() {
    recycler.setLayoutManager(managerL ? lLManager : glManager);
    recycler.setAdapter(sAdapter);
}

Note: I'm pretty sure you don't need to call notifyDataSetChanged if you reset the adapter.注意:如果您重置适配器,我很确定您不需要调用notifyDataSetChanged

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我发现我的代码中有什么问题,我找到了一个解决方案,问题出在我的 ViewPager 的适配器中,我在这个答案中找到了解决方案: 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.我的 RecyclerView 没有从 LayoutManager 更改的原因是,在我的 ViewPager 适配器中,它显示了带有 LayoutInflater 的 RecyclerView 布局,并且由于某种原因,这使得我的 RecyclerView 没有在我上面留下的链接中从列表更改为网格,有一种更好的方法可以为 ViewPager 创建适配器,它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从RecyclerView的ItemDecoration类访问LayoutManager? - How to access the LayoutManager from the RecyclerView's ItemDecoration class? 如何从RecyclerView.Adapter访问LayoutManager以获取scrollToPosition? - How to access LayoutManager from RecyclerView.Adapter to get scrollToPosition? 如何在 Recyclerview/Layoutmanager 中获取 Scrollposition? - How to get the Scrollposition in the Recyclerview/Layoutmanager? Android RecyclerView LayoutManager异常 - Android RecyclerView LayoutManager Exception 为片段创建RecyclerView时LayoutManager和RecyclerView问题 - LayoutManager and RecyclerView Issues When Creating RecyclerView For Fragment android:-如何更改recyclerview列表项的语言? - android:- How to change the language of list items of recyclerview? RecyclerView、RecyclerView.Adapter和RecyclerView.LayoutManager在哪里声明? - Where to declare RecyclerView, RecyclerView.Adapter and RecyclerView.LayoutManager? 这是什么“RecyclerView 没有 LayoutManager androidx.recyclerview.widget.RecyclerView”错误? - What is this "RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView" error? 无法使用LayoutManager在RecyclerView中还原滚动位置 - Unable to Restore the Scroll Position in RecyclerView using LayoutManager 添加 RecyclerView Layoutmanager 时我的应用程序停止 - My app stopped when adding a RecyclerView Layoutmanager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM