简体   繁体   English

当宽度和高度设置为 0dp 时,回收站视图不呈现

[英]Recycler view is not rendering when width & height are set to 0dp

I have a simple layout, which contains a text view , ImageView , and a recycler view , If i set recycler's view width & height to 0dp, and connects its constraint to parent, it does not render the recycler view.我有一个简单的布局,其中包含一个text view ImageView和一个recycler view ,如果我将回收器的视图宽度和高度设置为 0dp,并将其约束连接到父级,它不会呈现回收器视图。

But if i define height in numbers then it works fine, and renders recyclerview但是如果我用数字定义高度,那么它工作正常,并呈现recyclerview

This is my code这是我的代码

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue"
    tools:context=".activities.MyActivityV2">

    <include layout="@layout/content_more_activity" />

</android.support.design.widget.CoordinatorLayout>

Inner Layout内部布局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MyActivityV2"
    tools:showIn="@layout/activity_more">

      <ImageView
        android:id="@+id/imageView23"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/logo" />


 <TextView
        android:id="@+id/tvLabelMore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="12dp"
        android:layout_marginTop="10dp"
        android:letterSpacing="0.1"
        android:text="MORE"
        android:textAllCaps="true"
        android:textColor="@color/white"
        android:textSize="24dp"
        android:textStyle="bold"
        app:layout_constraintStart_toEndOf="@+id/imageView23"
        app:layout_constraintTop_toTopOf="parent" />


 <android.support.v7.widget.RecyclerView
        android:id="@+id/rvMoreItems"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView23" />       

Class File Class 文件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        init();
        generateDataForRecyclerView();
        initRecyclerView();
    }



    private void init() {

        rvMoreItems= findViewById(R.id.rvMoreItems);

    }


     private void generateDataForRecyclerView() {

        MoreDataClass moreDataClass= new MoreDataClass();
        moreDataClass.setLabel("AAA");
        moreDataClass.setHeaderItem(true);
        dataList.add(moreDataClass);

        dataList.add(moreDataClass);
}


    private void initRecyclerView() {

        MoreAdapter moreAdapter= new MoreAdapter(this);
        moreAdapter.setData(dataList);
        rvMoreItems.setLayoutManager(new LinearLayoutManager(this));
        rvMoreItems.setAdapter(moreAdapter);

    }

Update this function check below.在下面更新此 function 检查。

 private void initRecyclerView() {

        MoreAdapter moreAdapter= new MoreAdapter(this);
        rvMoreItems.setLayoutManager(new LinearLayoutManager(this));
        rvMoreItems.setAdapter(moreAdapter);
        moreAdapter.setData(dataList);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当使用 math_parent 和 0dp 作为宽度时,具有 0dp 高度的 ConstraintLayout 视图具有不同的实际高度 - ConstraintLayout View with 0dp height has a different actual height when using math_parent and 0dp as width 以编程方式将“视图高度”设置为0dp,将权重设置为1 - Set View height to 0dp and weight to 1 programatically 在RelativeLayout中将layout_width(height)设置为0dp - Set layout_width (height) to 0dp inside RelativeLayout 当高度设置为 0dp 时,包含 TextView 的 ScrollView 在 ConstraintLayout 中消失 - ScrollView containing a TextView disappears in ConstraintLayout when height is set to 0dp 将高度设置为0dp时布局权重不起作用 - layout weight doesn't work when set the height to 0dp 宽度为 0dp 的 ConstraintLayout 视图消失 - ConstraintLayout view with width of 0dp disappears 如何在代码中将android:width设置为0dp? - How to set android:width to 0dp in code? 具有包装内容行为的 androidx Recycler View 匹配约束(0dp) - androidx Recycler View match constraint (0dp) with wrap content behaviour 为什么 UI Design Builder 中的“Infer Constraints”将某些视图的布局高度和宽度设置为 0dp? - Why does "Infer Constraints" in the UI Design Builder set the layout height and width of some views to 0dp? 约束布局的宽度和高度属性自动重置为0dp - Constraint layout's width and height attributes automatically reset to 0dp
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM