Элементы Android Recyclerview в ConstraintLayout не отображаются

У меня есть макет ограничения, в котором есть RecyclerViews и некоторые TextViews. Вся видимость Recyclerview должна быть установлена ​​​​на «ушла», кроме последней. Я установил ширину и высоту этого RecyclerView на match_constraint. Ориентация RecyclerView Горизонтальная. Элементы также имеют Constraint Layout в качестве родителя. Во время выполнения элементы не отображаются.

XML:

home_lyt_children.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v4.view.ViewPager
    android:layout_width="0dp"
    android:layout_height="150dp"
    android:id="@+id/pager"
    android:layout_margin="8dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    >
</android.support.v4.view.ViewPager>
<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="SHOP BY CATEGORY"
    android:textAlignment="center"
    android:textSize="25sp"
    android:id="@+id/ctgry_lbl"
    app:layout_constraintTop_toBottomOf="@id/pager"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>
    <android.support.v7.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/ctgry_recycler"
        android:visibility="gone"
        app:layout_constraintStart_toStartOf="@id/pager"
        app:layout_constraintEnd_toEndOf="@id/pager"
        app:layout_constraintTop_toBottomOf="@id/ctgry_lbl"
        >
    </android.support.v7.widget.RecyclerView>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="POPULAR PRODUCTS"
        android:textAlignment="center"
        android:textSize="25sp"
        android:id="@+id/poplr_lbl"
        app:layout_constraintTop_toBottomOf="@id/ctgry_recycler"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
    <android.support.v7.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/poplr_recycler"
        android:visibility="gone"
        app:layout_constraintStart_toStartOf="@id/pager"
        app:layout_constraintEnd_toEndOf="@id/pager"
        app:layout_constraintTop_toBottomOf="@id/poplr_lbl">
    </android.support.v7.widget.RecyclerView>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="NEW PRODUCTS"
        android:textAlignment="center"
        android:textSize="25sp"
        android:id="@+id/newprdct_lbl"
        app:layout_constraintTop_toBottomOf="@id/poplr_recycler"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
    <android.support.v7.widget.RecyclerView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/newprdcts_recycler"
        android:visibility="gone"
        app:layout_constraintStart_toStartOf="@id/pager"
        app:layout_constraintEnd_toEndOf="@id/pager"
        app:layout_constraintTop_toBottomOf="@id/newprdct_lbl">
    </android.support.v7.widget.RecyclerView>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="RECOMMENDED FOR YOU"
        android:textAlignment="center"
        android:textSize="25sp"
        android:id="@+id/recommended_lbl"
        app:layout_constraintTop_toBottomOf="@id/newprdcts_recycler"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recommended_recycler"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@id/pager"
        app:layout_constraintStart_toStartOf="@id/pager"
        app:layout_constraintTop_toBottomOf="@id/recommended_lbl">
    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

home_recomended_prdcts_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<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">

    <ImageView
        android:id="@+id/recomm_prdct_img"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@color/colorPrimary"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/recomm_prdct_itemtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Oppo"
        android:textAlignment="center"
        android:textSize="25sp"
        app:layout_constraintEnd_toEndOf="@+id/recomm_prdct_img"
        app:layout_constraintStart_toStartOf="@+id/recomm_prdct_img"
        app:layout_constraintTop_toBottomOf="@+id/recomm_prdct_img" />

    <TextView
        android:id="@+id/recomm_prdct_itemdesc"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="8dp"
        android:text="Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem Ipsum Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum "
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@+id/recomm_prdct_img"
        app:layout_constraintStart_toStartOf="@+id/recomm_prdct_img"
        app:layout_constraintTop_toBottomOf="@+id/recomm_prdct_itemtitle" />
</android.support.constraint.ConstraintLayout>

Код:

адаптер:

public class HomeRecommendedProductsAdapter extends RecyclerView.Adapter<HomeRecommendedProductsAdapter.HomeRecommendedViewHolder> {

    @NonNull
    @Override
    public HomeRecommendedViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new HomeRecommendedViewHolder(LayoutInflater.from(viewGroup.getContext()).
                inflate(R.layout.home_recomended_prdcts_item,viewGroup,false));
    }

    @Override
    public void onBindViewHolder(@NonNull HomeRecommendedViewHolder popularproductsViewHolder, int i) {
    popularproductsViewHolder.recomm_prdct_itemtitle.setText("Google Pixel");
    popularproductsViewHolder.recomm_prdct_itemdesc.setText("google Pixel Rocks google Pixel Rocks google Pixel Rocks google Pixel Rocks google Pixel Rocks google Pixel Rocks");
    }

    @Override
    public int getItemCount() {
        return 10;
    }

    public class HomeRecommendedViewHolder extends RecyclerView.ViewHolder{
        private ImageView recomm_prdct_img;
        private TextView recomm_prdct_itemtitle,recomm_prdct_itemdesc;
        public HomeRecommendedViewHolder(@NonNull View itemView) {
            super(itemView);
            initializeIds(itemView);
        }

        private void initializeIds(View view) {
            recomm_prdct_img=view.findViewById(R.id.recomm_prdct_img);
            recomm_prdct_itemtitle=view.findViewById(R.id.recomm_prdct_itemtitle);
            recomm_prdct_itemdesc=view.findViewById(R.id.recomm_prdct_itemdesc);
        }
    }
}

HomeActivity.java:

recomm_recycler.setLayoutManager(new LinearLayoutManager(HomeActivity.this,LinearLayout.HORIZONTAL,false));
        recomm_recycler.setAdapter(new HomeRecommendedProductsAdapter());

person jobin    schedule 30.11.2018    source источник
comment
Измените высоту вашего ConstraintLayout в home_recomended_prdcts_item.xml: на android:layout_height="wrap_content"   -  person AskNilesh    schedule 30.11.2018
comment
Я изменил его, и я мог видеть изображение и заголовок, но не описание, для которого также было установлено значение match_constraint. Почему это не сработало, если установлено значение match_constraint?   -  person jobin    schedule 30.11.2018
comment
Вы пытались вместо этого установить высоту корня ConstraintLayout в вашем представлении элемента на wrap_content? И поможет ли setHasFixedSize в вашем случае?   -  person Aaron    schedule 30.11.2018
comment
В дополнение к установке android:layout_height="wrap_content" для ConstraintLayout вам также необходимо android:layout_height="wrap_content" для android:id="@+id/recomm_prdct_itemdesc"   -  person makvasic    schedule 30.11.2018


Ответы (1)


Текстовое представление с идентификатором «recomm_prdct_itemdesc» не может иметь высоту, установленную в соответствии с ограничением, поскольку у него нет эталонного представления, соответствующего его ограничению, например, ширина, которая может быть сопоставлена ​​с представлением изображения с идентификатором «recomm_prdct_img». Он должен быть установлен в значение wrapcontent.

person Nataraj KR    schedule 30.11.2018