kotlin 动态无限循环EditText视图

nwo49xxi  于 2023-01-26  发布在  Kotlin
关注(0)|答案(2)|浏览(138)

我试图创建一个EditText的循环视图,当你用按钮添加EditText时,它会当场更新。
对于reference,我希望在Gform上有一个类似于复选框问题编辑的功能,但只能使用EditText。
下面是我的代码:

    • 主要活动代码**
class AjoutObjectifActivity : AppCompatActivity() {

    //ArrayList used to add items to the RecyclerView
    private var etapes: ArrayList<String> = ArrayList()

    private lateinit var binding: ActivityAjoutObjectifBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityAjoutObjectifBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupButtons()

    }

    //The click listener of my button
    private fun setupButtons() {
        binding.buttonAddEtape.setOnClickListener{
            addEtape()
        }
    }

    //The function that add one element to my ArrayList, and is supposed to reload my RecyclerView to add the corresponding EditText
    //I know it is supposed to erase all the EditTexts contents as well, but I'm looking to repair this after I manage to add EditTexts
    private fun addEtape() {
        etapes.add("")
        val adapterEtapes = EtapesAdapter(etapes)
        binding.recyclerViewObjectifsEtapes.adapter = adapterEtapes
    }
}
    • 我的"EtapesAdapter"代码**
class EtapesAdapter(private val etapes: ArrayList<String>) : RecyclerView.Adapter<EtapesAdapter.EtapeViewHolder>() {

    override fun getItemCount(): Int = etapes.size

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): EtapeViewHolder {
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_etapes_input, parent, false)
        return EtapeViewHolder(view)
    }

    override fun onBindViewHolder(holder: EtapeViewHolder, position: Int) {
        holder.bind(etapes[position])
    }

    class EtapeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val etape : TextView = itemView.findViewById(R.id.edit_text_etapes_objectif)

        fun bind(etapes: String) = with(itemView){
            etape.text = etapes
        }
    }

}
    • 我的主布局**
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_view_objectifs_etapes"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

</ScrollView>
    • 我的项目布局**
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edit_text_etapes_objectif"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="20dp" />

在使用debbuger快速检查变量之后,我的ArrayList似乎工作得很好,并且在您按下按钮时会增长,所以我猜错误一定是在适配器中的某个地方。我尝试先使用Array来做这件事,但由于您不能添加它的大小,所以我决定使用ArrayList来代替。
请记住,我是一个初学者,所以也许我使用了错误的变量或视图类型,在这种情况下,随时告诉我!
先谢了

yrwegjxp

yrwegjxp1#

感谢Menno,这段代码确实很有帮助,你的"addEtape"函数完全重新初始化了ArrayList,所以我用"etapes.add("")"代替了它,而不是在mainActivity中调用"addEtape"。).

    • 主要活动**
class AjoutObjectifActivity : AppCompatActivity() {

    private var etapes: ArrayList<String> = ArrayList()

    private lateinit var binding: ActivityAjoutObjectifBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityAjoutObjectifBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setupButtons()

    }

    private fun setupButtons() {
        binding.buttonAddEtape.setOnClickListener{
            addEtape()
        }
    }

    private fun addEtape() {
        val adapterEtapes = EtapesAdapter()
        etapes.add("")
        binding.recyclerViewObjectifsEtapes.adapter = adapterEtapes
        binding.recyclerViewObjectifsEtapes.layoutManager = 
            LinearLayoutManager(this)
        adapterEtapes.addEtapes(etapes)
    }
}
    • 适配器**
class EtapesAdapter : RecyclerView.Adapter<EtapesAdapter.EtapeViewHolder>() {
    private val etapes = ArrayList<String>()

    fun addEtapes(etapes: ArrayList<String>) {
        this.etapes.addAll(etapes)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int = etapes.size

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): EtapeViewHolder {
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_etapes_input, parent, false)
        return EtapeViewHolder(view)
    }

    override fun onBindViewHolder(holder: EtapeViewHolder, position: Int) {
        holder.bind(etapes[position])
    }

    class EtapeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val etape : TextView = itemView.findViewById(R.id.edit_text_etapes_objectif)

        fun bind(etapes: String) = with(itemView){
            etape.text = etapes
        }
    }

}
brccelvz

brccelvz2#

如果您跟踪适配器本身中的“etapes”,效果会更好,就像这样。

class EtapesAdapter : RecyclerView.Adapter<EtapesAdapter.EtapeViewHolder>() {
private val etapes = ArrayList<String>()
    fun addEtape(etape: String) {
        this.etapes.add(etape)
        notifyDataSetChanged()
    }

    fun addEtapes(etapes: ArrayList<String>) {
        this.etapes.addAll(etapes)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int = etapes.size

    override fun onCreateViewHolder(parent: ViewGroup, position: Int): EtapeViewHolder {
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_etapes_input, parent, false)
        return EtapeViewHolder(view)
    }

    override fun onBindViewHolder(holder: EtapeViewHolder, position: Int) {
        holder.bind(etapes[position])
    }

    class EtapeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val etape : TextView = itemView.findViewById(R.id.edit_text_etapes_objectif)

        fun bind(etapes: String) = with(itemView){
            etape.text = etapes
        }
    }

}

现在你可以这样做了:

val adapterEtapes = EtapesAdapter()
binding.recyclerViewObjectifsEtapes.adapter = adapterEtapes
binding.recyclerViewObjectifsEtapes.layoutManager = LinearLayoutManager()
adapterEtapes.addEtapes(etapes)

并且它应该重新加载列表。

相关问题