Android的视按钮状态充气视图多个时间 - 优化 [英] Android inflate view multiple time depending on button state - optimisation
问题描述
计算器社区,我需要你的帮助(再次)!我创建了一个自定义类来快速创建视图对象(基本上它只是一个显示问题)。
例:
StackOverflow community, I need your help (again) ! I created a custom class to quickly create View object (basically it's just a question displayed). Exemple :
标题:首页
消息:是否有猫生活
[BtnYes] ------------------------ [BtnNo]
[BtnYes] ------------------------ [BtnNo]
因此,这里是我的自定义类QuestionHelper以编程方式创建问题:
So here is my custom class "QuestionHelper" to create question programmatically :
public List<LinearLayout> addQuestion(ViewGroup rootView, String title, String message, int id) {
List<LinearLayout> mButtons = new ArrayList<>();
// Inflate une question
LayoutInflater questionInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout myQuestion = (RelativeLayout) questionInflater.inflate(R.layout.activity_formulaire_question, rootView, false);
// Assigne la vue au controlleur
tvTitle = (TextView) myQuestion.findViewById(R.id.activity_formulaire_question_title);
tvMessage = (TextView) myQuestion.findViewById(R.id.activity_formulaire_question_message);
// Remplissage des champs textes
tvTitle.setText(title);
tvMessage.setText(message);
// Boutton sous la forme de LinearLayout
btnNo = (LinearLayout) myQuestion.findViewById(R.id.activity_formulaire_question_ll_btn_no);
btnYes = (LinearLayout) myQuestion.findViewById(R.id.activity_formulaire_question_ll_btn_yes);
// Ajout dans la liste
mButtons.add(btnNo);
mButtons.add(btnYes);
return mButtons;
}
这是,我认为,规划的坏的方式(因为我不尊重MVC模式,按钮状态由视图片段管理,有很多线)。
And this is, I think, the bad way of programming (because I don't respect MVC pattern, the button state is managed by the view fragment and there are lots of lines).
final LinearLayout rootQuestion = (LinearLayout) rootView.findViewById(R.id.activity_formulaire_questions_preliminaires_ll_wrapper);
final QuestionHelper questionHelper = new QuestionHelper(super.getActivity());
List<LinearLayout> question1 = questionHelper.addQuestion(rootQuestion,
getResources().getString(R.string.question_preliminaire_1_title),
getResources().getString(R.string.question_preliminaire_1_message), 1);
question1.get(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<LinearLayout> question2 = questionHelper.addQuestion(rootQuestion,
getResources().getString(R.string.question_preliminaire_2_title),
getResources().getString(R.string.question_preliminaire_2_message), 2);
}
});
question1.get(1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<LinearLayout> question3 = questionHelper.addQuestion(rootQuestion,
getResources().getString(R.string.question_preliminaire_3_title),
getResources().getString(R.string.question_preliminaire_3_message), 3);
question3.get(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<LinearLayout> question4 = questionHelper.addQuestion(rootQuestion,
getResources().getString(R.string.question_preliminaire_4_title),
getResources().getString(R.string.question_preliminaire_4_message), 4);
question4.get(0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<LinearLayout> question6 = questionHelper.addQuestion(rootQuestion,
getResources().getString(R.string.question_preliminaire_6_title),
getResources().getString(R.string.question_preliminaire_6_message), 6);
}
});
}
});
question3.get(1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<LinearLayout> question5 = questionHelper.addQuestion(rootQuestion,
getResources().getString(R.string.question_preliminaire_5_title),
getResources().getString(R.string.question_preliminaire_5_message), 5);
question5.get(1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
List<LinearLayout> question6 = questionHelper.addQuestion(rootQuestion,
getResources().getString(R.string.question_preliminaire_6_title),
getResources().getString(R.string.question_preliminaire_6_message), 6);
}
});
}
});
}
});
我可以有高达取决于previous问题8个问题
更新: 要解释清楚我是什么意思是说,这里是一个图像。 你觉得我们可以优化这个过程?
更新2:我寻找谁拿来看,型动物的变量,并用GET /套标签保持它QuestionHolder类。我在正确的方式?
UPDATE 2 : I looked for a QuestionHolder class who take the view, the differents variables, and hold it with a get/set tag. Am I in the right way ?
更新3: QuestionHolder指的是相同的观点与getTag ...
UPDATE 3 : QuestionHolder refer to the same view with getTag...
更新4:显示的看法是这样的:的案例1:当我preSS YES YES YES 但是当我在这个问题没有向上滚动和 preSS 1 的这看起来的问题3及4 | 5和6被删除,让出现问题2。 [的为例,我手动终止应用程序并重新加载它... 的:(]
UPDATE 4 : The view displayed is like that : case 1 : when I press YES YES YES but when I scroll up and press No at the question 1 it looks like that. the questions 3&4|5&6 are removed and let appears question 2. [for the exemple, I manually kill the application and reloaded it... :(]
更新5:我创建了一个适配器一个ListView,但它比我想象的更难。所以我可能会放弃,做了肮脏的方式
UPDATE 5 : I created a ListView with an adapter, but it's harder than I think... So I will probably give up and do it the "dirty way"
推荐答案
创建类的东西重新present问题项目:
Create class what represent question item:
public class Node{
private long id;
private String question;
private String title;
private long yesId;
private long noId;
//Constructor, getter here....
}
您需要适配器问题的清单。
You need adapter with list of question.
ArrayList<Node> questions = new ArrayList<>();
NodeAdapter adapter = new NodeAdapter(questions, ...);
在pressing按钮度日回答下一个节点ID(getYesId / getNoId)。如果这最后一个项目由ID创建新的或者不检查是一张ID一样的新一张ID。
When you pressing button get next node id by answer (getYesId/getNoId). And if it last item create new by id or if not check is next id same as new next id.
//Create interface in your adapter or do what you want, but like this
public void onNodeClicked(long nextId, int currentPosition){
Node next;
if(currentPosition == questions.size() - 1){
next = getNodeById(nextId);
question.add(next);
}else{
next = questions.get(position + 1);
if(nextId != next.getId()){
ArrayList<Node> remove = new ArrayList<>();
remove.addAll(position + 1, questions);
questions.removeAll(remove);
next = getNodeById(nextId);
question.add(next);
}
}
adapter.notifyDataSetChanged();
}
我希望它可以帮助你)
I hope it can help you)
这篇关于Android的视按钮状态充气视图多个时间 - 优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!