我怎样才能更改之前创建的自定义视图的一个属性?

问题描述 投票:-1回答:1

我有一个创建自定义视图的类。我称它们为植物视图。他们每个人都有这些属性:姓名,学位,信息和图像。我想更改名称为“Test”的信息文本,例如,在点击其他类的按钮后。我试图将标签设置为这些自定义视图,但我还没有真正理解它是如何工作的。

PlantView.java

public class PlantView extends FrameLayout {

//Views
private UnderlinedTextView nameView;
private TextView infoView;
private TextView degreeView;
private CircleImageView imageView;
private LinearLayout planteBg;

//Attributes
private String nameText;
private String infoText;
private String degreeText;
private Drawable plantImage;

private boolean isExtended = false;

/****************
 * CONSTRUCTEURS
 ***************/

/**
 * Constructeur de la classe.
 *
 * @param context the context.
 */
public PlantView(@NonNull Context context) {
    super(context);
    obtainStyledAttributes(context, null, 0);
    init();
}

/**
 * Constructeur.
 *
 * @param context the context.
 * @param attrs   the attributes from the layout.
 */
public PlantView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    obtainStyledAttributes(context, attrs, 0);
    init();
}

/**
 * Constructeur.
 *
 * @param context      the context.
 * @param attrs        the attributes from the layout.
 * @param defStyleAttr the attributes from the default style.
 */
public PlantView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    obtainStyledAttributes(context, attrs, defStyleAttr);
    init();
}


/**********
* FONCTIONS
***********/

//Fonction utile pour récupérer les attributs si l'on ajoute une "PlantView" directement en XML
private void obtainStyledAttributes(Context context, AttributeSet attrs, int defStyleAttr) {
    if (attrs != null) {
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PlantView, defStyleAttr, 0);
        nameText = typedArray.getString(R.styleable.PlantView_name);
        infoText = typedArray.getString(R.styleable.PlantView_info);
        degreeText = typedArray.getString(R.styleable.PlantView_degree);
        plantImage = typedArray.getDrawable(R.styleable.PlantView_android_src);

    }
}
//Fonction permettant d'initialiser la vue
private void init() {
    inflate(getContext(), R.layout.plantview, this);
    nameView = findViewById(R.id.nomPlante);
    infoView = findViewById(R.id.info);
    degreeView = findViewById(R.id.degree);
    imageView = findViewById(R.id.image);
    planteBg = findViewById(R.id.plante);

    setupView();
}
//Fonction permettant d'"installer" la vue
private void setupView() {

    nameView.setText(nameText);
    infoView.setText(infoText);
    degreeView.setText(degreeText +" °C");
    imageView.setImageDrawable(plantImage);
    planteBg.setOnClickListener(new OnClickListener() {
        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {

            if(isExtended){
                TransitionManager.beginDelayedTransition(planteBg, new TransitionSet()
                        .addTransition(new ChangeBounds()));


                ViewGroup.LayoutParams params = planteBg.getLayoutParams();
                params.height = LayoutParams.WRAP_CONTENT;

                planteBg.setLayoutParams(params);
                isExtended = false;

            }else if(isExtended == false) {

                TransitionManager.beginDelayedTransition(planteBg, new TransitionSet()
                        .addTransition(new ChangeBounds()));

                ViewGroup.LayoutParams params = planteBg.getLayoutParams();
                params.height = 500;
                planteBg.setLayoutParams(params);
                isExtended = true;
            }
        }
    });
}

/**
 * Fonctions permettant de changer les attributs
 * de la vue par programmation
 */
public void setName(String name) {
    nameView.setText(name);
}
public void setInfo(String info){
    infoView.setText(info);
}
public void setDegree(String degree){
    degreeView.setText(degree+" °C");
}
public void setImage(Bitmap image){
    imageView.setImageBitmap(image);
}
java android view android-custom-view
1个回答
0
投票
    On your setters after you've set the text call invalidate();
    public void setName(String name) {
    nameView.setText(name);
invalidate();
    }
    public void setInfo(String info){
        infoView.setText(info);
invalidate();
    }
    public void setDegree(String degree){
        degreeView.setText(degree+" °C");
invalidate();
    }
    public void setImage(Bitmap image){
        imageView.setImageBitmap(image);
invalidate();
    }
© www.soinside.com 2019 - 2024. All rights reserved.