2017-11-20 10 views
0

私は親レイアウトとしてcardviewを持っています。そのカードビューの中で私はアイテムのリストビューを膨らませています。私は今、cardviewの固定されたheigthを持っていますが、私は動的にlistviewの項目に基づいて高さを調整したい。リストビュー内のアイテムに基づいてカードビューの高さを調整します

これは私の親xmlです:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 


    <android.support.constraint.ConstraintLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:id="@+id/shelfShareLinearlayout" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="10dp" 
      android:layout_marginStart="10dp" 
      android:padding="20dp"> 



     </LinearLayout> 

    </android.support.constraint.ConstraintLayout> 
</ScrollView> 

これは私の子ビューのXMLです:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:id="@+id/shelfShareMasterLL" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/frontalRodShareCardView" 
     android:layout_width="585dp" 
     android:layout_height="500dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginStart="8dp" 
     android:layout_marginTop="15dp" 
     app:cardElevation="5dp" 
     app:cardUseCompatPadding="true" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"> 

     <RelativeLayout 
      android:id="@+id/shelfShareMasterRL" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <Button 
       android:id="@+id/deleteSlefShareCard" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_alignParentRight="true" 
       android:layout_marginRight="40dp" 
       android:layout_marginTop="10dp" 
       android:visibility="invisible" 
       android:background="@drawable/round_corner" 
       android:drawableTop="@drawable/ic_delete_black_24dp" 
       android:paddingTop="10dp" /> 

      <TextView 
       android:id="@+id/shelfShareHeadingTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_marginLeft="20dp" 
       android:layout_marginTop="20dp" 
       android:text="Frontal Rod Share" 
       android:textSize="20sp" /> 

      <CheckBox 
       android:id="@+id/noFrontalRodShareCheck" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignBottom="@+id/shelfShareHeadingTextView" 
       android:visibility="invisible" 
       android:layout_toLeftOf="@+id/deleteSlefShareCard" 

       android:layout_marginRight="20dp" 
       android:text="No Frontal Rod Share" /> 

      <ListView 
       android:id="@+id/shelfShareProductListView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/shelfShareHeadingTextView"> 

      </ListView> 

     </RelativeLayout> 

    </android.support.v7.widget.CardView> 

</LinearLayout> 

、これは私が子ビューを膨張させると、それにリストを表示しています方法です:

ArrayList<Products> shelfShareList = new ArrayList<Products>(); 
final LinearLayout shelfShareLL = (LinearLayout) view.findViewById(R.id.shelfShareLinearlayout); 
final View shelfShareChildView = getActivity().getLayoutInflater().inflate(R.layout.cardviewshelfshare, null); 
shelfShareLL.addView(shelfShareChildView); 

ListView listView = (ListView) shelfShareChildView.findViewById(R.id.shelfShareProductListView); 

Cursor crs = database.rawQuery(//My query, null); 

    while (crs.moveToNext()){ 

    shelfShareList.add(//data from query); 

    } 

listView.setAdapter(new ProductsAdapter(getActivity(), shelfShareList)); 

crs.close(); 
+0

カードビューと内部の相対レイアウトの両方でheightを 'wrap_content'として使用しようとしましたか? –

+0

私はすでにこれを試しましたが、私のためにはうまくいきませんでした。 –

+0

あなたはcardview、relativelayout、listview heightをwrap_content – Lucky

答えて

0

子ビューxmlをプログラムで親に追加しないで、これを親xmlに追加します。

<include 
    layout="@layout/layout_name_of_your_child" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

そして、子のレイアウトでは、ルートLinearLayoutを削除し、ルートとしてCardViewを作ります。 CardViewlayout_heightwrap_contentに変更してください。

次に、子xmlファイルのListViewlayout_heightwrap_contentに変更します。これはうまくいくはずです!

関連する問題