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

发布于 2022-09-01 20:58:28 字数 4042 浏览 11 评论 0

我想要自定义一个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的内容

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恍梦境° 2022-09-08 20:58:28

在 onLayout 里,不要使用 getHeight() getWidth() 之类的函数,这时候Layout还没完成,如果是初次使用,获取到的值就是0,你的View当然就没有宽度,显示不出来了。

要用getMeasuredHeight(), getMeasuredWidth()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文