ScrollView makes custom layout invisible

I created a custom Viewgroupone that I need to use in my application, but I need to put it in ScrollView. When the layout is done only with my custom Viewgroup, everything works fine, but when I put it in ScrollView, I don’t see anything. My layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <com.example.test.CustomLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </com.example.test.CustomLayout>

</ScrollView>

My viewing group:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            /* do something and call for each child             
                    View v = getChildAt(i);
                    v.measure(wspec, hspec);
                    */

        setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));

    }

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
        //do something and call layout on every child 
    }

UPDATE: My CustomLayout Class

public class CustomLayout extends ViewGroup{

    /*My params*/

    public CustomLayout(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

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

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

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
            //do something and call layout on every child 
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                /* do something and call for each child             
                        View v = getChildAt(i);
                        v.measure(wspec, hspec);
                        */

            setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));

    }

}

UPDATE 2: Sorry, but I made several other attempts, and it seems that if I have a view group in scrollview using the onMeasure method, I have heightMeasureSpec = 0, then if I put the view group in any other layout, I get an integer . Maybe this will help?

+5
source share
5 answers

, Viewgroup, .
:

int heightMeasured = 0;
/*for each child get height and
heightMeasured += childHeight;*/


//If I am in a scrollview i got heightmeasurespec == 0, so
if(heightMeasureSpec == 0){
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightMeasured, MeasureSpec.AT_MOST);
}

setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec));

.

+6
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

: layout_height to match_parent

0

You also use this contructor below, right? Because this is the one that gets called when you create your view in xml.

public CustomLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
        // Do your stuff
}
0
source

You use wrap_contentfor height:

<com.example.test.CustomLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

To allow 0px afaik, since there is no content - try giving you the default minimum height component

0
source

Check that it will be great!

 public class MaxHeightScrollView extends ScrollView {

    public static int DEFAULT_MAX_HEIGHT = -1;
    private int maxHeight = DEFAULT_MAX_HEIGHT;

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

    public MaxHeightScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.ScrollMaxHeight,
                0, 0);
        try {
            setMaxHeight(a.getInt(R.styleable.ScrollMaxHeight_maxHeight, 0));
        } finally {
            a.recycle();
        }
    }

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            int heightSize = MeasureSpec.getSize(heightMeasureSpec);
            if (maxHeight != DEFAULT_MAX_HEIGHT
                    && heightSize > maxHeight) {
                heightSize = maxHeight;
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
                getLayoutParams().height = heightSize;

            } else {
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
                setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec)
                        , getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec));
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }

    public void setMaxHeight(int maxHeight) {
        this.maxHeight = maxHeight;
    }


}

make attr.xmlin the values ​​folder and add below code

<resources>
   <declare-styleable name="ScrollMaxHeight">
        <attr name="maxHeight" format="integer"/>
    </declare-styleable>
</resources>

Use it as

<com.yourapppackage.customviews.MaxHeightScrollView
    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="wrap_content"
    app:maxHeight="200"
    android:fadeScrollbars="false"
    android:gravity="center_horizontal">

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

</com.yourapppackage.customviews.MaxHeightScrollView>
0
source

All Articles