简体   繁体   English

动态更改GridLayoutManager的列数

[英]Dynamically change the number of columns of a GridLayoutManager

I am actually using a GridLayoutManager with two columns in my app and I would like to have one column depending on the view type used. 我实际上在我的应用程序中使用了一个带有两列的GridLayoutManager,我希望根据所使用的视图类型有一列。

Here is the code I have in the method "onCreateView()" of my fragment : 这是我片段的方法“onCreateView()”中的代码:

// Recycler view for users
usersList = (RecyclerView) mView.findViewById(R.id.usersList);

// Layout manager (grid of users
layoutManager = new GridLayoutManager(mContext, 2);
usersList.setLayoutManager(layoutManager);

// ArrayList of users
users = new ArrayList<User>();

// Set the adapter for the list of users
mHomeListAdapter = new HomeListAdapter(getActivity(), users);
usersList.setAdapter(mHomeListAdapter);

Then, in my adapter, I have this kind of code : 然后,在我的适配器中,我有这样的代码:

@Override
public int getItemViewType(int position) {
    if (position == 5) {
        return TYPE_PREMIUM;
    } else {
        return TYPE_ITEM;
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_PREMIUM) {
        View view = mInflater.inflate(R.layout.become_premium, parent, false);
        HeaderViewHolder holder = new HeaderViewHolder(view);
        return holder;
    } else {
        View view = mInflater.inflate(R.layout.posts_item, parent, false);
        ViewHolderPosts holder = new ViewHolderPosts(view);
        return holder;
    }
}

So, when the view "TYPE_PREMIUM" is used, I would like to display a button (so in one column) and have a normal gridview (two columns) for the other results. 因此,当使用视图“TYPE_PREMIUM”时,我想显示一个按钮(所以在一列中)并且具有用于其他结果的普通网格视图(两列)。

Is it possible to do this? 是否有可能做到这一点?

Thanks! 谢谢!

mGridLayoutManager = new GridLayoutManager(mContext, 2);
mGridLayoutManager.setSpanSizeLookup(onSpanSizeLookup);    

/**
 * Helper class to set span size for grid items based on orientation and device type
 */
GridLayoutManager.SpanSizeLookup onSpanSizeLookup = new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        return mHomeListAdapter.getItemViewType(position) == TYPE_PREMIUM ? 2 : 1;
    }
};

If I understand your question, you would like to let premium-type Views span 2 columns instead of 1? 如果我理解你的问题,你想让高级类型的视图跨越2列而不是1列? That's possible by setting a custom SpanSizeLookup with setSpanSizeLookup(SpanSizeLookup) . 通过使用setSpanSizeLookup(SpanSizeLookup)设置自定义SpanSizeLookup可以实现这一点

i was use an checkBox for changing my Layout manager columns. 我使用checkBox来更改我的布局管理器列。 you should use this to change GridLayoutManager columns . 你应该用它来改变GridLayoutManager列。 here is code : 这是代码:

 checkBox = findViewById(R.id.change_manager);

    checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (checkBox.isChecked()) {
                layoutManager.setSpanCount(2);
                isLayoutChange = true;
            } else {
                isLayoutChange = false;
                layoutManager.setSpanCount(3);
            }
        }
    });

and boolean for checking layoutManager Changes in onResume() , onRestart() and onPause() method. boolean用于检查layoutManager onResume()onRestart()onPause()方法的变化。 use it like this : 像这样用它:

  if (isLayoutChange) {
        layoutManager.setSpanCount(2);
    } else {
        layoutManager.setSpanCount(3);
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM