Scrollview不适用于Cardview

时间:2016-12-24 17:32:57

标签: android xml android-layout scrollview nestedscrollview

我一直在研究应用程序,但是卡在xml布局问题中,当我在线性布局中使用卡片视图时,我通常使用的scrollview无效,我在google中查找并​​发现了有关NestedScrollview的信息,它仍然无法工作,app力关闭,有人可以告诉我,我做错了什么,并且正确。这将非常有帮助。

  

activity_items_details.xml

<LinearLayout
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:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    app:cardElevation="5dp">

    <TextView
        android:id="@+id/tvItemDetailsName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="32sp"
        android:paddingTop="65dp"
        android:paddingBottom="65dp"
        tools:text="Item Name"
        android:textAllCaps="true"
        android:textStyle="bold" />

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


    <android.support.v7.widget.CardView>
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Qty"
                android:textSize="16sp"
                android:padding="10dp"
                android:textColor="@android:color/black"/>

            <TextView
                android:id="@+id/tvItemDetailsQty"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                tools:text="Qty:"
                android:textSize="18sp"
                android:layout_gravity="center"/>

        </LinearLayout>

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="PO #"
                android:textSize="16sp"
                android:padding="10dp"
                android:paddingRight="5dp"
                android:textColor="@android:color/black"/>

            <TextView
                android:id="@+id/tvItemDetailsPono"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                tools:text="PO #:"
                android:textSize="18sp"
                android:layout_gravity="center"/>

        </LinearLayout>

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Supplier"
                android:textSize="16sp"
                android:padding="10dp"
                android:paddingRight="5dp"
                android:textColor="@android:color/black"/>

            <TextView
                android:id="@+id/tvItemDetailsSupplier"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                tools:text="Supplier:"
                android:textSize="18sp"
                android:layout_gravity="center"/>

        </LinearLayout>

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_gravity="center">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Contact"
                android:textSize="16sp"
                android:padding="10dp"
                android:paddingRight="5dp"
                android:textColor="@android:color/black"/>

            <TextView
                android:id="@+id/tvItemDetailsContact"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                tools:text="Contact:"
                android:textSize="18sp"
                android:layout_gravity="center"/>

        </LinearLayout>


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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Transporter"
                android:textSize="16sp"
                android:padding="10dp"
                android:paddingRight="5dp"
                android:textColor="@android:color/black"/>

            <TextView
                android:id="@+id/tvItemDetailsTransport"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                tools:text="Transporter:"
                android:textSize="18sp"
                android:layout_gravity="center"/>

        </LinearLayout>

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LR #"
                android:textSize="16sp"
                android:padding="10dp"
                android:paddingRight="5dp"
                android:textColor="@android:color/black"/>

            <TextView
                android:id="@+id/tvItemDetailsLrno"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                tools:text="LR #:"
                android:textSize="18sp"
                android:layout_gravity="center"/>

        </LinearLayout>

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

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="LR #"
                android:textSize="16sp"
                android:padding="10dp"
                android:paddingRight="5dp"
                android:textColor="@android:color/black"/>

            <TextView
                android:id="@+id/tvItemDetailsRemarks"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp"
                tools:text="Remarks:"
                android:textSize="18sp"
                android:layout_gravity="center"/>

        </LinearLayout>

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

</android.support.v4.widget.NestedScrollView>

3 个答案:

答案 0 :(得分:3)

如果您不想尝试使用RecyclerView,请在NestedScrollView之后放置LinearLayout,如下所示:

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>

答案 1 :(得分:1)

NestedScrollView内有多个子视图,ScrollViewNestedScrollView只能容纳一个孩子。

要修复您的布局,请将所有内容放入任何ViewGroup,例如LinearLayout / RelativeLayout,然后相应地安排您的观看次数

答案 2 :(得分:0)

你应该像这样实现它:

{{1}}

希望这个帮助