Skip to content

Commit 409ab48

Browse files
committed
update ignoreType bug
1 parent 2ec33e7 commit 409ab48

File tree

4 files changed

+42
-124
lines changed

4 files changed

+42
-124
lines changed

library/src/main/java/com/goyourfly/multiple/adapter/MultipleAdapter.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,11 @@ class MultipleAdapter(val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>
8282
}
8383
}
8484

85-
override fun onCreateViewHolder(viewGroup: ViewGroup, position: Int): RecyclerView.ViewHolder {
86-
val outerHolder = adapter.onCreateViewHolder(viewGroup, position)
87-
if (isIgnore(position))
85+
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
86+
val outerHolder = adapter.onCreateViewHolder(viewGroup, viewType)
87+
if(ignoreType != null && ignoreType.contains(viewType)){
8888
return outerHolder
89+
}
8990
return decorateFactory.decorate(outerHolder, this)
9091
}
9192

@@ -172,7 +173,8 @@ class MultipleAdapter(val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>
172173
* 判断这个类型是否被忽略
173174
*/
174175
fun isIgnore(position: Int): Boolean {
175-
if (ignoreType == null)
176+
if (ignoreType == null
177+
|| ignoreType.isEmpty())
176178
return false
177179
val type = getItemViewType(position)
178180
return ignoreType.contains(type)
Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
package com.goyourfly.multiple.adapter.viewholder
22

3-
import android.os.Handler
43
import android.support.v7.widget.RecyclerView
5-
import android.util.Log
6-
import android.view.MotionEvent
74
import android.view.View
8-
import android.view.ViewGroup
95
import com.goyourfly.multiple.adapter.MultipleAdapter
10-
import com.goyourfly.multiple.adapter.ViewState
116

127
/**
138
* Created by gaoyufei on 2017/6/8.
149
*/
1510
abstract class BaseViewHolder(val root: View,
1611
val viewHolder: RecyclerView.ViewHolder,
17-
val adapter: MultipleAdapter) : RecyclerView.ViewHolder(InterceptFrameLayout(root.context,adapter,root)) {
12+
val adapter: MultipleAdapter) : RecyclerView.ViewHolder(EventObserverView(root.context,adapter,root)) {
1813

19-
val onTouchListener = OnTouchListener(adapter,this)
2014
init {
21-
// viewHolder.itemView.setOnTouchListener(onTouchListener)
22-
if(itemView is InterceptFrameLayout){
15+
if(itemView is EventObserverView){
2316
itemView.setLongClickCallback {
24-
Log.d("...","onLongClickCallback")
2517
adapter.onItemLongClick(viewHolder.adapterPosition)
2618
}
2719
itemView.setSelectStateClickCallback {
@@ -34,69 +26,4 @@ abstract class BaseViewHolder(val root: View,
3426

3527
open fun showStateChanged(toState: Int) {}
3628

37-
class OnTouchListener(val adapter: MultipleAdapter, val holder: RecyclerView.ViewHolder) : View.OnTouchListener {
38-
var startX = 0F
39-
var startY = 0F
40-
var moveX = 0F
41-
var moveY = 0F
42-
var startTime = 0L
43-
val CLICK_ACTION_THRESHHOLD = 20
44-
val CLICK_LONG_TIME = 500L
45-
var isTouching = false
46-
val handler = Handler()
47-
val run = Runnable {
48-
if(isTouching && isAClick(startX,moveX,startY,moveY)){
49-
adapter.onItemLongClick(holder.adapterPosition)
50-
}
51-
}
52-
53-
override fun onTouch(v: View, event: MotionEvent): Boolean {
54-
when (event.action) {
55-
MotionEvent.ACTION_DOWN -> {
56-
isTouching = true
57-
startX = event.x
58-
startY = event.y
59-
moveX = startX
60-
moveY = startY
61-
startTime = System.currentTimeMillis()
62-
handler.postDelayed(run,CLICK_LONG_TIME)
63-
}
64-
65-
MotionEvent.ACTION_MOVE ->{
66-
moveX = event.x
67-
moveY = event.y
68-
}
69-
70-
MotionEvent.ACTION_UP -> {
71-
val endX = event.x
72-
val endY = event.y
73-
if (isTouching && isAClick(startX, endX, startY, endY)) {
74-
if (adapter.showState == ViewState.SELECT) {
75-
adapter.onItemClick(holder.adapterPosition)
76-
} else {
77-
v.performClick()
78-
}
79-
}
80-
isTouching = false
81-
handler.removeCallbacks(run)
82-
}
83-
84-
MotionEvent.ACTION_CANCEL -> {
85-
isTouching = false
86-
handler.removeCallbacks(run)
87-
}
88-
}
89-
return true
90-
}
91-
92-
private fun isAClick(startX: Float, endX: Float, startY: Float, endY: Float): Boolean {
93-
val differenceX = Math.abs(startX - endX)
94-
val differenceY = Math.abs(startY - endY)
95-
if (differenceX > CLICK_ACTION_THRESHHOLD/* =5 */ || differenceY > CLICK_ACTION_THRESHHOLD) {
96-
return false
97-
}
98-
return true
99-
}
100-
101-
}
10229
}

library/src/main/java/com/goyourfly/multiple/adapter/viewholder/InterceptFrameLayout.kt renamed to library/src/main/java/com/goyourfly/multiple/adapter/viewholder/EventObserverView.kt

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,36 @@ package com.goyourfly.multiple.adapter.viewholder
22

33
import android.content.Context
44
import android.support.v4.view.MotionEventCompat
5-
import android.util.Log
65
import android.view.MotionEvent
76
import android.view.View
7+
import android.view.ViewConfiguration
88
import android.widget.FrameLayout
99
import com.goyourfly.multiple.adapter.MultipleAdapter
1010
import com.goyourfly.multiple.adapter.ViewState
1111

1212
/**
1313
* Created by gaoyufei on 2017/7/20.
14+
* 用于检测和拦截长按手势的ViewGroup
1415
*/
1516

16-
internal class InterceptFrameLayout(context: Context, val adapter: MultipleAdapter, child: View) : FrameLayout(context) {
17-
val CLICK_ACTION_THRESHHOLD = 20
18-
val CLICK_LONG_TIME = 500L
17+
internal class EventObserverView(context: Context, val adapter: MultipleAdapter, child: View) : FrameLayout(context) {
18+
val CLICK_ACTION_THRESHHOLD = ViewConfiguration.get(context).getScaledTouchSlop()
19+
val CLICK_LONG_TIME = ViewConfiguration.getLongPressTimeout().toLong()
1920

2021
var startX = 0F
2122
var startY = 0F
2223
var moveX = 0F
2324
var moveY = 0F
2425
var downTime = 0L
2526
var isTouching = false
26-
var isLongClick = false
27-
var touchDownTime = 0L
27+
var mHasPerformedLongPress = false;
2828

2929
private var onLongClicked: (() -> Unit)? = null
3030
private var onClick: (() -> Unit)? = null
3131

3232
val run = Runnable {
33-
if (isTouching && !isLongClick && isAClick()) {
34-
// 制作一个假的事件
35-
val event = MotionEvent.obtain(
36-
downTime,
37-
System.currentTimeMillis(),
38-
MotionEvent.ACTION_MOVE,
39-
moveX,
40-
moveY,
41-
0)
42-
isLongClick = true
43-
onTouchEvent(event)
33+
if (isTouching && !mHasPerformedLongPress && isAClick()) {
34+
onLongClick()
4435
}
4536
}
4637

@@ -55,19 +46,20 @@ internal class InterceptFrameLayout(context: Context, val adapter: MultipleAdapt
5546
addView(child)
5647
}
5748

49+
5850
override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
59-
Log.d("...", "onIntercept:$ev")
6051
val action = MotionEventCompat.getActionMasked(ev)
52+
var isLongClick = false
6153
when (action) {
6254
MotionEvent.ACTION_DOWN -> {
63-
isLongClick = false
55+
mHasPerformedLongPress = false
6456
isTouching = true
6557
startX = ev.x
6658
startY = ev.y
6759
moveX = ev.x
6860
moveY = ev.y
6961
downTime = System.currentTimeMillis()
70-
handler.postDelayed(run, CLICK_LONG_TIME)
62+
postDelayed(run, CLICK_LONG_TIME)
7163
}
7264
MotionEvent.ACTION_MOVE -> {
7365
moveX = ev.x
@@ -78,51 +70,48 @@ internal class InterceptFrameLayout(context: Context, val adapter: MultipleAdapt
7870
}
7971
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
8072
isTouching = false
81-
handler.removeCallbacks(run)
73+
removeCallbacks(run)
8274
}
8375
}
8476
return isLongClick || adapter.showState == ViewState.SELECT
8577
}
8678

79+
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
80+
return super.dispatchTouchEvent(ev)
81+
}
82+
83+
fun onLongClick() {
84+
removeCallbacks(run)
85+
post {
86+
mHasPerformedLongPress = true
87+
onLongClicked?.invoke()
88+
}
89+
}
8790

8891
override fun onTouchEvent(ev: MotionEvent): Boolean {
8992
val action = MotionEventCompat.getActionMasked(ev)
90-
Log.d("...", "onTouch:$ev")
91-
if (isTouching
92-
&& isLongClick
93-
&& action != MotionEvent.ACTION_CANCEL) {
94-
handler.removeCallbacks(run)
95-
handler.post {
96-
onLongClicked?.invoke()
97-
}
98-
return true
99-
}
10093
when (action) {
10194
MotionEvent.ACTION_DOWN -> {
10295
isTouching = true
96+
mHasPerformedLongPress = false
10397
startX = ev.x
10498
startY = ev.y
10599
moveX = ev.x
106100
moveY = ev.y
107-
touchDownTime = System.currentTimeMillis()
108101
}
109102
MotionEvent.ACTION_MOVE -> {
110103
moveX = ev.x
111104
moveY = ev.y
112105
}
113-
MotionEvent.ACTION_UP -> {
106+
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
114107
isTouching = false
115-
handler.removeCallbacks(run)
116-
if (adapter.showState == ViewState.SELECT
117-
&& !isTouchLong()
108+
removeCallbacks(run)
109+
if (action == MotionEvent.ACTION_UP
110+
&& adapter.showState == ViewState.SELECT
118111
&& isAClick()) {
119112
onClick?.invoke()
120113
}
121114
}
122-
MotionEvent.ACTION_CANCEL -> {
123-
isTouching = false
124-
handler.removeCallbacks(run)
125-
}
126115
}
127116
return true
128117
}
@@ -137,7 +126,6 @@ internal class InterceptFrameLayout(context: Context, val adapter: MultipleAdapt
137126
}
138127

139128
private fun isInterceptLong() = System.currentTimeMillis() - downTime > CLICK_LONG_TIME
140-
private fun isTouchLong() = System.currentTimeMillis() - touchDownTime > CLICK_LONG_TIME
141129

142130
private fun isAClick(): Boolean {
143131
val differenceX = Math.abs(startX - moveX)

library/src/main/java/com/goyourfly/multiple/adapter/viewholder/view/CustomViewHolder.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ class CustomViewHolder(root: View,
1919
val selectViewContainer: View,
2020
val selectView: View,
2121
val unSelectView: View) : BaseViewHolder(root, viewHolder, adapter) {
22-
init {
23-
selectView.setOnTouchListener(onTouchListener)
24-
unSelectView.setOnTouchListener(onTouchListener)
25-
}
22+
// init {
23+
// selectView.setOnTouchListener(onTouchListener)
24+
// unSelectView.setOnTouchListener(onTouchListener)
25+
// }
2626

2727

2828
override fun selectStateChanged(state: Int) {
@@ -53,6 +53,7 @@ class CustomViewHolder(root: View,
5353
}
5454

5555

56+
5657
fun Float.toPx(context: android.content.Context): Int {
5758
return android.util.TypedValue.applyDimension(android.util.TypedValue.COMPLEX_UNIT_DIP, this, context.resources.getDisplayMetrics()).toInt();
5859
}

0 commit comments

Comments
 (0)