2016-10-03 8 views
-4

ListViewheightListViewからwrap_contentまたはmatch_parentに設定した場合、これは私のコードです。しかし、高さをdpで定義すると、私には両方ともListViewと表示され始めます。しかし、その場合は良く見えません。2リストビューを追加する方法

<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

'ScrollView'の代わりに' NestedScrollView'を試してみてください –

答えて

1

1つのレイアウトファイルに1つのListViewがあることを常に推奨します。しかし、はい、2つのリストを合わせる必要がある場合があります。その場合、両方のリストを単一のArrayListにマージして、単一のListViewで表示できるようにすることをお勧めします。

ListViewを複数のレイアウトで表示する方法は他にもあります。 ScrollViewの代わりにNestedScrollViewを使用してください。

RecyclerViewhere's an implementationも複数のリストを同じRecyclerViewで表示することを検討する場合は、 code sectionでプロジェクトを見つけることができ、wikiもよく文書化されています。

+0

ありがとう、ListListの両方のリスト項目を表示する 'NestedListView'を使用しようとしましたが、リスト項目のサイズが縮小され、listViewに1項目しか表示されません。 'wrap_content'と' match_parent'の両方を使ってみました。私が定義した場合、 '200dp'と言うと、2つのアイテムが表示されますが、リストビューにはスクロールビューがありません。 – Kirmani88

+0

' NestedListView'ではなく、 'NestedScrollView'内の' ListView'です。とにかく、あなたは 'RecyclerView'実装のために行くことをお勧めします。シンプルで簡単です。 –

0

プットでframeLayoutでListViewコントロールとスクロールビューの下にリストビューを使用して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

にlayout_weight設定はその後も、あなたが同じを使用したい場合は良い習慣ではありません。スクロールビューの下のリストビューはうまく動かず、リストビューの高さを文法的に管理しなければならないことに注意しなければなりません。両方のリストビューでアダプタを設定すると、この問題が発生します。同決議は、私は下のリンクを見ている以下の通りです: -

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); 
    } 
関連する問題