Dynamically change the number of columns of a GridLayoutManager

久未见 提交于 2019-12-03 16:31:53

问题


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.

Here is the code I have in the method "onCreateView()" of my fragment :

// 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.

Is it possible to do this?

Thanks!


回答1:


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;
    }
};



回答2:


If I understand your question, you would like to let premium-type Views span 2 columns instead of 1? That's possible by setting a custom SpanSizeLookup with setSpanSizeLookup(SpanSizeLookup).




回答3:


i was use an checkBox for changing my Layout manager columns. you should use this to change GridLayoutManager columns . 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. use it like this :

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


来源:https://stackoverflow.com/questions/30808225/dynamically-change-the-number-of-columns-of-a-gridlayoutmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!