嗨,大家好,

我是Android开发的新手。我对Recyclerview的滑动有了一个问题:
是否可以仅为某些物品而不是整体启用滑动?

@keldii.,

很高兴听到你的消息。 :least_smile:

是的,可以为某些项目启用滑动。

我从这个项目中拿走了这个项目 Android Recyclerview教程, then updated the setRecyclerViewItemToucherListener method.

private fun setRecyclerViewItemTouchListener() {

    val itemTouchCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
      override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, viewHolder1: RecyclerView.ViewHolder): Boolean {

        return false
      }

      override fun onSwiped(viewHolder: RecyclerView.ViewHolder, swipeDir: Int) {

       // If the Viewholder is a PhotoHolder type, swipe it away!
        if(viewHolder is RecyclerAdapter.PhotoHolder) {
          val position = viewHolder.adapterPosition
          photosList.removeAt(position)
          recyclerView.adapter!!.notifyItemRemoved(position)
        } else {
          // We have a different view holder type, don't do anything!
        }
      }
    }

    val itemTouchHelper = ItemTouchHelper(itemTouchCallback)
    itemTouchHelper.attachToRecyclerView(recyclerView)
  }

The ItemTouchHelper.onSwiped method contains a RecyclerView.ViewHolder object as a parameter, you can use the Viewholder to check if it’s an item you would like to be swiped away.

我附上了这个项目让你试试,我还建议通过上面的Recyclerview教程进行。

有任何问题,请告诉我。

达里尔

galacticon-check-recyclerview-item.zip. (347.5 KB)