Android: CoordinatorLayout problem with GridView

1

Inside a CoordinatorLayout I have a GridView, the problem I have is that only the first two images of the GridView are seen and the rest are not anymore, I hope and they can help me.

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/background_light"
android:fitsSystemWindows="true" >

<android.support.design.widget.AppBarLayout
    android:id="@+id/main.appbar"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true" >

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/main.collapsing"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        app:title="Bienvenido">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="30dp"
            android:lineSpacingExtra="8dp"
            android:id="@+id/data_checkin" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/main.toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeScot"
            app:layout_collapseMode="pin" />
    </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" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="@dimen/activity_horizontal_margin">

        <GridView
            android:id="@+id/grid_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:columnWidth="300dp"
            android:drawSelectorOnTop="true"
            android:gravity="center"
            android:numColumns="2"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp"
            android:focusable="true"
            android:clickable="true" />

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

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_picture"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:src="@drawable/picture"
    android:tint="#FFF"
    app:layout_anchor="@id/main.appbar"
    app:layout_anchorGravity="bottom|right|end" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_home"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_margin="@dimen/activity_horizontal_margin"
    android:src="@drawable/home"
    android:tint="#FFF"
    app:layout_anchor="@id/main.appbar"
    app:layout_anchorGravity="bottom|fill_horizontal|end" />
</android.support.design.widget.CoordinatorLayout>

This way I set the images to the GridView

if (checkIn.getImages().size() > 0){//valido de que existan imagenes
        gridView.setAdapter(new ImageAdapter(this, checkIn.getImages()));//seteo las imagenes al gridView
    }//./if
    
asked by Javier fr 13.12.2016 в 01:01
source

2 answers

3

Try adding android: fillViewport="true" to your NestedScrollView Example:

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

Edited

Add the following code in the method where you declare to your Gridview, example:

ViewCompat.setNestedScrollingEnabled(gridView, true);
    
answered by 13.12.2016 / 01:25
source
1

If you only want to show a list of images, it would be best to change the NestedScrollView for a RecyclerView that can be displayed as a GridView or ListView , more efficient in listing images or heavy data.

The elements ListView and GridView when they are inside a NestedScrollView their height is the porpoción of what occupies an element in its representation. In order to make the GridView expand to all its elements you must create your own component and modify its size.

public class ExpandableHeightGridView extends GridView {

    boolean expanded = false;

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

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

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

    public boolean isExpanded() {
        return expanded;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // HACK! TAKE THAT ANDROID!
        if (isExpanded()) {
            // Calculate entire height by providing a very large height hint.
            // View.MEASURED_SIZE_MASK represents the largest height possible.
            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                    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;
    }
}

To define it within your layout you must change the path com.example with your propia ruta de paquete

<com.example.ExpandableHeightGridView
    android:id="@+id/my_grid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:horizontalSpacing="2dp"
    android:isScrollContainer="false"
    android:numColumns="4"
    android:stretchMode="columnWidth"
    android:verticalSpacing="20dp" />

By code if you want to make it expand to its full

mGrid = (ExpandableHeightGridView) findViewById(R.id.my_grid);
mGrid.setExpanded(true);

Extracted from that SO response

Extra

The same but with a ListView

public class ExpandableListView extends ListView {

    boolean expanded = false;

    public ExpandableListView(Context context, AttributeSet attrs, int defaultStyle) {
        super(context, attrs, defaultStyle);
    }

    public boolean isExpanded() {
        return expanded;
    }

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

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // HACK!  TAKE THAT ANDROID!
        if (isExpanded()) {         
            // Calculate entire height by providing a very large height hint.
            // View.MEASURED_SIZE_MASK represents the largest height possible.
            int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                        MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);

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

Extracted from that answer SO

    
answered by 13.12.2016 в 10:30