recycleview不显示匹配父级

8dtrkrch  于 2021-06-26  发布在  Java
关注(0)|答案(2)|浏览(397)

xml格式:

<androidx.recyclerview.widget.RecyclerView
   android:visibility="visible"
   android:id="@+id/itemshowRecylerview"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:listitem="@layout/singleitemforrecylerview"/>

java代码:

itemshowRecylerview.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        itemshowRecylerview.setHasFixedSize(true);

viewholder代码:

@NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.singleitemforrecylerview, null,false);
        return new ItemDetailsAdapter.ViewHolder(view);
    }

ef1yzkbh

ef1yzkbh1#

您需要更改自定义适配器类,在充气方法中传递parent而不是null

@NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.singleitemforrecylerview, parent,false);
        return new ItemDetailsAdapter.ViewHolder(view);
    }
c3frrgcw

c3frrgcw2#

您已经在如下所示的recyclerview和工作代码中使用了这种类型的recyclerview设置方向和布局管理器。

<androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/recyclerView"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical"
           app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                        app:spanCount="1">
                    </androidx.recyclerview.widget.RecyclerView>
recyclerView.setAdapter(yourAdapter);

相关问题