繁体   English   中英

Android Kotlin 如何使用屏幕旋转复选框保存recyclerview的state?

[英]Android Kotlin how to save state of recyclerview with checkbox on screen rotation?

class MainActivity : AppCompatActivity() {

    val groceryList = ArrayList<String>()
    var recyclerViewGroceryList : RecyclerView ?= null
    lateinit var adapter: recyclerviewadapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        groceryList.add("data")
        groceryList.add("data")
        groceryList.add("data")
        groceryList.add("data")

        adapter = recyclerviewadapter(groceryList)
    
        recyclerViewGroceryList = findViewById(R.id.recyclerViewGroceryList)
        recyclerViewGroceryList?.layoutManager = LinearLayoutManager(this)
        recyclerViewGroceryList?.adapter = adapter
    }
}

这是我的主要方法。 这是我的适配器 class。

class recyclerviewadapter(private val list: ArrayList<String>) :
    RecyclerView.Adapter<recyclerviewadapter.ViewHolder>() {

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var checkBox = view.findViewById<CheckBox>(R.id.groceryListCheckBox)
        var groceryText = view.findViewById<TextView>(R.id.groceryListTextView)
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(viewGroup.context)
            .inflate(R.layout.recyclerviewitem, viewGroup, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
        var data_position = list.get(position)
        viewHolder.groceryText.text = "$data_position"
    }

    override fun getItemCount() = list.size
}

这是回收站视图中项目的 XML 文件。

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/groceryListTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="20dp"
            android:text="blank"
            android:textColor="@color/black"
            android:textSize="20sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <CheckBox
            android:id="@+id/groceryListCheckBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginEnd="15dp"
            android:text=""
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

该应用程序只是一个回收视图,带有 textview 和每个项目的复选框。 我想要实现的是在旋转时保存 recyclerview 的 state。 基本上,当我检查某些项目时,我希望在设备改变方向时仍然检查它们。

我在网上尝试了很多方法,保存实例或使用 ondestroy/onpause,但它们似乎都不再起作用了。 想知道我能做什么。

创建一个具有字符串和 boolean 的模型(数据类)。 change the boolean on click of check box and on onBindViewHolder, check the state of the boolean and set that state to the checkbox. 当复选框被选中/取消选中时,更改 model 中的值并调用通知数据集已更改。 当您需要获取选中的项目时,迭代列表并获取 boolean 为 true 的项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM