设置视图的宽度后,将LayoutParams的ClassCastException转换为MarginLayoutParams [英] ClassCastException of LayoutParams to MarginLayoutParams after setting a view's width

查看:48
本文介绍了设置视图的宽度后,将LayoutParams的ClassCastException转换为MarginLayoutParams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个小的代理类,以便可以使用ObjectAnimator为视图的边距设置动画.在检查了它是否可以正常工作并且所有动画都正确设置之后,我想在动画之前调整视图的大小,但是在设置宽度之后,我的动画因ClassCastException而失败.我不知道为什么.

I've written a small proxy class so that I can use ObjectAnimator to animate a view's margin. After checking that this worked and everything animated properly, I wanted to adjust the view's size before the animation, but after I set the width, my animation fails with the ClassCastException. I haven't a clue why.

private class handleClickListener implements View.OnClickListener {
    private static final int LEFT_MARGIN_VISIBLE = 0;
    private static final int LEFT_MARGIN_HIDDEN = -196;

    public void onClick(View view) {
        LinearLayout row = (LinearLayout) view.getParent().getParent();
        RelativeLayout options = (RelativeLayout) row.findViewById(R.id.options);
        LayoutProxy layoutProxy = new LayoutProxy(options);

        // First make sure the options are the right width to fill the screen with the handle
        int rowWidth = row.getWidth();
        int handleWidth = view.getWidth();
        layoutProxy.setWidth(rowWidth - handleWidth);
        ObjectAnimator animation;

        if (layoutProxy.getLeftMargin() == Util.dipsToPixels(options, LEFT_MARGIN_VISIBLE)) {
            animation = ObjectAnimator.ofInt(layoutProxy, "leftMargin", Util.dipsToPixels(view, LEFT_MARGIN_VISIBLE), Util.dipsToPixels(view, LEFT_MARGIN_HIDDEN)); 
        } else {
            animation = ObjectAnimator.ofInt(layoutProxy, "leftMargin", Util.dipsToPixels(view, LEFT_MARGIN_HIDDEN), Util.dipsToPixels(view, LEFT_MARGIN_VISIBLE)); 
        }

        animation.setDuration(200);
        animation.start();
    }

    /**
     * Allows an ObjectAnimator to set/get margins and dimensions of a view
     */
    private class LayoutProxy {
        private ViewGroup mView;

        public LayoutProxy(View view) {
            mView = (ViewGroup) view;
        }

        public int getWidth() {
            ViewGroup.LayoutParams lp = mView.getLayoutParams();
            return lp.width;
        }

        public void setWidth(int width) {
            ViewGroup.LayoutParams lp = mView.getLayoutParams();
            mView.setLayoutParams(new ViewGroup.LayoutParams(width, lp.height));
        }

        public int getHeight() {
            ViewGroup.LayoutParams lp = mView.getLayoutParams();
            return lp.height;
        }

        public void setHeight(int height) {
            ViewGroup.LayoutParams lp = mView.getLayoutParams();
            mView.setLayoutParams(new ViewGroup.LayoutParams(lp.width, height));
        }

        public int getLeftMargin() {
            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) mView.getLayoutParams();
            return lp.leftMargin;
        }

        public void setLeftMargin(int margin) {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            lp.setMargins(margin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
            mView.requestLayout();
        }

        public int getTopMargin() {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            return lp.topMargin;
        }

        public void setTopMargin(int margin) {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            lp.setMargins(lp.leftMargin, margin, lp.rightMargin, lp.bottomMargin);
            mView.requestLayout();
        }

        public int getRightMargin() {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            return lp.rightMargin;
        }

        public void setRightMargin(int margin) {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            lp.setMargins(lp.leftMargin, lp.topMargin, margin, lp.bottomMargin);
            mView.requestLayout();
        }

        public int getBottomMargin() {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            return lp.bottomMargin;
        }

        public void setBottomMargin(int margin) {
            MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
            lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, margin);
            mView.requestLayout();
        }
    }
}

错误:

FATAL EXCEPTION: main
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams
at com.test.data.ContactListCursorAdapter$handleClickListener$LayoutProxy.getLeftMargin(ContactListCursorAdapter.java:135)
at com.test.data.ContactListCursorAdapter$handleClickListener.onClick(ContactListCursorAdapter.java:94)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)

推荐答案

我有类似的问题

起初我在使用:

LayoutParams viewFlowLayout = new LayoutParams(originalWidth, newHeight);
viewFlow.setLayoutParams(flowViewLayout);

我有和您相同的错误消息:

And I have the same error message as yours:

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams

我不知道为什么不使用MarginLayoutParams.

which I have no idea why as i didn't use MarginLayoutParams.

然后我尝试将其更改为此

Then I try and change it to this

LayoutParams viewFlowLayout = viewFlow.getLayoutParams();
viewFlowLayout.height = newheight;
viewFlow.setLayoutParams(flowViewLayout);

错误消失了...

我想您对于上述代码有多个用例.例如:更改此

I guess you have multiple use case for the above code. For example: Change this

public void setHeight(int height) {
    ViewGroup.LayoutParams lp = mView.getLayoutParams();
    mView.setLayoutParams(new ViewGroup.LayoutParams(lp.width, height));
}

public void setHeight(int height) {
    ViewGroup.LayoutParams lp = mView.getLayoutParams();
    lp.height = height  //width will remain and height will be change to the newHeight
    mView.setLayoutParams(lp);
}

希望对您和那里的许多人有帮助...

Hope that it will be helpful to you and many people out there...

这篇关于设置视图的宽度后,将LayoutParams的ClassCastException转换为MarginLayoutParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