我有一个 Recyclerview,允许用户通过向上滑动来更改比例,我为此使用gestureDetector 和 onFling,这工作正常,但是用户应该能够单击 recyclerView 的项目,我通过创建一个界面来做到这一点RecyclerView 并单击“活动”中的“活动”,但现在向上滑动时出现此错误。
java.lang.NullPointerException:指定为非 null 的参数为 null:方法 kotlin.jvm.internal.Intrinsics.checkNotNullParameter,参数 e1
在 com.example.simmone.utils.SwipeServices$GestureListener.onFling(来源未知:2)
在 android.view.GestureDetector.onTouchEvent(GestureDetector.java:763)
在 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();
}
}
活动课
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()
}
}
}
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)