如何使用协同程序正确初始化Kotlin中的Recyclerview?

时间:2019-07-28 15:04:36

标签: android kotlin android-recyclerview kotlin-coroutines

我正在与Coroutines一起学习kotlin Android,但是我对正确启动RecyclerView遇到问题。 我在onCreate方法之外具有函数,但是无法将Arraylist从此方法传递给onCreate中的适配器。正如您在我的代码中看到的那样,我在getCities函数中设置了location_recycler_view.apply,但是随后我看到了:E / RecyclerView:未连接适配器;跳过日志中的布局。如何在我的情况下正确设置Recyclerview的代码?

  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_add_location)

    addLocationViewModel = ViewModelProviders.of(this, addLocationViewModelFactory).get(AddLocationViewModel::class.java)

    window.setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN)

    back_button.setOnClickListener {
        onBackPressed()
    }
    getCities()
    viewAdapter = LocationAdapter(cities, this) //in this case I can get normal list, but no list from ViewModel
    viewManager = LinearLayoutManager(this)

    deleteIcon = ContextCompat.getDrawable(this, R.drawable.ic_delete_white_24dp)!!

    location_recycler_view.apply {    //in this case I can set Recyclerview in normal way
        adapter = viewAdapter
        layoutManager = viewManager
    }


    val itemTouchHelperCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
        override fun onMove(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            target: RecyclerView.ViewHolder
        ): Boolean {
            return false
        }

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, position: Int) {
            (viewAdapter as LocationAdapter).removeItem(viewHolder)

            Log.e("ALA onSwiped", viewHolder.adapterPosition.toString())
            fun deleteLocation() = launch {
                addLocationViewModel.deleteLocation((viewAdapter as LocationAdapter).getCityAt(viewHolder.adapterPosition))
                Log.e("ALA onSwiped", viewHolder.adapterPosition.toString())
            }
        }

        override fun onChildDraw(
            c: Canvas,
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            dX: Float,
            dY: Float,
            actionState: Int,
            isCurrentlyActive: Boolean
        ) {
            val itemView = viewHolder.itemView

            val iconMargin = (itemView.height - deleteIcon.intrinsicHeight) / 2

            if (dX > 0) {
                swipeBackground.setBounds(itemView.left, itemView.top, dX.toInt(), itemView.bottom)
                deleteIcon.setBounds(itemView.left + iconMargin, itemView.top + iconMargin, itemView.left + iconMargin + deleteIcon.intrinsicWidth,
                    itemView.bottom - iconMargin)
            } else {
                swipeBackground.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
                deleteIcon.setBounds(itemView.right - iconMargin - deleteIcon.intrinsicWidth, itemView.top + iconMargin, itemView.right - iconMargin,
                    itemView.bottom - iconMargin)
            }

            swipeBackground.draw(c)

            c.save()

            if (dX > 0)
                c.clipRect(itemView.left, itemView.top, dX.toInt(), itemView.bottom)
            else
                c.clipRect(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)

            deleteIcon.draw(c)

            c.restore()

            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
        }

    }

    val itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
    itemTouchHelper.attachToRecyclerView(location_recycler_view)

    add_city_button.setOnClickListener {
        if (location_edit_text.text.toString().trim().isEmpty()) {
            Toast.makeText(this, "Location field is empty!", Toast.LENGTH_SHORT).show()
        }
         else {
            saveLocation()
        }
    }
}

private fun getCities() = launch(Dispatchers.Main) {
     val locationList = addLocationViewModel.locations.await()
    locationList.observe(this@AddLocationActivity, Observer {
        locationList -> viewAdapter = LocationAdapter(locationList as ArrayList<Location>, this@AddLocationActivity)
        location_recycler_view.apply {
            adapter = viewAdapter       //if I'm using this RV works, but I see error in logs
            layoutManager = viewManager
        }
    })
}

1 个答案:

答案 0 :(得分:0)

似乎您必须使用LocationAdapter方法中的空列表来初始化onCreate,然后使用getCities方法中的城市列表来设置适配器,如下所示:

viewAdapter.cities = locationList
viewAdapter.notifyDataSetChanged()