我想在RecyclerView中显示2列,但它们显示在1列中,如下图所示:
如何在2列中显示我的观点?
我在我的代码中尝试了2列:
rcv_pro.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
但它们不会显示在2列中。
我在2个文章中找到了表演的灵魂,我可以通过以下代码修复它: “menuList_info_models.clear();” 但是我现在有一个新的探测器,当我清除那个鳕鱼时,我在recyclerview中的物品很乱,任何时候我在recyclerview中畏缩,我的物品都会改变
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;
}
}
}
答案 0 :(得分:2)
您可以使用 GridLayoutManager
将
UIWindow
设置为您的if let rootVC = UIApplication.shared.keyWindow?.rootViewController { let chatLog = ChatLogViewController() rootVC.present(chatLog, animated: true, completion: nil) }
以下代码
GridLayoutManager
这里是关于如何将GridLayoutManager与RecyclerView
答案 1 :(得分:1)
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 );
答案 2 :(得分:0)
您可以尝试2列: -
mRecycler.setLayoutManager(new GridLayoutManager(YourActivity.this, 2));
mRecycler.setLayoutManager(gridLayoutManager);
使用您的recyclerview setLayoutMangaer是可以的,否则它将无效。
答案 3 :(得分:0)
您也可以RecyclerView
通过StaggeredGridLayoutManager
获取项目的两列。
从 documentation ,您可以使用以下方法:
StaggeredGridLayoutManager(int spanCount,int orientation)
StaggeredGridLayoutManager yourStaggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
yourRecyclerView.setLayoutManager(staggeredGridLayoutManager);
或直接:
yourRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
答案 4 :(得分:0)
我必须创建两个ViewHolder查看更多代码
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;
}
}
}