自定义ViewGroup未显示所有子项

时间:2017-01-09 00:06:22

标签: android children display viewgroup

出于某种原因,当我向Horizo​​ntalscrollview添加重量时,我的自定义视图组没有显示所有子项,但是当我删除重量时它是完美的,这是我的xml。有谁知道发生了什么事?

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="7"
            android:orientation="vertical">

        </LinearLayout>

        <HorizontalScrollView
            android:id="@+id/handScrollView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_weight="2"
            android:background="@color/colorAccent">

            <com.example.example.cardgame.bin.template.HandViewLayout
                android:id="@+id/handView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/black_grey">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/cardview_light_background"
                    android:text="View1" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/cardview_dark_background"
                    android:text="View2" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/face_down"
                    android:text="View3" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@color/colorAccent"
                    android:text="View4" />
            </com.example.example.cardgame.bin.template.HandViewLayout>
        </HorizontalScrollView>
    </LinearLayout>
</LinearLayout>

但是,当我将Horizo​​ntal scrollview设置为wrap_content或match_parent时,它会按照需要运行,为什么在我设置权重属性时它不起作用?

这是我的Class HandViewLayout

public class HandViewLayout extends ViewGroup {
    private int mLeftWidth;
    public float coverCard = 0;
    private int mRightWidth;

    private final Rect mTmpContainerRect = new Rect();
    private final Rect mTmpChildRect = new Rect();
    public OnClickListenerViewGroup onClickListenerViewGroup;


    public HandViewLayout(Context context) {
        super(context);
        setHorizontalScrollBarEnabled(true);
        setVerticalScrollBarEnabled(true);
    }

    public void setCoverCard(float coverCard) {
        this.coverCard = -Math.abs(coverCard);
    }

    public float getCoverCard() {
        return this.coverCard;
    }

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

    public HandViewLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnClickListenerViewGroup(OnClickListenerViewGroup onClickListenerViewGroup) {
        this.onClickListenerViewGroup = onClickListenerViewGroup;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int count = getChildCount();

        // These are the far left and right edges in which we are performing layout.
        int leftPos = getPaddingLeft();
        int rightPos = r - l - getPaddingRight();

        // This is the middle region inside of the gutter.
        final int middleLeft = leftPos + mLeftWidth;
        final int middleRight = rightPos - mRightWidth;

        // These are the top and bottom edges in which we are performing layout.
        final int parentTop = getPaddingTop();
        final int parentBottom = b - t - getPaddingBottom();

        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);

            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            final int width = child.getMeasuredWidth();
            final int height = child.getMeasuredHeight();

            final int index = i;

            child.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickListenerViewGroup.onClickListenerViewGroup(index);
                }
            });

            if (i > 0) {
                final View previousChild = getChildAt(i - 1);
                mTmpContainerRect.left = middleLeft + lp.leftMargin + previousChild.getLeft() + previousChild.getWidth() + (int) coverCard;
                mTmpContainerRect.right = middleRight - lp.rightMargin;
                mTmpContainerRect.top = parentTop + lp.topMargin;
                mTmpContainerRect.bottom = parentBottom - lp.bottomMargin;
            } else {
                mTmpContainerRect.left = middleLeft + lp.leftMargin;
                mTmpContainerRect.right = middleRight - lp.rightMargin;
                mTmpContainerRect.top = parentTop + lp.topMargin;
                mTmpContainerRect.bottom = parentBottom - lp.bottomMargin;
            }


            // Use the child's gravity and size to determine its final
            // frame within its container.
            Gravity.apply(lp.gravity, width, height, mTmpContainerRect, mTmpChildRect);

            // Place the child.
            child.layout(mTmpChildRect.left, mTmpChildRect.top,
                    mTmpChildRect.right, mTmpChildRect.bottom);
        }
    }

    @Override
    protected boolean awakenScrollBars() {
        return true;
    }

    @Override
    protected int computeVerticalScrollExtent() {
        return super.computeVerticalScrollExtent();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();

        mLeftWidth = 0;
        mRightWidth = 0;

        int maxHeight = 0;
        int maxWidth = 0;
        int childState = 0;


        for (int i = 0; i < count; i++) {

            final View child = getChildAt(i);
//            Log.e("child Text", i + ": " + ((TextView) child).getText().toString());
            if (child.getVisibility() != GONE) {
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);

                final LayoutParams lp = (LayoutParams) child.getLayoutParams();

                // last one show all
                if (i == getChildCount() - 1) {
                    maxWidth += child.getMeasuredWidth();
                } else {
                    maxWidth += child.getMeasuredWidth() + coverCard;
                }

                Log.d("Spacer", "spacer, " + i + "," + maxWidth);

                maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
            }
        }


        setMeasuredDimension(resolveSizeAndState(maxWidth, heightMeasureSpec, childState),
                resolveSizeAndState(maxHeight, heightMeasureSpec,
                        childState));
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new HandViewLayout.LayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }

    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    public static class LayoutParams extends MarginLayoutParams {
        public int gravity = Gravity.TOP | Gravity.START;

        public static int POSITION_MIDDLE = 0;
        public static int POSITION_LEFT = 1;
        public static int POSITION_RIGHT = 2;

        public int position = POSITION_MIDDLE;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);


            TypedArray atts = c.obtainStyledAttributes(attrs, R.styleable.HandViewLayout);
            gravity = atts.getInt(R.styleable.HandViewLayout_android_layout_gravity, gravity);
            position = atts.getInt(R.styleable.HandViewLayout_layout_position, position);
            atts.recycle();
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }
    }
}

属性xml

<declare-styleable name="HandViewLayout">
        <attr name="layoutColor" format="color" />
        <attr name="android:layout_gravity" />
        <attr name="layout_position">
            <enum name="middle" value="0" />
            <enum name="left" value="1" />
            <enum name="right" value="2" />
        </attr>
    </declare-styleable>

PIC

enter image description here

0 个答案:

没有答案