2015-10-20 88 views
0

you can see gridview in the image which is not wrapping whole nestedscrollview夥計們請幫我我在這個代碼中被困住了,我需要一個崩潰的工具欄佈局,我也成功了,但是嵌套的scrollview裏面的gridview沒有包裝nestedscrollview的總空間,我嘗試了很多方式但失敗。嵌套滾動視圖gridview的大小問題

這是在幫我崩潰工具欄,在那裏我得到gridview的問題XML,

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/coordinatorLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.patelsanket.myalbum.activities.AlbumImageDisplayActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/app_bar_height" 
     android:fitsSystemWindows="true" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/toolbar_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <ImageView 
       android:id="@+id/headerImage1" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:contentDescription="@string/shows_header_image" 
       android:fitsSystemWindows="true" 
       android:scaleType="fitXY" 
       android:src="@drawable/cod" /> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/AppTheme.PopupOverlay" /> 

     </android.support.design.widget.CollapsingToolbarLayout> 

    </android.support.design.widget.AppBarLayout> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

<GridView 

     android:id="@+id/album_image_grid_view" 
     android:layout_width="match_parent" 
     android:layout_height="550dip" 
     android:choiceMode="multipleChoice" 
     android:horizontalSpacing="0.5dp" 
     android:numColumns="2" 
     android:paddingBottom="20dip" /> 

</RelativeLayout> 

</android.support.v4.widget.NestedScrollView> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" 
     android:src="@drawable/ic_add" 
     app:backgroundTint="#009688" 
     app:layout_anchor="@id/app_bar" 
     app:layout_anchorGravity="bottom|end|right" /> 

</android.support.design.widget.CoordinatorLayout> 

回答

2

使用RecyclerViewGridLayoutManager。由於Recycler視圖可以與Collasping bartoolbar兼容... 我希望它有幫助..

+0

對於未來的推薦人,它的工作原理,這應該被標記爲答案! –

+0

是的,這是一個正確的方法。但那些沒有使用recyclerview的人,那麼我的回答也會很好。 –

0

如果添加此代碼 (Build.VERSION.SDK_INT> = Build.VERSION_CODES.LOLLIPOP){ album_image_grid_view.setNestedScrollingEnabled(true); } 或者你可以使用RecyclerView代替GridView

+0

感謝回覆,但它沒有工作,但在我找到它的解決方案之前的幾天。 –

+0

還沒有嘗試過RecyclerView ...因爲我已經實現了gridview,所以必須找到別的東西。 @andy –

2

搜索了很多後,我發現使用可擴展的gridview可以解決它。我使用了下面的類,它工作正常。

這是本地GridView的自定義實現, 這裏我們重寫與GridView的高度有關的功能。 此實現取消了一個固定的高度GridView,而是 GridView的高度根據添加到其中的子元素的數量展開。

public class ExpandableGridView extends GridView{ 

    boolean expanded = false; 
    public boolean isExpanded() 
    { 
     return expanded; 
    } 

    public ExpandableGridView(Context context) 
    { 
     super(context); 
    } 

    public ExpandableGridView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
    } 

    public ExpandableGridView(Context context, AttributeSet attrs, 
      int defStyle) 
    { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 

     if (isExpanded()) 
     { 
      // Calculate entire height by providing a very large height hint. 
      // But do not use the highest 2 bits of this integer; those are 
      // reserved for the MeasureSpec mode. 
      int expandSpec = MeasureSpec.makeMeasureSpec( 
        Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); 
      super.onMeasure(widthMeasureSpec, expandSpec); 

      ViewGroup.LayoutParams params = getLayoutParams(); 
      params.height = getMeasuredHeight(); 
     } 
     else 
     {  
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    } 

    public void setExpanded(boolean expanded) 
    { 
     this.expanded = expanded; 
    } 

} 
+0

This Works!謝謝 –