添加项目时Anko DSL recyclerview奇怪的行为

时间:2017-12-21 13:08:32

标签: android kotlin anko

所以我最初在RecyclerView中有6个项目。当我添加一个项目时,我得到了一个祝酒词,表示添加了新项目并显示适配器项目增加了。但新项目是项目的副本" 6"。然后我添加更多的项目,所有" 6"。然后我向上和向下滚动,我看到" byButton"(这是通过点击一个按钮添加的按钮的名称),它们位于" 6"的重复项之间。过了一会儿整个RecycleView重置,我仍然有6个项目,就像刚开始时一样。我不知道我的代码有什么问题。

class ConnectDeviceUI(val listAdapter: DeviceListAdapter): AnkoComponent<ConnectBleActivity> {
    lateinit var addItemButton: FloatingActionButton
    override fun createView(ui: AnkoContext<ConnectBleActivity>): View = with(ui) {
        return relativeLayout() {
            lparams(width= matchParent, height = matchParent)

            textView("List of BLE devices"){
                setTextAppearance(android.R.style.TextAppearance_Material_Large)
            }

            addItemButton = floatingActionButton {
                imageResource = android.R.drawable.ic_input_add
            }.lparams{
                margin = dip(10)
                alignParentBottom()
                alignParentEnd()
                alignParentRight()
                gravity = Gravity.BOTTOM or Gravity.END
            }

            recyclerView(){
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, true)
                lparams(width= matchParent, height = matchParent)
                adapter = listAdapter

            }
        }
    }
}


class RowLayout(): AnkoComponent<ViewGroup>{
    override fun createView(ui: AnkoContext<ViewGroup>): View = with(ui) {
        return linearLayout(){
            lparams(width= matchParent, height = wrapContent)
            setPadding(0,dip(50),0,dip(50))

            textView {
                id = R.id.ble_item
                setTextAppearance(android.R.style.TextAppearance_Material_Large)
                setPadding(0,0,dip(100),0)
            }.lparams(width = wrapContent, height = wrapContent )

            button{
                id = R.id.ble_item_button
            }.lparams(width = wrapContent, height = wrapContent)

        }
    }
}


class ConnectBleActivity : AppCompatActivity(), AnkoLogger {

    lateinit var BleDevicesList: ArrayList<String>

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

        BleDevicesList = arrayListOf("1", "2","3", "4", "5", "6")
        var adapter = DeviceListAdapter(BleDevicesList)
        var ui = ConnectDeviceUI(adapter)
        ui.setContentView(this)
        ui.addItemButton.onClick {
            adapter.put()
            toast("New item added. Number of items: ${adapter.itemCount} ")
        }

    }
}




class DeviceListAdapter(var deviceList: ArrayList<String>): RecyclerView.Adapter<DeviceListHolder>(){
    override fun onBindViewHolder(holder: DeviceListHolder?, position: Int) {
        holder?.bindItems(deviceList[position])
    }
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): DeviceListHolder {
        return DeviceListHolder(RowLayout().createView(AnkoContext.create(parent!!.context, parent)))
    }
    override fun getItemCount(): Int {
        return deviceList.size
    }

    fun put(){
        val randomString = UUID.randomUUID().toString()
        deviceList.add(deviceList.lastIndex, "byButton")
        notifyItemInserted(deviceList.lastIndex)
    }

    fun drop(){
        deviceList.removeAt(deviceList.lastIndex)
        notifyItemRemoved(deviceList.lastIndex)
    }


}

class DeviceListHolder(var view: View): RecyclerView.ViewHolder(view){
    val name: TextView = view.find(R.id.ble_item)
    val bt_name: Button = view.find(R.id.ble_item_button)
    fun bindItems(listItem: String){
        name.text = listItem
        bt_name.text = "Test"
    }

}

1 个答案:

答案 0 :(得分:1)

修改列表本身后使用List.lastIndex()会导致在模型和视图中更新不同的值

deviceList.add(deviceList.lastIndex, "byButton")
notifyItemInserted(deviceList.lastIndex)

通知告诉RecyclerView仅更新最后一项。这包含"6",而旧位置根本不更新。要解决此问题,您必须为两者使用相同的索引。

val index = deviceList.lastIndex
deviceList.add(index, "byButton")
notifyItemInserted(index)