Android - Listview不在ScrollView下滚动

时间:2014-03-08 03:19:20

标签: android listview scrollview android-linearlayout

这是我的xml文件,显示了linerlayout和scrollview下的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>

1 个答案:

答案 0 :(得分:1)

您不应将ListView放在ScrollView下。 ListView自行滚动。相反,您可以将视图作为页眉或页脚添加到listview。

引用文档

  

你永远不应该使用带有ListView的ScrollView,因为ListView   负责自己的垂直滚动。最重要的是,这样做   击败ListView中的所有重要优化以进行处理   使用大型列表,因为它有效地强制ListView显示   它的整个项目列表,用于填充由提供的无限容器   滚动型。

您还可以使用RelativeLayout。只有ListView滚动EditText和按钮不滚动。修改以下相应的

<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>