可滚动父版面内的不可滚动ListView

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了可滚动父版面内的不可滚动ListView相关的知识,希望对你有一定的参考价值。

使用android Studio,我的布局如下:

enter image description here

底部的三个列表项是ListView对象,它们都在RelativeLayout对象的内部,而在ScrollView对象的内部。我希望三个列表视图对象的布局高度可以包装内容,并且不能滚动。如果三个列表对象的高度超出屏幕底部,我希望RelativeLayout成为可滚动而不是ListView对象。

enter image description here

如果您愿意,这是此活动的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:padding="@dimen/root_padding"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CourseViewActivity">

        <TextView
            android:id="@+id/title_display"
            android:text="@string/course_view"
            android:textSize="@dimen/header_text_size"
            android:layout_toLeftOf="@id/edit_course_button"
            android:layout_toStartOf="@id/edit_course_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/date_display"
            android:text="@string/start_and_end_dates"
            android:layout_toLeftOf="@id/edit_course_button"
            android:layout_toStartOf="@id/edit_course_button"
            android:layout_below="@id/title_display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/mentor_display"
            android:text="@string/course_mentor_display_default"
            android:layout_toLeftOf="@id/edit_course_button"
            android:layout_toStartOf="@id/edit_course_button"
            android:layout_below="@id/date_display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/credits_display"
            android:text="@string/course_credits_display_default"
            android:layout_toLeftOf="@id/edit_course_button"
            android:layout_toStartOf="@id/edit_course_button"
            android:layout_below="@id/mentor_display"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/passed_display"
            android:text="@string/passed"
            android:textColor="#128039"
            android:textStyle="bold"
            android:textSize="@dimen/sub_header_text_size"
            android:visibility="gone"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_above="@id/separator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/edit_course_button"
            android:text="@string/edit_course"
            android:onClick="editCourse"
            android:background="@color/blue_button_bg"
            android:textColor="@color/blue_button_fg"
            android:padding="@dimen/button_padding"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/separator"
            android:layout_below="@id/credits_display"
            android:background="#333"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:layout_width="match_parent"
            android:layout_height="2dp" />

    <ScrollView
        android:layout_below="@id/separator"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/objective_header"
            android:text="@string/objectives"
            android:textSize="@dimen/sub_header_text_size"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/create_objective_button"
            android:onClick="createNewObjective"
            android:src="@drawable/ic_add"
            android:layout_alignTop="@+id/objective_header"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/objective_list"
            android:layout_below="@id/objective_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/assessment_header"
            android:text="@string/assessments"
            android:textSize="@dimen/sub_header_text_size"
            android:layout_below="@id/objective_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/create_assessment_button"
            android:onClick="createNewAssessments"
            android:src="@drawable/ic_add"
            android:layout_alignTop="@+id/assessment_header"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/assessment_list"
            android:layout_below="@id/assessment_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/note_header"
            android:text="@string/notes"
            android:textSize="@dimen/sub_header_text_size"
            android:layout_below="@id/assessment_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/create_note_button"
            android:onClick="createNewNote"
            android:src="@drawable/ic_add"
            android:layout_alignTop="@+id/note_header"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/note_list"
            android:layout_below="@id/note_header"
            android:layout_marginBottom="10dp"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </RelativeLayout>

    </ScrollView>

</RelativeLayout>

编辑:该项目必须是API 15,这意味着我不能使用RecyclerView之类的东西。

答案

解决方案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:padding="@dimen/_5sdp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/relativeLayout">

    <TextView
        android:id="@+id/title_display"
        android:text="@string/course_view"
        android:textSize="@dimen/header_text_size"
        android:layout_toLeftOf="@id/edit_course_button"
        android:layout_toStartOf="@id/edit_course_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/date_display"
        android:text="@string/start_and_end_dates"
        android:layout_toLeftOf="@id/edit_course_button"
        android:layout_toStartOf="@id/edit_course_button"
        android:layout_below="@id/title_display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/mentor_display"
        android:text="@string/course_mentor_display_default"
        android:layout_toLeftOf="@id/edit_course_button"
        android:layout_toStartOf="@id/edit_course_button"
        android:layout_below="@id/date_display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/credits_display"
        android:text="@string/course_credits_display_default"
        android:layout_toLeftOf="@id/edit_course_button"
        android:layout_toStartOf="@id/edit_course_button"
        android:layout_below="@id/mentor_display"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/passed_display"
        android:text="@string/passed"
        android:textColor="#128039"
        android:textStyle="bold"
        android:textSize="@dimen/sub_header_text_size"
        android:visibility="gone"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_above="@id/separator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/edit_course_button"
        android:text="@string/edit_course"
        android:onClick="editCourse"
        android:background="@color/blue_button_bg"
        android:textColor="@color/blue_button_fg"
        android:padding="@dimen/button_padding"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/separator"
        android:layout_below="@id/credits_display"
        android:background="#333"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="2dp" />

    </RelativeLayout>

    <ScrollView
        android:layout_below="@id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/objective_header"
                android:text="@string/objectives"
                android:textSize="@dimen/sub_header_text_size"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/create_objective_button"
                android:onClick="createNewObjective"
                android:src="@drawable/ic_add"
                android:layout_alignTop="@+id/objective_header"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ListView
                android:id="@+id/objective_list"
                android:layout_below="@id/objective_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/assessment_header"
                android:text="@string/assessments"
                android:textSize="@dimen/sub_header_text_size"
                android:layout_below="@id/objective_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/create_assessment_button"
                android:onClick="createNewAssessments"
                android:src="@drawable/ic_add"
                android:layout_alignTop="@+id/assessment_header"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ListView
                android:id="@+id/assessment_list"
                android:layout_below="@id/assessment_header"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/note_header"
                android:text="@string/notes"
                android:textSize="@dimen/sub_header_text_size"
                android:layout_below="@id/assessment_list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <ImageView
                android:id="@+id/create_note_button"
                android:onClick="createNewNote"
                android:src="@drawable/ic_add"
                android:layout_alignTop="@+id/note_header"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <ListView
                android:id="@+id/note_list"
                android:layout_below="@id/note_header"
                android:layout_marginBottom="10dp"
                android:scrollbars="none"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>

</RelativeLayout>

我希望对您有用

另一答案

您的xml代码正确,但是需要一些更改,请检查以下代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".SwipeRefreshLayoutActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/title_display"
                android:text="@string/course_view"
                android:layout_toLeftOf="@id/edit_course_button"
                android:layout_toStartOf="@id/edit_course_button"
                android:layout_width="ma

以上是关于可滚动父版面内的不可滚动ListView的主要内容,如果未能解决你的问题,请参考以下文章

flutter 实现不可滚动的ListView构建器

一个 ListView 在 AnimatedCrossFade 中不可滚动

ScrollView 内不可滚动的 ListView

iOS 移动设备:在可滚动父级内的 iFrame 中滚动

Flutter:ListView内的ListView不滚动

颤动中滚动视图内的Listview