2013-05-02 46 views
0

我是新來的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

回答

2

您可以添加列表的兩個上tablelayout您認爲作爲一個ListViewHeader View兩個tablelayout下面的列表視圖列表視圖的footer view

事情是這樣的:

頭查看:

ListView.addHeaderView(HeaderView); 

頁腳視圖:

ListView.addFooterView(FooterView); 

取出scrollview。現在你的佈局將會隨着列表一起滾動而不需要任何scrollview

+0

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

1

你不能在ScrollView裏使用ListView,除非你實現你自己的Scrollview並處理觸摸事件。

我看到你希望整個頁面可以滾動,這種方式當你向下滾動表格時最終會走出屏幕。爲了達到此目的,您需要

  1. 刪除ScrollView。
  2. 將你的表放在另一個xml文件中。
  3. 在你的列表視圖中使用addHeaderView(View v)方法,並將該表添加到列表視圖。

    listview.addeaderView(tableView); 
    
0

根據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; 
     } 
    }); 

我希望這會幫助你。

0

將線性佈局的方向設置爲垂直。

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