"Redraw" LinearLayout 视图更改选项

"Redraw" LinearLayout view to change option

我正在尝试将属性更改为自定义 LinearLayout class,我将选项设置为 class:

MyBuilder option = new MyBuilder.Builder()
.image(...)
.setCardRadius(...)
.build());

比我打电话MainActivity

MyObject obj = (MyObject) findViewById(R.id.myObject); 
obj.init(context, option);

但是如果我用不同的 option 多次调用 obj.init(...) 构建器设置了旧值,因此视图无法正确更改属性。

所以我的问题是:我可以在初始化新对象之前重置 Builder o LinearLayout 吗?

这是我的LinearLayout.java:

public class MyObject extends LinearLayout{
    CardView card;
    ImageView image;
    float cardRadiusAttr;
    View rootView;
    AttributeSet attributeSet;

    public void init(final Context context, final MyBuilder option){
        if(option != null)
        {
         /*
        Get attribute from XML
         */
            TypedArray ta = context.obtainStyledAttributes(attributeSet, R.styleable.Card, 0, 0);
            try {
                cardRadiusAttr = ta.getDimension(R.styleable.Card_c_cardRadius, option.getCardRadius());

            } finally {
                ta.recycle();
            }

        /*
        Set up xml object.
         */
            card = (CardView) findViewById(R.id.card);
            image = (ImageView) findViewById(R.id.image);

            card.setRadius(cardRadiusAttr);

            /**
             * Check if Option is set
             */
            if (option.isImage() != null) {

              //Set Image

            }
        }else{
            Log.e("Initialization", "Option View not initialize!");
        }
    }


    public MyObject(Context context, AttributeSet attrs) {
        super(context, attrs);
        /*
        Inflater custom layout to view.
         */
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rootView = inflater.inflate(R.layout.Card, this, true);
        attributeSet = attrs;
    }


    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

}

这是MyBuilder.java

public class MyBuilder {
    private int mImage;
    private float mCardRadius = 4f;

    private MyBuilder(Builder builder)
    {
        mImage = builder.mImage;
        mCardRadius = builder.mCardRadius;
    }

    public static class Builder{
        private int mImage;
        private float mCardRadius = 4f;

        public Builder setCardRadius(float radius)
        {
            if(radius <= 0)
            {
                Log.e("CardRadius", "Impossible to set Card Radius lower than 0! Please Check it");
            }
            else {
                mCardRadius = radius;
            }
            return this;
        }

        public Builder image(int image) {

            if(image == 0)
            {
                Log.e("Image", "Impossible to set Image to 0! Please Check it");
            }
            else {
                mImage = image;
            }
            return this;
        }

        public MyBuilder build() {
            return new MyBuilder(this);
        }

    }

    public int getImage() {
        return mImage;
    }

    public float getCardRadius() {
        return mCardRadius;
    }
}

我终于找到问题了。

MyObjectinit 方法中,您必须 在上次使用后清理视图

在这种特殊情况下,首先,您传递一组选项。基于它们,View 正在调整其控件的 Visibility(使 button1button2Visible)。但是当你传递 另一组 选项时 - 你必须清除之前所做的所有更改。 (即隐藏 button1button2 等,让视图再次调整其控件的 Visibility