У меня есть класс, который создает пользовательские представления. Я назвал их Растениями. Каждый из них имеет следующие атрибуты: имя, степень, информация и изображение. Я хотел бы изменить информационный текст того, который имеет имя: «Тест», например, после нажатия на кнопку из другого класса. Я пытался установить теги для этих пользовательских видов, но я не совсем понял, как это работает.
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);
}