RecycleView滑动元素

时间:2019-07-30 06:48:14

标签: kotlin android-recyclerview swipe

我有回收适配器。当我滑动一个元素时,我可以看到如何调用OnSwipeItem方法,但是屏幕上什么都没有发生。

我在某个地方犯了一个错误,但是我不知道在哪里,因为这段代码在并行项目中可以完美地工作。

 class OrderHistoryAdapter : RecyclerView.Adapter<OrderHistoryVH>(),
        SwipeableItemAdapter<OrderHistoryVH>,
        RecyclerViewSwipeManager.OnItemSwipeEventListener {

        private var itemList = mutableListOf<ConcreteData>()
        var eventListener: EventListener? = null

        init {
            setHasStableIds(true)
        }

        //some standart adapter code

        override fun getItemId(position: Int) = itemList[position].order.aventaId.hashCode().toLong()

        override fun onSwipeItem(
            holder: OrderHistoryVH,
            position: Int,
            result: Int
        ): SwipeResultAction? {
            return when (result) {
                SwipeableItemConstants.RESULT_SWIPED_LEFT -> position.takeIf { it != RecyclerView.NO_POSITION }
                    ?.let { SwipeLeftResultAction(this, position) }
                else -> position.takeIf { it != RecyclerView.NO_POSITION }
                    ?.let { UnpinResultAction(this, position) }
            }
        }

        override fun onGetSwipeReactionType(
            holder: OrderHistoryVH,
            position: Int,
            x: Int,
            y: Int
        ): Int {
            return SwipeableItemConstants.REACTION_CAN_SWIPE_LEFT
        }

        override fun onSwipeItemStarted(holder: OrderHistoryVH, position: Int) {}
        override fun onSetSwipeBackground(holder: OrderHistoryVH, position: Int, type: Int) {}
        override fun onItemSwipeFinished(position: Int, result: Int, afterSwipeReaction: Int) {}
        override fun onItemSwipeStarted(position: Int) = closeSwipe()

        fun getItem(position: Int): ConcreteData? {
            return itemList.getOrNull(position)
        }

        fun closeSwipe() {
            itemList.forEachIndexed { index, item ->
                if (item.pinned) {
                    item.pinned = false
                    notifyItemChanged(index)
                    return
                }
            }
        }

        class OrderHistoryVH(itemView: View) : AbstractSwipeableItemViewHolder(itemView) {

            @BindDimen(R.dimen.order_item_buttons_layout_width)
            @JvmField
            var buttonsWidth = 0f
            private var maxAmount = 0f

            init {
                maxAmount = buttonsWidth / Helpers.getScreenWidth(itemView.context)
                maxLeftSwipeAmount = -maxAmount
                maxRightSwipeAmount = 0f
            }

            override fun getSwipeableContainerView(): ViewGroup = itemView.titleLayout

            fun bind(item: ConcreteData) {
                itemView.apply {
                    setTitle(item.order)
                    dateTextView.text = item.order.dateTitle
                }

                swipeItemHorizontalSlideAmount = if (item.pinned) -maxAmount else 0f
            }

            private fun setTitle(orderHistory: OrderHistory) {
                if (orderHistory.title.isEmpty()) {
                    itemView.titleTextView.text = orderHistory.messageTitle
                } else {
                    itemView.titleTextView.text = orderHistory.title
                }
            }
        }

        class ConcreteData(val order: OrderHistory) {
            var pinned: Boolean = false
        }

    }

这是片段中的代码块

 private fun initRecyclerView() {
        val layoutManager = LinearLayoutManager(activity)
        val animator = SwipeDismissItemAnimator()
        adapter = OrderHistoryAdapter()

        recyclerTouchActionGuardManager = RecyclerViewTouchActionGuardManager()
        recyclerTouchActionGuardManager.setInterceptVerticalScrollingWhileAnimationRunning(true)
        recyclerTouchActionGuardManager.isEnabled = true

        recyclerSwipeManager = RecyclerViewSwipeManager()

        animator.supportsChangeAnimations = false

        wrappedAdapter = recyclerSwipeManager.createWrappedAdapter(adapter)

        orderHistoryRecycle.layoutManager = layoutManager
        orderHistoryRecycle.adapter = wrappedAdapter
        orderHistoryRecycle.itemAnimator = animator

recyclerTouchActionGuardManager.attachRecyclerView(orderHistoryRecycle)
        recyclerSwipeManager.attachRecyclerView(orderHistoryRecycle)

        val divider = DividerItemDecoration(context, LinearLayoutManager.VERTICAL)
        divider.setDrawable(activity?.getDrawable(R.drawable.separator_horizontal)!!)
        orderHistoryRecycle.addItemDecoration(divider)

        orderHistoryRecycle.addOnScrollListener(SwipeScrollListener(adapter))

        recyclerSwipeManager.onItemSwipeEventListener = adapter
    }

如果有人至少提示可能是问题所在,我会很高兴。

0 个答案:

没有答案