How to view items with recyclerview

Meatball 2021-09-15 08:14:22

< Original address https://www.itbaoku.cn/post/2...;

I want to RecyclerView It shows that 2 Column , But they are shown in the same way as this picture 1 In the column :
How to be in 2 Show my view in the column ?
  I tried in my code 2 Column :

rcv_pro.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));

But they don't show up in 2 In the column .

I am here 2 culmn Found a displayed soul in , I can fix it and clear the code :" menuList_info_models.clear(); " But now I have a new problem , When I clear cod when , I am here recyclerview The items in the are a mess , Whenever I'm in recyclerview When scrolling in , My things will 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; } } }

Solution :
I have to create two ViewHolder View more codes

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

Other solutions
You can use GridLayoutManager
< Block references >
take GridLayoutManager Set to your RecyclerView The following code

GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
recyclerView.setLayoutManager(gridLayoutManager);

Here's how to use  GridLayoutManager Link to   And RecyclerView
Other solutions

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

Please bring the original link to reprint ,thank
Similar articles

2021-09-15