1 /** 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 package com.android.healthconnect.controller.datasources.appsources 15 16 import android.graphics.Canvas 17 import androidx.recyclerview.widget.ItemTouchHelper 18 import androidx.recyclerview.widget.RecyclerView 19 20 class AppSourcesItemMoveCallback(private val adapter: AppSourcesAdapter) : 21 ItemTouchHelper.Callback() { 22 isLongPressDragEnablednull23 override fun isLongPressDragEnabled(): Boolean = false 24 25 override fun isItemViewSwipeEnabled(): Boolean = false 26 27 override fun getMovementFlags( 28 recyclerView: RecyclerView, 29 viewHolder: RecyclerView.ViewHolder 30 ): Int { 31 val dragFlags: Int = ItemTouchHelper.UP or ItemTouchHelper.DOWN 32 return makeMovementFlags(dragFlags, /* swipeFlags= */ 0) 33 } 34 onMovenull35 override fun onMove( 36 recyclerView: RecyclerView, 37 viewHolder: RecyclerView.ViewHolder, 38 target: RecyclerView.ViewHolder 39 ): Boolean { 40 return adapter.onItemMove(viewHolder.bindingAdapterPosition, target.bindingAdapterPosition) 41 } 42 onSwipednull43 override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {} 44 onChildDrawnull45 override fun onChildDraw( 46 c: Canvas, 47 recyclerView: RecyclerView, 48 viewHolder: RecyclerView.ViewHolder, 49 dX: Float, 50 dY: Float, 51 actionState: Int, 52 isCurrentlyActive: Boolean 53 ) { 54 val topY: Float = viewHolder.itemView.top + dY 55 val bottomY: Float = topY + viewHolder.itemView.height 56 57 // Only redraw child if it is inbounds of view 58 if (topY > 0 && bottomY < recyclerView.height) { 59 super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive) 60 } 61 } 62 }