Java 在每个回收器视图中添加自定义布局的问题

Java 在每个回收器视图中添加自定义布局的问题,java,android,kotlin,android-recyclerview,Java,Android,Kotlin,Android Recyclerview,我正在开发一个应用程序,发布者可以将问题上传到数据库。出版商可以添加任意数量的问题,所以我使用了回收者视图来处理这个问题。当publisher单击按钮添加问题时,将按照my recyclerview适配器中的指定为新布局充气。TopicQuestionsAdapter是我的回收器视图适配器的名称 ... RecyclerView addTopicOfTheDayWhereDynamicLayoutsWillBeAdded; TopicQ

我正在开发一个应用程序,发布者可以将问题上传到数据库。出版商可以添加任意数量的问题,所以我使用了回收者视图来处理这个问题。当publisher单击按钮添加问题时,将按照my recyclerview适配器中的指定为新布局充气。TopicQuestionsAdapter是我的回收器视图适配器的名称

                             ...

RecyclerView addTopicOfTheDayWhereDynamicLayoutsWillBeAdded;
TopicQuestionsAdapter topicQuestionsAdapter;
ArrayList<TopicQuestions> topicQuestionsList;

addTopicOfTheDayWhereDynamicLayoutsWillBeAdded.setHasFixedSize(true);
addTopicOfTheDayWhereDynamicLayoutsWillBeAdded.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
topicQuestionsList = new ArrayList<>();

                              ...

addTopicOfTheDayAddQuiz.setOnClickListener(v29 -> {

                              ...

            // dynamically add questions layout on subsequent clicks
            // here with an x button in each layout to remove the
            // entire question layout

            topicQuestionsList.add(new TopicQuestions());
            topicQuestionsAdapter = new TopicQuestionsAdapter("Adapter", getContext(), topicQuestionsList);
            addTopicOfTheDayWhereDynamicLayoutsWillBeAdded.setAdapter(topicQuestionsAdapter);
            topicQuestionsAdapter.notifyItemInserted(topicQuestionsList.size());

        }
    });
正如我在前面的代码中所暗示的,每当我单击AddTopicoftheDayaddQuike按钮时,该按钮应该在已经存在的视图下面添加另一个视图,已经存在的视图中的所有子视图都将被清除。请确定解决此问题的更好方法是什么


您的适配器在
onBindViewHolder()
中做了大量工作:设置大量单击侦听器,这些侦听器反过来切换UI,以便用户可以输入他们的问题详细信息,而不必担心他们不需要的UI元素

但您似乎没有跟踪用户为每个问题所做的选择。 每次用户添加新问题时,您都会扔掉现有适配器:

topicQuestionsAdapter=新的topicQuestionsAdapter(“适配器”,getContext(),topicQuestionsList); AddTopicOftheDayWhere dynamicClayOutswillbeaded.setAdapter(topicQuestionsAdapter)

由于您没有为我共享足够的代码来调试您的应用程序和测试我的解决方案,因此我将简要介绍一些想法:

不要每次创建新问题时都使用新适配器,只需将问题添加到列表中,然后调用即可(请注意:topicQuestionsList.size()-1

您需要做的另一件事是跟踪每个列表项的状态(哪些
按钮等可见,哪些是
EditText
s的内容,如果有…)。如果列表变长,用户需要滚动,即使您继续使用同一适配器,也会重复调用onBindViewHolder()


因此,您需要一个类(我们称之为
ListEntry
),其中包含反映列表项可能状态的字段。在适配器内部,您可以为数据列表的每个项目保留一个
列表
,其中包含一个
ListEntry
。每次看到某个
按钮
时,您都会更新相应的
列表
。在
onBindViewHolder()
中,您计算给定位置的
ListEntry
,并相应地设置
holder.itemView的子
视图的属性。

谢谢。如果可以的话,我想要一段代码。这将帮助我更好地理解您,我将更新此内容,以便您可以看到代码的重要部分。特别是我的POJO课程,主题问题
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TopicQuestionsViewHolder {
//R.layout.add_question_layout is the layout
    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.add_question_layout, parent, false)
    return TopicQuestionsViewHolder(itemView)
}

/*this is where my problem is i observed that whenever i
    add a new question, the layouts i had already added to previous
    views get cleared. Recycler view destroys states*/

 override fun onBindViewHolder(holder: TopicQuestionsViewHolder, position: Int) {
    
    // the textview to show this question number
    holder.questionNumber.text = "${position + 1}."

    // the image button to remove the whole view
    holder.removeQuestion.setOnClickListener {
        topicQuestions.removeAt(position)
        notifyItemRemoved(position)
        notifyItemRangeChanged(position, topicQuestions.size)
    }

    holder.addField.setOnClickListener {
        // need to fix this
        when (holder.theFields.visibility == View.GONE) {
            true -> {
                holder.theFields.visibility = View.VISIBLE
                holder.addField.setImageResource(R.drawable.close)
                Log.wtf(TAG, "true")
            }
            false -> {
                holder.theFields.visibility = View.GONE
                holder.addField.setImageResource(R.drawable.add)
                Log.wtf(TAG, "false")
            }
        }
    }

    // this button would add a question field to this view
    holder.addQuestionField.setOnClickListener {
        val lParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        lParams.topMargin = 16
        val view = LayoutInflater.from(ctx).inflate(R.layout.add_question_field_to_question_layout, null)
        view.layoutParams = lParams
        holder.toAddViews.addView(view)
    }

    // this button would add an image question field to this view
    holder.addImageQuestionField.setOnClickListener {
        Log.wtf(TAG, "image question will be added here")
    }

    // this button would add toggle option to this view
    holder.addToggleOption.setOnClickListener {
        Log.wtf(TAG, "toggle option will be added here")
    }

    // this button would add check box option to this view
    holder.addCheckBoxOption.setOnClickListener {
        Log.wtf(TAG, "check box option will be added here")
    }
}
topicQuestionsAdapter.notifyItemInserted(topicQuestionsList.size() - 1);