gpt4 book ai didi

java - "Redraw"LinearLayout View 更改选项

转载 作者:行者123 更新时间:2023-11-29 20:10:26 28 4
gpt4 key购买 nike

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

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

然后我调用 MainActivity

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

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

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

这是我的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

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

关于java - "Redraw"LinearLayout View 更改选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35049909/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com