0

I've got a problem with one of my views on android, which has quite complex structure.

Basically, what I wan't to achieve is something like this:

Having scrolled view, that has inside some views with wrap content, but last of those views, has gridview. The problem is that without setting exact height of the grid view (and with wrap content)... scroll doesn't allow to scroll to the end of this grid. I don't want to use scroll inside this GridView, I used this widget to simplify data population and use build-in controls and adapters.

My XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:id="@+id/scrollview_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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

            // some relevant subviews

        </LinearLayout>

        // here problem starts
        <GridView
            android:id="@+id/grid_products"
            android:layout_width="match_parent"
            android:minHeight="50dp"
            android:layout_height="wrap_content" />
        // here problem ends

    </LinearLayout>

</ScrollView>

I tried to provide custom scrollView with overwritten onMesure, also tried in onResume invalidateScroll and use computeScroll method.

Here is some code for overwritten onMesure:

 @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,MeasureSpec.AT_MOST);

    super.onMeasure(widthMeasureSpec, expandSpec);

    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
    params.height = getMeasuredHeight()+10000;
    setLayoutParams(params);

    invalidate();
    requestLayout();
}

But with no luck ... still I have to provide exact value for layout_height, otherwise its not visible and not reachable via scrolling.

I experimented a lot and for now have no idea, what I can check next and to do to make it works.

Thanks in advance for any help, Regards David

UPDATE WITH WORKING SOLUTION

This works for me https://stackoverflow.com/a/8483078/566817 with custom GridView class, that has this method inside:

public int measureRealHeight(Context context)
{
    final int columnsCount = 3;
    final int screenWidth = ((Activity)context).getWindowManager().getDefaultDisplay().getWidth();
    final double screenDensity = getResources().getDisplayMetrics().density;
    final int columnWidth = (int) (screenWidth / columnsCount + screenDensity + 0.5f);
    final int verticalSpacing = (int) context.getResources().getDimension(R.dimen.grid_spacing_vertical);
    final int rowsCount = getAdapter().getCount() / columnsCount + (getAdapter().getCount() % columnsCount == 0 ? 0 : 1);

    return columnWidth * rowsCount + verticalSpacing * (rowsCount - 1);
}

Also need to update LayoutParams in my Fragment in onResume method:

@Override
public void onResume()
{
    super.onResume();

    int realHeight = gridViewProducts.measureRealHeight(getActivity());
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) gridViewProducts.getLayoutParams();
    lp.height = realHeight;
    gridViewProducts.setLayoutParams(lp);
    gridViewProducts.requestLayout();
}

My grid in XML

<xxx.CustomGridView
            android:id="@+id/grid_products"
            android:isScrollContainer="false"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:numColumns="3" />

I spend much time on this so far, and have no better solution so far unfortunately (I know its not perfect solution).

I hope it will help someone :)

Community
  • 1
  • 1
poslinski.net
  • 201
  • 1
  • 3
  • 15
  • Linear layout that contains your gridView is causing your problem. Try giving it some value instead of using wrap content. – Orkun Kocyigit Nov 08 '14 at 09:54
  • The problem is that I don't know that value and would be nice to have some generic solution that will calculate this value for me. I expect wrap_content will calculate it for me automatically :), but it isn't. – poslinski.net Nov 09 '14 at 13:38

0 Answers0