RecyclerView不在ConstraintLayout

时间:2018-12-13 22:36:42

标签: android xml android-recyclerview android-constraintlayout

我有一个RecyclerView,它不会在ConstraintLayout内滚动,项目已设置但它们不动。我相信问题出在XML中,因为信息就在那里,第二项被删减了。谁能知道我的xml布局是否有问题

这是我的product.xml,其中有RecyclerView

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:background="#fafafa"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorBackgroundProducts"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <TextView
            android:id="@+id/textView24"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="32dp"
            android:gravity="center"
            android:text="You need at leat € 1.000 on your Account Balance to add an Investment Account"
            android:textColor="#5e657b"
            android:textSize="13sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <androidx.cardview.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="80dp"
            android:layout_marginEnd="16dp"
            app:cardCornerRadius="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">


            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">


                <TextView
                    android:id="@+id/textView25"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginBottom="24dp"
                    android:text="My Account Balance"
                    android:textColor="#5e657b"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/textView26"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:text="€ 5.000,00"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/textView25"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/textView25" />
            </androidx.constraintlayout.widget.ConstraintLayout>


        </androidx.cardview.widget.CardView>

        <TextView
            android:id="@+id/textView27"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="+ Add fund to your account "
            android:textColor="#00ce7c"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="@+id/cardView"
            app:layout_constraintStart_toStartOf="@+id/cardView"
            app:layout_constraintTop_toBottomOf="@+id/cardView" />


    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout">



            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/productsRv"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:background="@color/colorWhite"
                android:layoutAnimation="@anim/layout_animation_fall_down"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:spanCount="1"
                tools:listitem="@layout/row_product" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的行产品,其中具有项目布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginBottom="40dp"
    app:cardCornerRadius="20dp"
    android:layout_height="240dp">


    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_marginTop="20dp"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/percentage_tae"
            android:layout_width="74dp"
            android:layout_height="37dp"
            android:layout_marginStart="16dp"
            android:text="2,5% TAE"
            android:textColor="@color/blank"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/account_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="My Account"
        android:textStyle="bold"
        android:textColor="#0a0f35"
        android:textSize="17sp"
        app:layout_constraintBottom_toBottomOf="@+id/percentage_tae"
        app:layout_constraintStart_toEndOf="@+id/percentage_tae"
        app:layout_constraintTop_toTopOf="@+id/percentage_tae" />

        <TextView
            android:id="@+id/conditionsButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:text="Conditions"
            android:textColor="@color/colorIban"
            app:layout_constraintBottom_toBottomOf="@+id/account_name"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/account_name" />

        <TextView
            android:id="@+id/textView29"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="23dp"
            android:text="Interest payment period"
            app:layout_constraintStart_toStartOf="@+id/percentage_tae"
            app:layout_constraintTop_toBottomOf="@+id/percentage_tae" />

        <TextView
            android:id="@+id/interestPaymentPeriodText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Daily"
            app:layout_constraintBottom_toBottomOf="@+id/textView29"
            app:layout_constraintEnd_toEndOf="@+id/conditionsButton"
            app:layout_constraintTop_toTopOf="@+id/textView29" />

        <TextView
            android:id="@+id/textView31"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:text="Deposit maturity"
            app:layout_constraintStart_toStartOf="@+id/textView29"
            app:layout_constraintTop_toBottomOf="@+id/textView29" />

        <TextView
            android:id="@+id/deposityMaturiryInvestmentText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            android:text="1 Year"
            app:layout_constraintBottom_toBottomOf="@+id/textView31"
            app:layout_constraintEnd_toEndOf="@+id/interestPaymentPeriodText"
            app:layout_constraintTop_toTopOf="@+id/textView31" />

        <TextView
            android:id="@+id/textView33"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="12dp"
            android:text="Minimium Investment"
            app:layout_constraintStart_toStartOf="@+id/textView31"
            app:layout_constraintTop_toBottomOf="@+id/textView31" />

        <Button
            android:id="@+id/addInvestmentAccount"
            android:layout_width="0dp"
            android:layout_height="52dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="16dp"
            android:background="@drawable/selector_button_green"
            android:text="Add investment account"
            android:textAllCaps="false"
            android:textColor="@color/blank"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/minimumInvestmentText"
            app:layout_constraintStart_toStartOf="@+id/textView33"
            app:layout_constraintTop_toBottomOf="@+id/textView33" />

        <TextView
            android:id="@+id/minimumInvestmentText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="€ 1.000"
            app:layout_constraintBottom_toBottomOf="@+id/textView33"
            app:layout_constraintEnd_toEndOf="@+id/deposityMaturiryInvestmentText"
            app:layout_constraintTop_toTopOf="@+id/textView33"
            app:layout_constraintVertical_bias="0.0" />

        <View
            android:id="@+id/view5"
            android:layout_width="0dp"
            android:layout_height="1dp"
            android:layout_marginTop="13dp"
            android:background="#e8e8e8"
            app:layout_constraintEnd_toEndOf="@+id/conditionsButton"
            app:layout_constraintStart_toStartOf="@+id/percentage_tae"
            app:layout_constraintTop_toBottomOf="@+id/percentage_tae" />


    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

