首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在添加项目时,Anko DSL回收器查看异常行为

在添加项目时,Anko DSL回收器查看异常行为
EN

Stack Overflow用户
提问于 2017-12-21 21:08:32
回答 1查看 1.1K关注 0票数 2

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

代码语言:javascript
运行
复制
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"
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-25 05:53:17

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

代码语言:javascript
运行
复制
deviceList.add(deviceList.lastIndex, "byButton")
notifyItemInserted(deviceList.lastIndex)

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

代码语言:javascript
运行
复制
val index = deviceList.lastIndex
deviceList.add(index, "byButton")
notifyItemInserted(index)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47925718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档