如何使用listview和table可滚动的屏幕?

问题描述:

我是新来的android.When我想使屏幕滚动,屏幕变得滚动,但内容不显示。屏幕包含一个列表视图和另一个tableview.This是我使用的代码。如何使用listview和table可滚动的屏幕?

 <?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="fill_parent" > 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" android:layout_height="fill_parent" 
android:stretchColumns="1" 
android:scrollbars="vertical" 
android:isScrollContainer="true" 
android:scrollbarAlwaysDrawVerticalTrack="true"> 




    <TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="70dp" android:layout_marginRight="3dp" 
    > 
    <TableRow android:layout_width="wrap_content" android:layout_marginTop="5dp" > 
    <ImageView 
    android:id="@+id/logo" 
    android:layout_width="90dp" 
    android:layout_height="90dp" 
    android:src="@drawable/logo" 
    android:layout_marginLeft="3dp" 

    /> 
    <ImageView 
    android:id="@+id/back" 
    android:layout_width="30dp" 
    android:layout_height="40dp" 
    android:src="@drawable/back" 
    android:layout_marginLeft="20dp" 
    android:layout_marginTop="20dp" 
    /> 

    <ImageView 
     android:id="@+id/timesheet" 
     android:layout_width="90dp" 
     android:layout_height="70dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="1dp" 
     android:src="@drawable/timesheet" /> 

    <ImageView 
    android:id="@+id/home" 
    android:layout_width="35dp" 
    android:layout_height="50dp" 
    android:src="@drawable/home" 
    android:layout_marginTop="17dp" 
    android:layout_marginLeft="25dp" 
    android:layout_marginRight="20dp" 

    /> 
    </TableRow> 

    </TableLayout> 


    <TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginTop="70dp" android:layout_marginLeft="5dp" android:layout_marginRight="8dp" 
    > 

    <TableRow android:layout_width="70dp" android:layout_height="100dp" android:layout_marginTop="10dp" android:background="#000000" > 
     <TextView 
     android:text="Timesheets for Approval" 
     android:layout_width="wrap_content" 
     android:layout_height="80dp" 
     android:textSize="18dp" 
     android:layout_marginTop="3dp" 
     android:textColor="#ffff" 
     android:layout_marginLeft="70dp" 
     />  
    </TableRow> 
    </TableLayout> 


<ListView android:layout_width="wrap_content" 
    android:id="@+id/listView1" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="120dp" 
    android:layout_marginLeft="5dp" 
    android:layout_marginRight="8dp" 
      /> 

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" android:layout_marginTop="415dp" android:layout_marginLeft="5dp" android:layout_marginRight="8dp" android:layout_width="400dp" android:layout_height="170dp" 
    android:stretchColumns="*" 
    android:background="@drawable/shape"> 

     <TableRow android:layout_width="fill_parent" android:layout_marginTop="2dp" android:layout_marginLeft="5dp" android:layout_marginRight="8dp" > 
      <TextView 
     android:text="Timesheet Details" 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     />  

     </TableRow> 

    <TableRow android:layout_width="fill_parent" android:layout_marginLeft="1dp"> 
     <TextView 
     android:text="Emp Name" 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="13dp" 

     />   
     <EditText android:text="EditText" android:id="@+id/emp_name" android:enabled="false" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="13dp" 
      ></EditText> 
     <EditText android:text="EditText" android:id="@+id/emp_code" android:enabled="false" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="13dp" 
      ></EditText> 
    </TableRow> 
    <TableRow android:layout_width="fill_parent" android:layout_marginLeft="1dp"> 

     <TextView 
     android:text="From Date" 
     android:id="@id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="13dp" 

     />   
     <EditText android:text="EditText" android:id="@+id/from_date" android:enabled="false" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="13dp" 
      ></EditText> 
    </TableRow> 
    <TableRow android:layout_width="fill_parent" android:layout_marginLeft="1dp"> 
     <TextView 
      android:text="To Date" 
      android:id="@id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="13dp" 

      />  
     <EditText android:text="EditText" android:id="@+id/to_date" android:enabled="false" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="13dp" 
      ></EditText> 
    </TableRow> 
    <TableRow android:layout_width="fill_parent" android:layout_marginLeft="1dp"> 
     <TextView 
      android:text="Target Hrs" 
      android:id="@id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="13dp" 

     /> 
      <EditText android:text="EditText" android:id="@+id/target_hours" android:enabled="false" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="13dp" 
       ></EditText> 
     <TextView 
      android:text="Actual Hrs" 
      android:id="@id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="13dp" 

      /> 
      <EditText android:text="EditText" android:id="@+id/actual_hours" android:enabled="false" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="13dp" 
       ></EditText> 
    </TableRow> 

</TableLayout> 
<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" android:layout_marginTop="600dp" android:layout_marginLeft="1dp" android:layout_width="400dp" android:layout_height="wrap_content" 
    > 

    <TableRow android:layout_width="fill_parent" android:layout_marginLeft="1dp"> 
<Button android:id="@+id/approve" android:layout_width="wrap_content" android:layout_height="22dp" android:background = "@drawable/approve" android:textColor="#ffff" android:layout_marginLeft="30dp" ></Button> 
<Button android:id="@+id/reject" android:layout_width="wrap_content" android:layout_height="22dp" android:background = "@drawable/reject" android:textColor="#ffff" android:layout_marginLeft="50dp" android:layout_marginRight="20dp" ></Button> 
<Button android:id="@+id/detail" android:layout_width="wrap_content" android:layout_height="22dp" android:background = "@drawable/details" android:textColor="#ffff" android:layout_marginLeft="30dp" android:layout_marginRight="30dp"></Button> 

