android关于自定义ViewGroup加载组件的问题?

我想要自定义一个ViewGroup,里面用ViewDragHelper做一个可以拖拽的界面。现在遇到的问题是,我需要在ViewGroup中加入HorizontalScrollView,我希望把加载HorizontalScrollView中数据的部分放在activity中去完成,但是每次都没有HorizontalScrollView,之前自定义这种View确实做的比较少,现在不知道如何解决。

1.先贴布局文件。

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <widget.PaperLayout
        android:id="@+id/paperlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@mipmap/tempescope"
            android:scaleType="centerCrop"/>

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

            <LinearLayout
                android:id="@+id/scrollview_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:background="@null"
                android:layout_gravity="center_vertical"
                android:orientation="horizontal"/>

        </HorizontalScrollView>

    </widget.PaperLayout>

</FrameLayout>

2.PaperLayout代码

private void init(Context context) {
        final float density = context.getResources().getDisplayMetrics().density;
        viewDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback());
        viewDragHelper.setMinVelocity(density * MIN_FLING_VELOCITY);
    }

    @Override
    protected void onFinishInflate() {
        mHeaderView = getChildAt(0);
        mBottomView = (HorizontalScrollView) getChildAt(1);
        super.onFinishInflate();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int parentHeight = getHeight();
        int headerHeight = mHeaderView.getHeight();
        mHeaderView.layout(0, 0, getWidth(), parentHeight / 2);
        mBottomView.layout(0, mHeaderView.getHeight(), getWidth(), parentHeight / 2);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
        int maxHeight = MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, 0), resolveSizeAndState(maxHeight, heightMeasureSpec, 0));

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private class DragHelperCallback extends ViewDragHelper.Callback {

        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return child == mBottomView;
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            requestLayout();
//            super.onViewPositionChanged(changedView, left, top, dx, dy);
        }
    }

3.在activity中加载horizontalscrollview中图片代码

private void initScrollView() {
        linearLayout.removeAllViews();
        for (int i = 0; i < images.length; i++) {
            int width = this.width / 3;
            LinearLayout itemLayout = (LinearLayout) LinearLayout.inflate(this, R.layout.item_scrollview, null);
            itemLayout.setLayoutParams(new LinearLayout.LayoutParams(width, ViewGroup.LayoutParams.MATCH_PARENT));
            itemLayout.setGravity(Gravity.CENTER_VERTICAL);

            final ImageView imageView = (ImageView) itemLayout.findViewById(R.id.item_scrollview_image);
            imageView.setBackgroundResource(images[i]);

            linearLayout.addView(itemLayout);
        }
    }

希望会的哥们能指导我一下,这个PaperLayout中我应该怎么去写,才能出现仔面hrizontalscrollview的内容

查看回复