gpt4 book ai didi

ConstraintLayout 中的 Android Recyclerview 项目不显示

转载 作者:行者123 更新时间:2023-11-30 05:12:04 26 4
gpt4 key购买 nike

我有一个包含 RecyclerView 和一些 TextView 的约束布局。除了最后一个之外,所有 RecyclerView 的可见性都已设置为“消失”。我已将 RecyclerView 的宽度和高度设置为 match_constraint。RecyclerView 的方向是水平的。这些项目也有约束布局作为他们的 parent 。在运行时,项目不会显示。

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

最佳答案

ID 为“recomm_prdct_itemdesc”的 TextView 不能将高度设置为 matchconstraint,因为它没有引用 View 来匹配其约束,例如可以与 ID 为“recomm_prdct_img”的 ImageView 匹配的宽度。它应该设置为 wrapcontent。

关于ConstraintLayout 中的 Android Recyclerview 项目不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53551673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com