2016-10-03 82 views
-4

這是我的代碼,它只給我單個ListView,如果我設置heightListViewwrap_contentmatch_parent。但如果我根據dp定義高度,它會向我展示ListView。但在這種情況下它看起來不太好。如何在ScrollView中添加2個ListViews

<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" 
    android:fillViewport="true"> 

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

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:text="ListView1" 
      android:gravity="center_vertical" 
      android:paddingLeft="16dp" 
      android:layout_marginTop="45dp" 
      android:textColor="@color/white" 
      android:textStyle="bold" /> 

     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/lv_upcoming" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:divider="@null" /> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="0.5dp" 
      android:background="#ffffff" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:text="List Number 2" 
      android:gravity="center_vertical" 
      android:textColor="@color/white" 
      android:fontFamily="sans-serif-condensed" 
      android:textSize="18sp" 
      android:id="@+id/tvPrevious" 
      android:paddingLeft="16dp" 
      android:paddingRight="16dp" /> 

     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/list_view_2" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:divider="@null"/> 

    </LinearLayout> 
</ScrollView> 

請引導我如何在佈局中添加多個ListView

+1

你不能把內部列表視圖ScrollViews,從來沒有。不好用。這是一個非常糟糕的選擇。真的,想想另一種方式。 – Aspicas

+0

嘗試使用'NestedScrollView'而不是'ScrollView' –

回答

1

它始終建議在一個佈局文件中只有一個ListView。但是,是的,可能會出現某種情況,您需要適合兩個列表。所以在這種情況下,我想建議將這兩個列表合併爲一個ArrayList,以便它可以顯示在一個單獨的ListView中。

現在還有其他方法可以在佈局中顯示多個ListView。嘗試使用NestedScrollView而不是ScrollView

如果您考慮使用RecyclerViewhere's an implementation也在同一個RecyclerView中顯示多個列表。你可以在code section找到這個項目,這個wiki也是有據可查的。在FrameLayout裏

+0

謝謝,我試着使用'NestedListView',它顯示兩個列表項,但它減少了List Item的大小,並且在listView中只顯示1個項目。我嘗試使用'wrap_content'和'match_parent'兩者。如果我定義,比如說'200dp',它會顯示2個項目,但滾動視圖不再有在列表視圖 – Kirmani88

+0

不是'NestedListView' ..它實際上''ListView'內'NestedScrollView'。無論如何,我寧願建議你去使用'RecyclerView'實現。它簡單和容易。 –

0

認沽ListView和layout_weight設置爲1

<LinearLayout 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" 
    android:fillViewport="true" 
    android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:text="ListView1" 
      android:gravity="center_vertical" 
      android:paddingLeft="16dp" 
      android:layout_marginTop="45dp" 
      android:textStyle="bold" 
      android:textColor="@color/white"/> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <ListView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:id="@+id/lv_upcoming" 
       android:paddingLeft="4dp" 
       android:paddingRight="4dp" 
       android:divider="@null" 
       /> 
    </FrameLayout> 

    <View 
      android:layout_width="fill_parent" 
      android:layout_height="0.5dp" 
      android:background="#ffffff" /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:text="List Number 2" 
      android:gravity="center_vertical" 
      android:fontFamily="sans-serif-condensed" 
      android:textSize="18sp" 
      android:id="@+id/tvPrevious" 
      android:paddingLeft="16dp" 
      android:textColor="@color/white" 
      android:paddingRight="16dp" /> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1"> 

     <ListView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/list_view_2" 
       android:paddingLeft="4dp" 
       android:paddingRight="4dp" 
       android:divider="@null" 
      /> 
    </FrameLayout> 

</LinearLayout> 
0

下滾動視圖使用列表視圖是不是一個好的做法,然後也,如果你想使用相同的。然後你必須記住,在滾動視圖下的列表視圖不能很好地工作,你必須以編程方式管理列表視圖的高度。當您在兩個列表視圖上設置適配器時,您將遇到此問題。相同的分辨率下面給出我看到下面的鏈接: -

Android ListView rows in ScrollView not fully displayed - clipped

public static void setListViewHeightBasedOnChildren(ListView listView) { 
     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); 
    }