Android - Listview 不在 ScrollView 下滚动

标签 android listview scrollview android-linearlayout

这是我的 xml 文件,它在 linerlayout 和 scrollview 下显示 ListView 。我怎样才能使我的 ListView 滚动?因为我想要的是线性布局下的背景图像也在滚动。但是每当我想滚动 ListView 时,它都会滚动整个布局。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainScreenEntered" 
   >

     <LinearLayout
        android:layout_width="320dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@drawable/mainscreen"
         >  

     <EditText
        android:id="@+id/txtSearch"
        android:layout_width="220dp"
        android:layout_height="40dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="130dp"
        android:background="@drawable/white"
        android:height="10dp"
        android:singleLine="true"
        android:textSize="15sp"
        android:hint="@string/search"
      />    

     <Button
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_marginLeft="250dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="-41dp"
        android:background="@drawable/btnsearch"
      />

    <ListView
        android:id="@+id/lvEntries"
        android:layout_width="255dp"
        android:layout_height="300dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="41dp"
        />

    <Button
        android:layout_width="280dp"
        android:layout_height="55dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="21dp"
        android:background="@drawable/btnaddentry"
        android:onClick="goToAddEntryScreen"
      />

     </LinearLayout>
</ScrollView>

最佳答案

您不应该将 ListView 放在 ScrollView 下。 ListView 自己滚动。相反,您可以将 View 作为页眉或页脚添加到 ListView 。

引用文档

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling. Most importantly, doing this defeats all of the important optimizations in ListView for dealing with large lists, since it effectively forces the ListView to display its entire list of items to fill up the infinite container supplied by ScrollView.

您还可以使用 RelativeLayout。只有 ListView 滚动 EditText 和 Buttons 不滚动。相应地修改以下内容

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/button1"
        android:layout_marginTop="81dp"
        android:text="Button" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <ListView
        android:id="@+id/listView1"
        android:layout_above="@id/button1"
        android:layout_below="@id/button2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

    >
    </ListView>

</RelativeLayout>

关于Android - Listview 不在 ScrollView 下滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22264575/

相关文章:

android - 如何将字符串添加到 JsonArray

java - 丢失 debug.keystore,我可以更新 Android 应用程序或检索应用程序数据吗?

android - 使用背景容器在自定义 ListView 上加载动态数据

c# - ListView 自动调整大小以显示所有项目

scrollview - 著名的 : scrollview within scrollview

ios - 滚动不适用于 iOS 13 beta 上的 WKWebView

android - Android 音频编解码器

android - 哪个属性在 Android-Studio 2.2 中替换为 "relativePercent"

Android ListView - 项目准备好后如何执行代码?

android - 防止 ScrollView 占用太多高度