2012-06-08 104 views
1

在這裏我使用scrollview在我的linearlayout.But我想包括我的scrollview linearlayout內的另一個列表視圖。如何解決scrollview listview在linearlayout android

Scrollview支持ony一個child.I知道在scrollview列表視圖不工作。但有沒有其他選項使用listview.I想滾動所有的內容。

<?xml version="1.0" encoding="utf-8"?> 
    <ScrollView 
     android:id="@+id/widget54" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     > 
     <LinearLayout 
      android:layout_width="310px" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      > 
      <Button 
       android:id="@+id/button1" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Button 1" 
       /> 
      <Button 
       android:id="@+id/button2" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Button 2" 
       /> 
      <Button 
       android:id="@+id/button3" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Button 3" 
       /> 
      <EditText 
       android:id="@+id/txt" 
       android:layout_width="fill_parent" 
       android:layout_height="300px" 
       /> 
      <Button 
       android:id="@+id/button4" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="Button 4" 
       /> 

     </LinearLayout> 
    </ScrollView> 
+0

你可以看到這一點: http://stackoverflow.com/questions/3495890/how-can-i-put-a-listview-into-a -scrollview - 沒有 - 它壓扁 – user3682351

回答

1

ScrollView裏面的ListView肯定不能工作。

我能想到的唯一方法,就是你只有一個列表視圖中的接口,並創建一個自定義適配器它:

public class HeaderAdapter{ 
    private Adapter mAdapter; // this adapter is the adapter that you will normally have, for your listview 

    getViewTypeCount(){ 
     return mAdapter.getViewTypeCount() + 1; 
    } 

    getView(int pos, View convertView){ 
     // Handle only the first position, othervise, let mAdapter return 
     if (pos!=0) return mAdapter.getView(pos-1, convertView); 

     // 
     View v = LayoutInflater.inflate(R.layout.other_linear_layout); 
     // Bind the v if necessary 

     return v; 
    } 
} 

上面只是一個例子,想法是你把剩下的的內容(linearlayouts,buttons,edittext等)作爲適配器的「Header」行。

0

如果在ScrollView中只有一個ListView int,則可以在佈局XML中向ScrollView添加一個屬性:android:fillViewport="true"

這將展開ListView的所有內容。

否則,如果在ScrollView中有多個ListView或GridView,則需要創建一個customListView。

擴展ListView和@覆蓋onMeasure:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, 
       MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, expandSpec); 
}