"重绘" LinearLayout视图更改选项

时间:2016-01-27 23:09:32

标签: java android android-layout android-linearlayout builder

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

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

我打电话给MainActivity

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

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

所以我的问题是:我可以在初始化新对象之前重置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;
    }
}

1 个答案:

答案 0 :(得分:2)

我终于找到了这个问题。

init的{​​{1}}方法中,您必须在上次使用后清理视图

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