gpt4 book ai didi

Android 使用自定义 View 多次 inflatedView

转载 作者:行者123 更新时间:2023-11-29 20:18:49 27 4
gpt4 key购买 nike

我创建了一个自定义类来快速创建 View 对象(基本上它只是一个显示的问题)。例子:

Title : Weather

Message : How is the weather today?

所以这是我的扩展 View 的自定义类:

public class Question extends View {

Context mContext;

public Question(Context context) {
super(context);
mContext = context;
}

public void addQuestion (View rootView, String title, String message) {

// Inflate une question
LinearLayout rootQuestion = (LinearLayout) rootView.findViewById(R.id.activity_formulaire_questions_preliminaires_ll_wrapper);
LayoutInflater questionInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myQuestion = (LinearLayout) questionInflater.inflate(R.layout.activity_formulaire_question, rootQuestion);

// Assigne la vue au controlleur
TextView tvTitle = (TextView) myQuestion.findViewById(R.id.activity_formulaire_question_title);
TextView tvMessage = (TextView) myQuestion.findViewById(R.id.activity_formulaire_question_message);

// Remplissage des champs textes
tvTitle.setText(title);
tvMessage.setText(message);
}

还有我的 fragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_formulaire_questions_preliminaires, null);

Question question = new Question(super.getActivity());
question.addQuestion(rootView, "Question 1 Title", "1");
question.addQuestion(rootView, "Question 2 Title", "2");
question.addQuestion(rootView, "Question 3 Title", "3");
question.addQuestion(rootView, "Question 4 Title", "4");

return rootView;
}

但是在启动 fragment 时,它只是正确显示的最后一个问题。

->第4题标题

->4

->默认标题

->-1

->默认标题

->-1

->默认标题

->-1

为什么? 我虽然在使用 questionInflater.inflate 时会得到一个新 View ,并且尊重层次结构“View -> ViewGroup -> LinearLayout”

最佳答案

您可以通过多种方式解决这个问题。例如,在这种情况下您实际上不需要扩展 View。

像这样的东西应该可以工作:

 public class QuestionHelper {
Context mContext;

public QuestionHelper(Context context) {
mContext = context;
}

public void addQuestion(ViewGroup rootView, String title, String message) {
// Inflate une question
LayoutInflater questionInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myQuestion = (LinearLayout) questionInflater.inflate(R.layout.activity_formulaire_question, null, false);

// Assigne la vue au controlleur
TextView tvTitle = (TextView) myQuestion.findViewById(R.id.activity_formulaire_question_title);
TextView tvMessage = (TextView) myQuestion.findViewById(R.id.activity_formulaire_question_message);

// Remplissage des champs textes
tvTitle.setText(title);
tvMessage.setText(message);

rootView.addView(myQuestion);
}
}

在你的 fragment 上:

QuestionHelper questionHelper = new QuestionHelper(this);
questionHelper.addQuestion(rootView, "Question 1 Title", "1");
questionHelper.addQuestion(rootView, "Question 2 Title", "2");
questionHelper.addQuestion(rootView, "Question 3 Title", "3");
questionHelper.addQuestion(rootView, "Question 4 Title", "4");

关于Android 使用自定义 View 多次 inflatedView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33496440/

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