优化代码

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

Loading…
Cancel
Save