gpt4 book ai didi

java - 在每个“回收者” View 中添加自定义布局的问题

转载 作者:行者123 更新时间:2023-12-02 13:17:32 25 4
gpt4 key购买 nike

我正在构建一个应用程序,发布者将问题上传到数据库。发布者可以添加任意数量的问题,因此我使用了回收站 View 来解决这个问题。当发布者单击按钮以添加问题时,将按照我的recyclerview适配器中的指定扩大新布局。 TopicQuestionsAdapter是我的回收站 View 适配器的名称

                             ...

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

}
});
现在,回收者 View 布局由四个FAB(用于添加不同类型的问题和选项),一个textview和一个用于删除整个 View 的图像按钮组成
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")
}
}
正如我之前在代码中暗示的那样,每当我单击addTopicOfTheDayAddQuiz按钮时,该按钮应该在已经存在的 View 下方添加另一个 View ,否则将清除现有 View 内的所有 subview 。请问有什么更好的方法来解决此问题?
each question is a recycler view here
question 1 and 2 are recycler views

最佳答案

您的适配器在onBindViewHolder()中做了很多工作:设置许多单击侦听器,这些单击侦听器又会切换UI,以便用户可以输入其问题详细信息而不必为不需要的UI元素所困扰。
但是您似乎并没有跟踪用户对每个问题所做的选择。
并且,每当用户添加新问题时,您就丢弃现有的适配器:

topicQuestionsAdapter = new TopicQuestionsAdapter("Adapter", getContext(), topicQuestionsList);addTopicOfTheDayWhereDynamicLayoutsWillBeAdded.setAdapter(topicQuestionsAdapter);


由于您没有为我共享足够的代码来调试您的应用程序和测试我的解决方案,因此,我只介绍一些想法:
不必在每次创建新问题时都使用新的适配器,只需将问题添加到列表中,然后调用即可(请注意:topicQuestionsList.size() -1 )
topicQuestionsAdapter.notifyItemInserted(topicQuestionsList.size() - 1);
您需要做的另一件事是跟踪每个列表项的状态(可见的 Button等,如果有……的话 EditText的内容是什么)。如果列表变长并且用户需要滚动,即使您继续使用同一适配器,也会重复调用 onBindViewHolder()
因此,您需要一个类-我们将其称为 ListEntry-具有用于反射(reflect)列表项可能状态的字段。在适配器内部,您可以为数据列表的每个项目保留一个 List<ListEntry>和一个 ListEntry。每次看到一些 Button时,您就更新相应的 ListEntry。在 onBindViewHolder()中,您将评估给定位置的 ListEntry并相应地设置 View的子 holder.itemView的属性。

关于java - 在每个“回收者” View 中添加自定义布局的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62487467/

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