有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何将RecyclerView添加到LinearLayout(高度问题)

我想添加一个带有自定义适配器的recyclerview,但当我将其添加到LinearLayout时,它不会显示任何项目,但当我将高度设置为500(例如)时,我可以看到项目,但它无法处理包裹内容或匹配父项,那么我该怎么办?如何自动设置高度? 对不起,我的英语不好

代码:

    adapterCustom = new AdapterCustom(customArrayList);
LinearLayout recyclerLayout = (LinearLayout) LayoutInflater.from(currentActivity).inflate(R.layout.recycler_layout, null);
RecyclerView recycler = (RecyclerView) recyclerLayout.findViewById(R.id.mainRecycler);
LinearLayoutManager layoutManager = new LinearLayoutManager(currentActivity, LinearLayoutManager.HORIZONTAL, false);
recycler.setLayoutManager(layoutManager);
recycler.setAdapter(adapterCustom);
recycler.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
recyclerLyt.addView(recyclerLayout);  

我忘了说,我的回收视图是水平的


共 (1) 个答案

  1. # 1 楼答案

    请在gradle文件中更新库的版本:

    编译'com。安卓支持:recyclerview-v7:23.2.0' 在此之前,recylerviews需要在“高度”上设置一个固定的高度以匹配父对象