2015-10-20 3 views
0

you can see gridview in the image which is not wrapping whole nestedscrollview Ребята, пожалуйста, помогите мне. Я застрял в этом коде, мне нужен сворачиваемый макет панели инструментов, и мне тоже удастся, но gridview внутри вложенного scrollview не обертывает общее пространство вложенного scrollview, я пробовал много но не удалось.вопрос о размере сетки в вложенном scrollview

Это XML, который помогает мне, чтобы свернуть панель инструментов и где я получаю GridView вопросы,

<?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

Использование RecyclerView с GridLayoutManager. Поскольку просмотр 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 ... coz Я реализовал 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

Это работает! Спасибо –