Reputation: 191
I want to show 2 columns in a RecyclerView, but they show in 1 column like this photo:
How can I show my view in 2 columns?
I try it in my code with 2 columns:
rcv_pro.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
but they don't show in 2 columns.
i find a soulotion for show in 2 culmn , i can fix it whit clear this code : " menuList_info_models.clear(); " but i have a new probem now , when i clear that cod , my items in recyclerview is mess , any time i scrole in recyclerview , my items change
public class MultiItem_Music_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
ArrayList<Object> objects = new ArrayList<Object>();
Context context;
private final static int MUSIC_CAT = 1;
private final static int MUSIC_ADA = 2;
private ArrayList<MenuList_Info_Model> menuList_info_models;
private ArrayList<Cat_Model> cat_models;
private Music_Adapter adapter;
private List<MenuList_Info_Model> personList = new ArrayList<>();
private RecyclerView recyclerView;
private ListAdapter mAdapter;
private ArrayList<MenuList_Info_Model> music_data;
public GridLayoutManager manager ;
public MultiItem_Music_Adapter(ArrayList<Object> objects, Context context) {
Log.e("MyLog","farzad");
this.objects = objects;
this.context = context;
if (menuList_info_models == null)
menuList_info_models = new ArrayList<MenuList_Info_Model>();
if (cat_models == null) cat_models = new ArrayList<Cat_Model>();
adapter = new Music_Adapter(menuList_info_models, this.context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder ;
LayoutInflater inflater = LayoutInflater.from(context);
View view;
switch (viewType) {
case MUSIC_CAT:
view = inflater.inflate(R.layout.music_cat_item, parent, false);
viewHolder = new Music_cat_ViewHolder(view);
break;
case MUSIC_ADA:
view= inflater.inflate(R.layout.music_item_multi, parent, false);
viewHolder = new Multi_item_ViewHolder(view);
break;
default:
view = inflater.inflate(R.layout.music_item_multi, parent, false);
viewHolder = new Multi_item_ViewHolder(view);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
Log.e("hamid_ali", "---------------------");
switch (holder.getItemViewType()) {
case MUSIC_CAT:
Cat_Config((Music_cat_ViewHolder) holder,position);
break;
case MUSIC_ADA:
item_Config((Multi_item_ViewHolder) holder,position);
break;
}
}
private void item_Config(Multi_item_ViewHolder item_viewHolder,int pos) {
MenuList_Info_Model menuList_info_model = (MenuList_Info_Model) objects.get(pos);
menuList_info_models.clear();
menuList_info_models.add(menuList_info_model);
Log.e("hamid_st",menuList_info_models.size() +"");
manager = new GridLayoutManager(this.context, 2, LinearLayoutManager.VERTICAL, false);
item_viewHolder.rcv_multi.setLayoutManager(manager);
item_viewHolder.rcv_multi.setAdapter(adapter);
}
private void Cat_Config(Music_cat_ViewHolder musicCatViewHolder,int pos) {
Cat_Model cat_model = (Cat_Model) objects.get(pos);
musicCatViewHolder.txt_cat_name.setText(cat_model.getMenu_name());
}
@Override
public int getItemCount() {
return objects.size();
}
@Override
public int getItemViewType(int position) {
if (objects.get(position) instanceof Cat_Model) {
return MUSIC_CAT;
} else {
return MUSIC_ADA;
}
}
}
Upvotes: 1
Views: 960
Reputation: 191
i must be create two ViewHolder see more code
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
class ViewHolder0 extends RecyclerView.ViewHolder {
...
public ViewHolder0(View itemView){
...
}
}
class ViewHolder2 extends RecyclerView.ViewHolder {
...
public ViewHolder2(View itemView){
...
}
@Override
public int getItemViewType(int position) {
// Just as an example, return 0 or 2 depending on position
// Note that unlike in ListView adapters, types don't have to be contiguous
return position % 2 * 2;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new ViewHolder0(...);
case 2: return new ViewHolder2(...);
...
}
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
switch (holder.getItemViewType()) {
case 0:
ViewHolder0 viewHolder0 = (ViewHolder0)holder;
...
break;
case 2:
ViewHolder2 viewHolder2 = (ViewHolder2)holder;
...
break;
}
}
}
Upvotes: 0
Reputation: 3815
You can get the two columns of an items in RecyclerView
via StaggeredGridLayoutManager
too.
From documentation, you can use following method:
StaggeredGridLayoutManager(int spanCount, int orientation)
StaggeredGridLayoutManager yourStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
yourRecyclerView.setLayoutManager(staggeredGridLayoutManager);
Or directly:
yourRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
Upvotes: 0
Reputation: 1159
You can try this for 2 columns :-
mRecycler.setLayoutManager(new GridLayoutManager(YourActivity.this, 2));
mRecycler.setLayoutManager(gridLayoutManager);
It is mendatory to setLayoutMangaer with your recyclerview otherwise it will not work.
Upvotes: 0
Reputation: 591
RvAdapter MultiItem_Music_Adapter ;
RecyclerView rcv_pro;
MultiItem_Music_Adapter = new MultiItem_Music_Adapter (getActivity());
rcv_pro.setLayoutManager(new GridLayoutManager(getActivity(), 2));
rcv_pro.setAdapter(MultiItem_Music_Adapter );
Upvotes: 1
Reputation: 69734
you can use GridLayoutManager
set
GridLayoutManager
to yourRecyclerView
uisng below code
GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
recyclerView.setLayoutManager(gridLayoutManager);
here is link how to use GridLayoutManager with RecyclerView
Upvotes: 2