2 个答案:

答案 0 :(得分:0)

尝试以下代码:

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

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@color/colorBackgroundProducts"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView24"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="32dp"
            android:gravity="center"
            android:text="You need at leat € 1.000 on your Account Balance to add an Investment Account"
            android:textColor="#5e657b"
            android:textSize="13sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="80dp"
            android:layout_marginEnd="16dp"
            app:cardCornerRadius="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textView25"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginBottom="24dp"
                    android:text="My Account Balance"
                    android:textColor="#5e657b"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@+id/textView26"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:text="€ 5.000,00"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/textView25"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/textView25" />
            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.cardview.widget.CardView>

        <TextView
            android:id="@+id/textView27"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:text="+ Add fund to your account "
            android:textColor="#00ce7c"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="@+id/cardView"
            app:layout_constraintStart_toStartOf="@+id/cardView"
            app:layout_constraintTop_toBottomOf="@+id/cardView" />


    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/productsRv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:background="@color/colorWhite"
        android:layoutAnimation="@anim/layout_animation_fall_down"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/constraintLayout"
        app:spanCount="1"
        tools:listitem="@layout/row_product" />

</androidx.constraintlayout.widget.ConstraintLayout>

希望它可以为您提供帮助。

答案 1 :(得分:0)

使用约束布局的目的是删除所有不必要的布局嵌套。

也不建议在ConstraintLayout中使用match_parent,而是使用match_constraint0dp)并将其约束为parent

如果您需要一些“视图”来创建约束点,则只需输入准则即可。

<?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"
    android:background="#F0CD"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView24"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginStart="32dp"
        android:layout_marginTop="32dp"
        android:gravity="center"
        android:text="You need at leat € 1.000 on your Account Balance to add an Investment Account"
        android:textColor="#5e657b"
        android:textSize="13sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <android.support.v7.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="80dp"
        app:cardCornerRadius="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/textView25"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="24dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="24dp"
                android:text="My Account Balance"
                android:textColor="#5e657b"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@+id/textView26"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:text="€ 5.000,00"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@+id/textView25"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/textView25" />
        </android.support.constraint.ConstraintLayout>


    </android.support.v7.widget.CardView>

    <TextView
        android:id="@+id/textView27"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="+ Add fund to your account "
        android:textColor="#00ce7c"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@+id/cardView"
        app:layout_constraintStart_toStartOf="@+id/cardView"
        app:layout_constraintTop_toBottomOf="@+id/cardView" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/productsRv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/guideline2"
        app:spanCount="1"
        tools:listitem="@layout/row_product" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="200dp" />

</android.support.constraint.ConstraintLayout>