2015-05-05 64 views
3

我使用的GridView裏面scrollview(我知道不好的做法,但要求是這樣的)。問題在於它們是GridView結尾處不需要的額外垂直空間。我也通過這個代碼動態地設置GridView的高度。 coulmns的額外的空間雖然向ScrollView添加網格視圖

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

     int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED); 
     int totalHeight = 0; 
     View view = null; 
     for (int i = 0; i < listAdapter.getCount(); i++) { 
      view = listAdapter.getView(i, view, listView); 
      view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); 
      totalHeight += view.getMeasuredHeight(); 
     } 
     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalHeight; 
     listView.setLayoutParams(params); 
     listView.requestLayout(); 
    } 

我的XML是這樣的

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

    <include 
     android:id="@+id/header_section" 
     layout="@layout/header_screen" /> 


    <ScrollView 
     android:id="@+id/parent_scroll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/white" 
     android:layout_alignWithParentIfMissing="false" 

     android:layout_below="@+id/header_section"> 

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


       <FrameLayout 
        android:id="@+id/frame" 
        android:layout_width="match_parent" 
        android:layout_height="200dp" /> 





      <ListView 
       android:id="@+id/list" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:dividerHeight="1dp" 
       ></ListView> 

      <TextView 
       android:id="@+id/more" 
       android:layout_width="match_parent" 
       android:layout_height="20dp" 
       android:background="@android:color/darker_gray" 
       android:gravity="center" 
       android:textColor="@android:color/white" 
       android:textSize="14sp" 
       android:visibility="gone" /> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="@android:color/darker_gray" /> 

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

       <TextView 
        android:id="@+id/txt" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="3dp" 

        android:textColor="@android:color/black" 
        android:textSize="14sp" /> 

       <ImageView 
        android:id="@+id/icon" 
        android:layout_width="50dp" 
        android:layout_height="50dp" 
        android:layout_below="@+id/txt" 
        android:scaleType="fitXY" 
        android:layout_margin="5dp" 
        /> 

       <TextView 
        android:id="@+id/name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/txt" 
        android:layout_margin="5dp" 
        android:layout_marginLeft="3dp" 
        android:layout_toRightOf="@+id/icon" 
        android:textColor="@android:color/black" 
        android:textSize="14sp" /> 

       <TextView 
        android:id="@+id/demo_demo" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/name" 
        android:layout_marginBottom="3dp" 
        android:layout_marginLeft="5dp" 
        android:layout_toRightOf="@+id/icon" 
        android:textColor="@android:color/black" 
        android:textSize="12sp" /> 
      </RelativeLayout> 

      <View 
       android:layout_width="match_parent" 
       android:layout_height="1dp" 
       android:background="@android:color/darker_gray" /> 

       <GridView 
        android:id="@+id/items_list" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:numColumns="2" /> 

     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

<include layout="@layout/nav_drawer" /> 

+1

嘗試將您的ScrollView內容添加到GridView標題。 –

+0

你可以添加演示來展示這是如何完成的嗎? –

+0

在你的滾動視圖中使用android:fillViewPort =「true」 – Iqbal

回答

3

製作增量根據列在網格視圖的數量,在這裏我假設數量2所以只需在你的循環中增加+2點

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

     int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED); 
     int totalHeight = 0; 
     View view = null; 
     for (int i = 0; i < listAdapter.getCount(); i = i+2) { 
      view = listAdapter.getView(i, view, listView); 
      view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED); 
      totalHeight += view.getMeasuredHeight(); 
     } 
     ViewGroup.LayoutParams params = listView.getLayoutParams(); 
     params.height = totalHeight; 
     listView.setLayoutParams(params); 
     listView.requestLayout(); 
    } 
+1

感謝Abhishek這工作。 –