【问题标题】:unable to scroll cardview containing list views无法滚动包含列表视图的卡片视图
【发布时间】:2017-05-31 20:46:31
【问题描述】:

我有一个布局,其中包含 3 个卡片视图。其中一个具有静态内容和固定大小,而另外三个包含从列表视图加载的动态内容。

我在 3 个卡片视图中获得了可滚动的内容,但我希望列表视图在卡片视图中填充父视图,而卡片视图应该滚动。这是我的代码 sn-p

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    tools:context="com.example.animo.gita.activity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

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

            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                card_view:cardElevation="8dp">

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

                    <TextView
                        android:id="@+id/commiter_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="16sp"
                        tools:text="animo93" />

                    <TextView
                        android:id="@+id/days"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        tools:text="4 days ago" />

                    <TextView
                        android:id="@+id/commit_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        tools:text="Added File Fragment" />

                    <TextView
                        android:id="@+id/commit_desc"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="italic"
                        tools:text="33 changed files with 506 additions and 42 deletions." />
                </LinearLayout>

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

            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                card_view:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:text="FILES CHANGED"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <ListView
                        android:id="@+id/changed_files_list"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"></ListView>
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                card_view:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:text="FILES ADDED"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <ListView
                        android:id="@+id/added_files_list"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"></ListView>
                </LinearLayout>

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

            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                card_view:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:text="FILES RENAMED"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <ListView
                        android:id="@+id/renamed_files_list"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"></ListView>
                </LinearLayout>

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

        </LinearLayout>
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

  • 谁能给我一个答案?

标签: android android-layout listview android-cardview android-scrollview


【解决方案1】:

android.support.v4.widget.NestedScrollView 包裹您的CardView's 容器LinearLayout 以进行嵌套滚动。

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.animo.gita.activity.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                card_view:cardElevation="8dp">

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

                    <TextView
                        android:id="@+id/commiter_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="16sp"
                        tools:text="animo93" />

                    <TextView
                        android:id="@+id/days"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        tools:text="4 days ago" />

                    <TextView
                        android:id="@+id/commit_name"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        tools:text="Added File Fragment" />

                    <TextView
                        android:id="@+id/commit_desc"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textStyle="italic"
                        tools:text="33 changed files with 506 additions and 42 deletions." />
                </LinearLayout>

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

            <android.support.v7.widget.CardView 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                card_view:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:text="FILES CHANGED"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <ListView
                        android:id="@+id/changed_files_list"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

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

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                card_view:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:text="FILES ADDED"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <ListView
                        android:id="@+id/added_files_list"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                    </ListView>
                </LinearLayout>

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

            <android.support.v7.widget.CardView 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="8dp"
                android:layout_weight="1"
                card_view:cardElevation="8dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="8dp"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"

                        android:text="FILES RENAMED"
                        android:textSize="18sp"
                        android:textStyle="bold" />

                    <ListView
                        android:id="@+id/renamed_files_list"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">

                    </ListView>
                </LinearLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

【讨论】:

  • 它给出了“ScrollView can host only one direct child”错误
  • 我忘记在NestedScrollView 中添加LinearLayout。 ScrollView 必须只有一个直接子级。
  • 我也试过了,但这次卡片视图只是包装了内容并没有填满屏幕
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 2022-06-22
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
相关资源
最近更新 更多