Android中的ListView Scrollable

时间:2013-10-02 07:17:20

标签: android listview

我在制作ListView Scrollable时遇到了问题。我知道listview本身是可滚动的。但我必须放入..请告诉我如何才能使listView Scrollable ....非常感谢

 <?xml version="1.0" encoding="utf-8"?>

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

<RelativeLayout
android:layout_width="fill_parent"
android:focusable="false"
android:focusableInTouchMode="true"



  android:layout_height="597dp"
  android:orientation="vertical" >

   <TextView
    android:id="@+id/TextViewMob"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/textViewCNIC"
    android:layout_below="@+id/textViewCNIC"
    android:layout_marginTop="20dp"
    android:text="Customer Mobile No."
    android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
    android:id="@+id/TextViewName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/TextViewMob"
    android:layout_below="@+id/editTextMob"
    android:layout_marginTop="20dp"
    android:text="Customer Name"
    android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
    android:id="@+id/TextViewAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignRight="@+id/TextViewName"
    android:layout_below="@+id/TextViewName"
    android:layout_marginTop="20dp"
    android:text="Mailing Address"
    android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
    android:id="@+id/EditTextAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/TextViewAdd"
    android:layout_marginTop="20dp"
    android:layout_toRightOf="@+id/TextViewAdd"
    android:ems="10"
    android:inputType="textMultiLine" />

    <TextView
    android:id="@+id/TextViewKin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/TextViewAdd"
    android:layout_marginTop="20dp"
    android:layout_toLeftOf="@+id/EditTextAdd"
    android:text="Customer&apos;s Next of Kin"
    android:textAppearance="?android:attr/textAppearanceMedium" />


    <EditText
    android:id="@+id/EditTextKin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/TextViewKin"

       android:layout_toRightOf="@+id/TextViewKin"
    android:layout_marginTop="20dp"
     android:ems="10"
       android:inputType="textMultiLine" />

 <EditText
  android:id="@+id/editTextName"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_above="@+id/EditTextAdd"
  android:layout_marginTop="20dp"
  android:layout_toRightOf="@+id/TextViewName"
  android:ems="10" >


   </EditText>

  <TextView
  android:id="@+id/textViewPhoto"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_below="@+id/TextViewKin"
  android:layout_marginTop="20dp"
  android:layout_toLeftOf="@+id/EditTextKin"
  android:text="Attach File"
  android:textAppearance="?android:attr/textAppearanceMedium" />

  <Button
  android:id="@+id/buttonCamera"
  style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBaseline="@+id/textViewPhoto"
  android:layout_alignBottom="@+id/textViewPhoto"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="20dp"
  android:text="From Camera" />

  <Button
  android:id="@+id/buttonAttach"
  style="?android:attr/buttonStyleSmall"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBaseline="@+id/buttonCamera"
  android:layout_alignBottom="@+id/buttonCamera"
  android:layout_marginLeft="18dp"
  android:layout_marginTop="20dp"
  android:layout_toRightOf="@+id/buttonCamera"
  android:text="From Sdcard" />



 <TextView
  android:id="@+id/textViewCNIC"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true"
  android:layout_marginTop="65dp"
  android:layout_toLeftOf="@+id/buttonCamera"
  android:text="Customer CNIC"
  android:textAppearance="?android:attr/textAppearanceMedium" />

  <EditText
  android:id="@+id/EditTextCNIC"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignBaseline="@+id/textViewCNIC"
  android:layout_alignBottom="@+id/textViewCNIC"
  android:layout_toRightOf="@+id/textViewCNIC"
  android:ems="10"
  android:inputType="number" >


  </EditText>

  <EditText
  android:id="@+id/editTextMob"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignLeft="@+id/EditTextCNIC"
  android:layout_alignTop="@+id/TextViewMob"
  android:ems="10"
  android:hint="Enter Ufone No. only"
  android:inputType="phone" />

  <Button
  android:id="@+id/buttonSubmit"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignRight="@+id/EditTextKin"
  android:layout_marginBottom="43dp"
  android:layout_marginRight="26dp"
  android:text="Save and Open Account" />

  <ImageView
  android:id="@+id/imageView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentLeft="true"
  android:layout_marginLeft="19dp"
  android:src="@drawable/cr_l0_button" />

  <ListView
  android:id="@+id/listView1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_above="@+id/EditTextKin"
  android:layout_alignParentLeft="true">


  </ListView>

</RelativeLayout>

</ScrollView>

我也试过这个:(可能有问题)

         setListViewHeightBasedOnChildren(listviewattachment);//method call inonCreate  
    private  void setListViewHeightBasedOnChildren(ListView listView) {
    // TODO Auto-generated method stub
     ListAdapter listAdapter = listView.getAdapter(); 
     if (listAdapter == null) {
         // pre-condition
         return;
     }

     int totalHeight = 0;
     for (int i = 0; i < listAdapter.getCount(); i++) {
         View listItem = listAdapter.getView(i, null, listView);
         listItem.measure(0, 0);
         totalHeight += listItem.getMeasuredHeight();
     }

     ViewGroup.LayoutParams params = listView.getLayoutParams();
     params.height = totalHeight + (listView.getDividerHeight() *     (listAdapter.getCount() - 1));
     listView.setLayoutParams(params);

}

2 个答案:

答案 0 :(得分:1)

您不应在ListView内使用ScrollView因为ListView类实现了自己的滚动行为。因此,接收手势事件会有问题,因为它们都由父ScrollView处理。也许你可以重新考虑/重新设计你的布局,以避免这个解决方案。

答案 1 :(得分:0)

如果观看次数/字段数是静态的,只需删除ListView并将字段添加到ScrollView子节点,该子节点不会滚动,因为您为{{1}设置了固定高度};尝试只设置RelativeLayout

wrap_content

其他提示.-

  • <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:focusable="false" android:focusableInTouchMode="true"> 已弃用,请改用fill_parent
  • match_parent没有RelativeLayout属性,但实际上您应该将orientation替换为RelativeLayout垂直方向,这样您就可以删除所有LinearLayout / { {1}} atributes。