2015-10-23 123 views
1

我想在我的UI的底部實現一個嵌套的ListView,但我的數組中只有一個項目正在填充屏幕。我已經知道,如果我設置了我的高度屬性(即「175dp」代替「Wrap_Content」),我可以顯示所有項目。問題是列表大小不是靜態的,所以我需要在運行時調整我的ListView的高度。在運行時更改ListView高度

我已經挖過了互聯網和StackOverflow這裏,但我似乎無法找到解決方案。

謝謝!從我的片段

代碼:

for (int i = 0; i < values.length; ++i) { 
    list.add(values[i]); 
} 

final StableArrayAdapter adapter = new StableArrayAdapter(rootView.getContext(), 
     android.R.layout.simple_list_item_1, list); 
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) rootView.findViewById(R.id.list_linear).getLayoutParams(); 
LinearLayout manager = (LinearLayout) rootView.findViewById(R.id.list_linear); 
lp.height = 45 * values.length; 
manager.setLayoutParams(lp); 

listView.setAdapter(adapter); 

我的XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#BBDEFB" 
android:padding="3dp" 
android:id="@+id/movie_description_container"> 

<ImageView 
    android:layout_width="match_parent" 
    android:layout_height="225dp" 
    android:id="@+id/movie_detail_hero_image" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentLeft="true" 
    android:gravity="top" 
    android:src="@drawable/sample_1"/> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="movie title" 
    android:id="@+id/movie_detail_title" 
    android:layout_alignBottom="@+id/movie_detail_hero_image" 
    android:backgroundTint="@color/background_floating_material_dark" 
    android:textSize="36dp" 
    android:textColor="#BBDEFB" 
    android:background="#a3000000" 
    android:layout_above="@id/movie_detail_hero_image" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginLeft="3dp" 
    android:layout_marginRight="3dp" 
    android:paddingLeft="18dp"/> 

<ImageView 
    android:layout_width="175dp" 
    android:layout_height="215dp" 
    android:id="@+id/movie_detail_poster" 
    android:src="@drawable/sample_0" 
    android:layout_below="@+id/movie_detail_title" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_margin="12dp" 
    android:gravity="left"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="date" 
    android:textSize="22dp" 
    android:id="@+id/movie_detail_date" 
    android:layout_centerVertical="true" 
    android:layout_below="@id/movie_detail_hero_image" 
    android:layout_alignRight="@+id/movie_detail_hero_image" 
    android:layout_marginRight="12dp" 
    android:layout_marginTop="32dp"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Rating:" 
    android:textSize="22dp" 
    android:id="@+id/movie_detail_rating" 
    android:layout_below="@+id/movie_detail_date" 
    android:layout_alignRight="@+id/movie_detail_date" 
    android:layout_alignEnd="@+id/movie_detail_date" 
    android:layout_marginTop="6dp" /> 

<RatingBar 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/ratingBar" 
    android:numStars="4" 
    android:nestedScrollingEnabled="true" 
    android:maxWidth="77dp" 
    android:layout_below="@+id/movie_detail_rating" 
    android:layout_alignRight="@+id/movie_detail_rating" 
    android:layout_alignEnd="@+id/movie_detail_rating" 
    android:layout_marginTop="3dp"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Synopsis" 
    android:id="@+id/movie_detail_synopsis_header" 
    android:layout_below="@+id/movie_detail_poster" 
    android:layout_alignLeft="@+id/movie_detail_poster" 
    android:layout_alignStart="@+id/movie_detail_poster" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="description" 
    android:id="@+id/movie_detail_synopsis" 
    android:layout_below="@+id/movie_detail_synopsis_header" 
    android:layout_alignLeft="@+id/movie_detail_synopsis_header" 
    android:layout_alignStart="@+id/movie_detail_synopsis_header" 
    android:layout_margin="6dp"/> 

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageButton_Favorite" 
    android:layout_below="@+id/ratingBar" 
    android:layout_alignRight="@+id/ratingBar" 
    android:src="@drawable/ic_favorite_white_24dp" 
    android:maxWidth="45dp" 
    android:maxHeight="45dp" 
    /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/list_linear" 
    android:layout_below="@id/movie_detail_synopsis"> 
    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="45dp" 

     android:id="@+id/list_view2"/> 
</LinearLayout> 
+0

你的ListView的高度設置爲奇爾德斯的大小沒有爲包含列表視圖的LinearLayout設置方向。默認是水平的。此外,列表視圖是可滾動的。您不必爲了騰出更多物品而騰出更高的名單。 –

回答

0

其實你可以用這種方法

public static void setListViewHeightBasedOnChildren(ListView listView) { 
    ListAdapter listAdapter = listView.getAdapter(); 
    if (listAdapter == null) 
     return; 

    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), 
      MeasureSpec.UNSPECIFIED); 
    int totalHeight = 0; 
    View view = null; 
    for (int i = 0; i < listAdapter.getCount(); i++) { 
     view = listAdapter.getView(i, view, listView); 
     if (i == 0) 
      view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, 
        LayoutParams.WRAP_CONTENT)); 

     view.measure(desiredWidth, MeasureSpec.UNSPECIFIED); 
     totalHeight += view.getMeasuredHeight(); 
    } 
    ViewGroup.LayoutParams params = listView.getLayoutParams(); 
    params.height = totalHeight 
      + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
    listView.setLayoutParams(params); 
    listView.requestLayout(); 
} 
相關問題