ConstraintLayout中的Android Recyclerview项目不显示

时间:2018-11-30 05:21:53

标签: android android-recyclerview android-constraintlayout

我有一个包含RecyclerViews和一些TextViews的约束布局,除了最后一个之外,所有Recyclerview的可见性都已设置为``消失'',我已将RecyclerView的宽度和高度设置为match_constraint.RecyclerView的方向为水平。这些项目还具有“约束布局”作为其父项。在运行时,不会显示这些项目。

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());

1 个答案:

答案 0 :(得分:0)

ID为“ recomm_prdct_itemdesc”的textview的高度不能设置为matchconstraint,因为它没有参考视图来匹配其约束,例如宽度可以与ID为“ recomm_prdct_img”的imageview匹配。应该将其设置为wrapcontent。