如何用RecyclerView 查看项目

<原文地址https://www.itbaoku.cn/post/2...;

我想在 RecyclerView 中显示 2 列,但它们像这张照片一样显示在 1 列中:
如何在 2 列中显示我的视图?
 我在我的代码中尝试了 2 列:

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

但它们不会显示在 2 列中.
如何用RecyclerView 查看项目_第1张图片
我在 2 culmn 中找到了一个显示的灵魂,我可以修复它并清除此代码:" menuList_info_models.clear(); "但我现在有一个新的问题,当我清除 cod 时,我在 recyclerview 中的物品一团糟,每当我在 recyclerview 中滚动时,我的物品都会改变如何用RecyclerView 查看项目_第2张图片

public class MultiItem_Music_Adapter extends RecyclerView.Adapter { ArrayList objects = new ArrayList(); Context context; private final static int MUSIC_CAT = 1; private final static int MUSIC_ADA = 2; private ArrayList menuList_info_models; private ArrayList cat_models; private Music_Adapter adapter; private List personList = new ArrayList<>(); private RecyclerView recyclerView; private ListAdapter mAdapter; private ArrayList music_data; public GridLayoutManager manager ; public MultiItem_Music_Adapter(ArrayList objects, Context context) { Log.e("MyLog","farzad"); this.objects = objects; this.context = context; if (menuList_info_models == null) menuList_info_models = new ArrayList(); if (cat_models == null) cat_models = new ArrayList(); 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; } } } 
 

解决方案:
我必须创建两个ViewHolder 查看更多代码

public class MyAdapter extends RecyclerView.Adapter {
    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;
        }
    }
}

其他解决方案
你可以使用GridLayoutManager
<块引用>
将 GridLayoutManager 设置为您的 RecyclerView 下面代码

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

这里是如何使用 GridLayoutManager 的链接 与 RecyclerView
其他解决方案

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

你可能感兴趣的:(android)