优化代码

pull/352/head
gedoor 4 years ago
parent ea40d0d285
commit a3054307f4
  1. 32
      app/src/main/java/io/legado/app/ui/widget/recycler/DragSelectTouchHelper.kt

@ -190,7 +190,8 @@ class DragSelectTouchHelper(
private var mLastRealEnd = RecyclerView.NO_POSITION
private var mSlideStateStartPosition = RecyclerView.NO_POSITION
private var mHaveCalledSelectStart = false
private val mScrollRunnable: Runnable = object : Runnable {
private val mScrollRunnable: Runnable by lazy {
object : Runnable {
override fun run() {
if (mIsScrolling) {
scrollBy(mScrollDistance)
@ -198,7 +199,9 @@ class DragSelectTouchHelper(
}
}
}
private val mOnItemTouchListener: OnItemTouchListener = object : OnItemTouchListener {
}
private val mOnItemTouchListener: OnItemTouchListener by lazy {
object : OnItemTouchListener {
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
Logger.d("onInterceptTouchEvent: x:" + e.x + ",y:" + e.y
+ ", " + MotionEvent.actionToString(e.action))
@ -292,6 +295,7 @@ class DragSelectTouchHelper(
}
}
}
}
init {
setHotspotRatio(DEFAULT_HOTSPOT_RATIO)
@ -316,13 +320,11 @@ class DragSelectTouchHelper(
if (mRecyclerView === recyclerView) {
return // nothing to do
}
if (mRecyclerView != null) {
mRecyclerView!!.removeOnItemTouchListener(mOnItemTouchListener)
}
mRecyclerView?.removeOnItemTouchListener(mOnItemTouchListener)
mRecyclerView = recyclerView
if (mRecyclerView != null) {
mRecyclerView!!.addOnItemTouchListener(mOnItemTouchListener)
mRecyclerView!!.addOnLayoutChangeListener(mOnLayoutChangeListener)
mRecyclerView?.let {
it.addOnItemTouchListener(mOnItemTouchListener)
it.addOnLayoutChangeListener(mOnLayoutChangeListener)
}
}
@ -510,8 +512,8 @@ class DragSelectTouchHelper(
private fun activeSelectInternal(position: Int) {
// We should initialize the hotspot here, because its data may be delayed load
if (mRecyclerView != null) {
init(mRecyclerView!!.height)
mRecyclerView?.let {
init(it.height)
}
if (position == RecyclerView.NO_POSITION) {
Logger.logSelectStateChange(mSelectState, SELECT_STATE_SLIDE)
@ -553,7 +555,7 @@ class DragSelectTouchHelper(
updateSelectedRange(rv, e.x, e.y)
}
private fun updateSelectedRange(rv: RecyclerView?, x: Float, y: Float) {
private fun updateSelectedRange(rv: RecyclerView, x: Float, y: Float) {
val position = getItemPosition(rv, x, y)
if (position != RecyclerView.NO_POSITION && mEnd != position) {
mEnd = position
@ -685,7 +687,7 @@ class DragSelectTouchHelper(
private fun stopAutoScroll() {
if (mIsScrolling) {
mIsScrolling = false
mRecyclerView!!.removeCallbacks(mScrollRunnable)
mRecyclerView?.removeCallbacks(mScrollRunnable)
}
}
@ -698,7 +700,7 @@ class DragSelectTouchHelper(
}
mRecyclerView!!.scrollBy(0, scrollDistance)
if (mLastX != Float.MIN_VALUE && mLastY != Float.MIN_VALUE) {
updateSelectedRange(mRecyclerView, mLastX, mLastY)
updateSelectedRange(mRecyclerView!!, mLastX, mLastY)
}
}
@ -720,8 +722,8 @@ class DragSelectTouchHelper(
return getItemPosition(rv, e.x, e.y)
}
private fun getItemPosition(rv: RecyclerView?, x: Float, y: Float): Int {
val v = rv!!.findChildViewUnder(x, y)
private fun getItemPosition(rv: RecyclerView, x: Float, y: Float): Int {
val v = rv.findChildViewUnder(x, y)
if (v == null) {
val layoutManager = rv.layoutManager
if (layoutManager is GridLayoutManager) {

Loading…
Cancel
Save