Android 使用自定义视图多次膨胀视图

Android inflate view multiple time with custom View

我创建了一个自定义的class来快速创建视图object(基本上只是一个显示的问题)。例子:

Title : Weather

Message : How is the weather today?

所以这是我的自定义 class 扩展视图 :

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);
    }

还有我的片段:

@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;
    }

但是启动片段时,只有最后一个问题正确显示。

-> 第 4 题标题

-> 4

-> 默认标题

->-1

-> 默认标题

->-1

-> 默认标题

->-1

为什么? 我虽然在使用 questionInflater.inflate 时会得到一个新视图,并且尊重层次结构 "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);
     }
}

在你的片段上:

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");