</TableRow> 

</TableLayout> 

</LinearLayout> 
</ScrollView> 
+0

滚动视图不起作用,因为列表视图中已经包含了滚动型 – 2013-05-02 05:16:50

+0

列表视图是一个复杂的控制装置与滚动一些优化。如果将它放在滚动视图中,所有优化都将消失!所以考虑改变你的设计。 – Mbt925 2013-05-02 05:21:47

您可以添加列表的两个上tablelayout您认为作为一个ListViewHeader View两个tablelayout下面的列表视图列表视图的footer view

事情是这样的:

头查看:

ListView.addHeaderView(HeaderView); 

页脚视图:

ListView.addFooterView(FooterView); 

取出scrollview。现在你的布局将会随着列表一起滚动而不需要任何scrollview

+0

+1为好的答案 – 2013-05-02 05:37:40

你不能在ScrollView里使用ListView,除非你实现你自己的Scrollview并处理触摸事件。

我看到你希望整个页面可以滚动,这种方式当你向下滚动表格时最终会走出屏幕。为了达到此目的,您需要

  1. 删除ScrollView。
  2. 将你的表放在另一个xml文件中。
  3. 在你的列表视图中使用addHeaderView(View v)方法,并将该表添加到列表视图。

    listview.addeaderView(tableView); 
    

根据UI指导线和最佳实践,建议不要使用内滚动型滚动内容,做防止滚动内容的滚动。

当你把两个滚动视图android只是感到困惑哪个滚动视图被触摸。所以有时它无法提供触摸事件。

但是,如果您仍希望实现滚动功能,则可以通过使用特定视图的onTouch事件来进行管理。你需要相应地设计你的布局。

但即使该要求迫使你做出这样的布局。试试这个...

说的情况有点像这样......。

<RelativeLayout 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=".MainActivity" > 

<ScrollView 
    android:id="@+id/svssvMain" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignParentTop="true" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/black" 
      android:textSize="20sp" 
      android:layout_margin="10dp" 
      android:text="@string/tv_content"/> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="@android:color/black"/> 
     <ListView 
      android:id="@+id/svslvMain" 
      android:layout_width="fill_parent" 
      android:layout_height="100dp" 
      android:cacheColorHint="#00000000"/> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="@android:color/black"/> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/black" 
      android:textSize="20sp" 
      android:layout_margin="10dp" 
      android:text="@string/tv_content"/> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="@android:color/black"/> 
     <ScrollView 
      android:id="@+id/svssvchild" 
      android:layout_width="fill_parent" 
      android:layout_height="100dp"> 

      <TextView 
       android:id="@+id/svstvContent" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="20sp" 
       android:layout_margin="10dp" 
       android:text="@string/lipsum"/> 
     </ScrollView> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="@android:color/black"/> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:textColor="@android:color/black" 
      android:textSize="20sp" 
      android:layout_margin="10dp" 
      android:text="@string/tv_content"/> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="@android:color/black"/> 
     <ImageView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:src="@drawable/android"/> 
    </LinearLayout> 
</ScrollView> 

步骤1:提供唯一的ID到两个滚动视图。

第2步:在您的活动获得的两个Scrollview & ListView参考。

 m_lvMain = (ListView) findViewById(R.id.svslvMain); 
    m_svMain = (ScrollView) findViewById(R.id.svssvMain); 
    m_svchild = (ScrollView) findViewById(R.id.svssvchild); 

步骤3:现在设置触摸侦听ListviewScrollView

m_svMain.setOnTouchListener(new View.OnTouchListener(){ 
     public boolean onTouch(View p_v, MotionEvent p_event) 
     { 
      // Log.v(TAG, "PARENT TOUCH"); 
     findViewById(R.id.svslvMain).getParent().requestDisallowInterceptTouchEvent(false); 
      findViewById(R.id.svssvchild).getParent().requestDisallowInterceptTouchEvent(false); 
      return false; 
     } 
    }); 
    m_svchild.setOnTouchListener(new View.OnTouchListener(){ 
     public boolean onTouch(View p_v, MotionEvent p_event) 
     { 
      // Log.v(TAG, "CHILD TOUCH"); 
      // Disallow the touch request for parent scroll on touch of 
      // child view 
      p_v.getParent().requestDisallowInterceptTouchEvent(true); 
      return false; 
     } 
    }); 
    m_lvMain.setOnTouchListener(new View.OnTouchListener(){ 
     public boolean onTouch(View p_v, MotionEvent p_event) 
     { 
      // Log.v(TAG, "CHILD TOUCH"); 
      // Disallow the touch request for parent scroll on touch of 
      // child view 
      p_v.getParent().requestDisallowInterceptTouchEvent(true); 
      return false; 
     } 
    }); 

我希望这会帮助你。

将线性布局的方向设置为垂直。

android:orientation="vertical" 

和除去

xmlns:android="http://schemas.android.com/apk/res/android" 
从其他布局比主滚动视图

将你的listView换成另一个线性布局并指定它的高度。 当你使用scrollView的时候,为什么要加入

android:stretchColumns="1" 
android:scrollbars="vertical" 
android:isScrollContainer="true" 
android:scrollbarAlwaysDrawVerticalTrack="true" 

到你的线性布局。修改你的LinearLayout为:与ListView的预期

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical">