I have a Recyclerview and that allow user to change scale by swipping up, I use the gestureDetector and onFling for this, this works fine, but then the user should able to click on an Item of recyclerView, i did that by creating an interface from RecyclerView and click actives from Activity, but now I have this error on swipping up.
java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter e1 at com.example.simmone.utils.SwipeServices$GestureListener.onFling(Unknown Source:2) at android.view.GestureDetector.onTouchEvent(GestureDetector.java:763) at com.example.simmone.utils.SwipeServices.onTouch(SwipeServices.kt:33)
SwipeService.kt
class SwipeServices : View.OnTouchListener {
enum class SwipeDirection {
bottomToTop
}
private var rootLayout: ViewGroup? = null
private var layoutToShowHide: ViewGroup? = null
private var gestureDetector: GestureDetector? = null
private var swipeDirections: MutableList<SwipeDirection>? = null
fun initialize(rootLayout: ViewGroup, layoutToShowHide:ViewGroup?, swipeDirections: MutableList<SwipeDirection>,maxSwipeDistance: Int = 1) {
val gestureListener = GestureListener()
gestureDetector = GestureDetector(rootLayout.context, gestureListener)
this.rootLayout = rootLayout
this.layoutToShowHide = layoutToShowHide
this.swipeDirections = swipeDirections
gestureListener.MAX_SWIPE_DISTANCE = maxSwipeDistance
this.rootLayout!!.setOnTouchListener(this)
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View?, event: MotionEvent): Boolean {
return gestureDetector?.onTouchEvent(event)!!
}
inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
var MAX_SWIPE_DISTANCE = 1
private val SWIPE_VELOCITY_THRESHOLD = 1
override fun onDown(e: MotionEvent): Boolean {
return true
}
override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean {
var result = false
try {
val diffY = e2.y - e1.y
if (abs(diffY) > MAX_SWIPE_DISTANCE && abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY<0) {
onSwipeBottomToTop()
}
}
result = true
} catch (exception: Exception) {
exception.printStackTrace()
}
return result
}
}
fun cancel() {
layoutToShowHide?.animate()?.scaleX(1f)?.scaleY(1f)?.setDuration(500)?.start();
}
fun onSwipeBottomToTop() {
layoutToShowHide?.animate()?.scaleX(.7f)?.scaleY(.7f)?.setDuration(500)?.start();
}
}
Activity class
class SwappingAppsActivity : AppCompatActivity(),
SwappingAppsAdapter.OnItemClickListener{
private lateinit var swappingAppsBinding: ActivitySwappingAppsBinding
private var swappingAppsAdapter: SwappingAppsAdapter? = null
private lateinit var selectedApp: SwappingItems
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
swappingAppsBinding = ActivitySwappingAppsBinding.inflate(layoutInflater)
setContentView(swappingAppsBinding.root)
val swappingAppViewModel = ViewModelProvider(this)[SwappingAppViewModel::class.java]
swappingAppViewModel.generateSwapItems()
swappingAppViewModel.swapList.observe(this@SwappingAppsActivity) {
swappingAppsAdapter = SwappingAppsAdapter(this@SwappingAppsActivity, it)
swappingAppsBinding.rvSwappingApps.layoutManager = LinearLayoutManager(
this@SwappingAppsActivity,
LinearLayoutManager.HORIZONTAL,
false
)
swappingAppsBinding.rvSwappingApps.adapter = swappingAppsAdapter
}
val movingElement = swappingAppsBinding.rvSwappingApps
val swipeServices = SwipeServices()
//part where i call the swap service
swipeServices.initialize(rootLayout =movingElement,movingElement,
arrayListOf(SwipeServices.SwipeDirection.bottomToTop), 50)
}
override fun onItemClick(view: View, swappingItems: SwappingItems) {
if (swappingAppsAdapter?.getItemList()?.indexOf(selectedApp)==2){
val movingElement = swappingAppsBinding.rvSwappingApps
SwipeServices().cancel()
}
}
}