问题描述
我实际上在应用中使用了一个带有两列的GridLayoutManager,我想根据所使用的视图类型来设置一列。
这里是代码I在我的片段的方法onCreateView()中:
//用户的Recycler视图
usersList =( RecyclerView)mView.findViewById(R.id.usersList);
布局管理器(用户网格
layoutManager = new GridLayoutManager(mContext,2);
usersList.setLayoutManager(layoutManager);
/ / ArrayList of users
users = new ArrayList< User>();
//设置用户列表的适配器
mHomeListAdapter = new HomeListAdapter(getActivity(),users );
usersList.setAdapter(mHomeListAdapter);
然后,在我的适配器中,这种类型的代码:
$ $ $ $ $ $ $ $ $ $
$ 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);
回报持有人;
} else {
View view = mInflater.inflate(R.layout.posts_item,parent,false);
ViewHolderPosts holder = new ViewHolderPosts(view);
回报持有者;
$ b所以,当使用视图TYPE_PREMIUM时,我想显示一个按钮(在一列中),并为其他结果使用正常的gridview(两列)。
是否可以这样做?
谢谢!
mGridLayoutManager = new GridLayoutManager(mContext,2);
mGridLayoutManager.setSpanSizeLookup(onSpanSizeLookup);
/ **
*辅助类根据方向和设备类型设置网格项的跨度大小
* /
GridLayoutManager.SpanSizeLookup onSpanSizeLookup = new GridLayoutManager.SpanSizeLookup (){
@Override
public int getSpanSize(int position){
return mHomeListAdapter.getItemViewType(position)== TYPE_PREMIUM? 2:1;
}
};
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!
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;
}
};
这篇关于动态更改GridLayoutManager的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!