首页 > 解决方案 > 使用 ListView 使 LinearLayout 可滚动

问题描述

我有一个看起来像这样的线性布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    tools:context=".activities.EnrolStudentFormActivity">

    <include
        layout="@layout/app_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:fitsSystemWindows="true" />

    <ListView
        android:id="@+id/list_view
        android:layout_width="match_pare
        android:layout_height="wrap_cont
        android:divider="@null"
        android:dividerHeight="0dp" />

</LinearLayout>

中间有几个ImageViews,TextInputLayouts和TextInputEditTexts。我想让这个布局可滚动。ScrollView我试图用and使它可以滚动NestedScrollView,但他们都搞砸ListView了。

我也在ListView动态更改内容,并且TextInputEditText在. 调整大小并没有多大帮助,我无法单击. 似乎捕获了触摸输入。TextInputLayoutListViewListViewTextInputEditTextListViewScrollView

是否有任何解决方法可以让我拥有一个ListView内部 aScrollView或使线性布局可滚动,同时使TextInputEditText内部ListView可用?

标签: javaandroidlistviewscrollviewandroid-linearlayout

解决方案


使用RecyclerView代替ListViewNestedScrollView在顶部添加:

<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="
http://schemas.android.com/apk/res/android"
xmlns:http="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"
    tools:context=".activities.EnrolStudentFormActivity">

    <include
        layout="@layout/app_bar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:fitsSystemWindows="true" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@null"
        android:dividerHeight="0dp" />>

</LinearLayout>

最后在您的代码中添加:

RecyclerView.setNestedScrollingEnabled(false);

推荐阅读