Skip to content

Commit

Permalink
update ignoreType bug
Browse files Browse the repository at this point in the history
  • Loading branch information
goyourfly committed Jul 21, 2017
1 parent 2ec33e7 commit 409ab48
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,11 @@ class MultipleAdapter(val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>
}
}

override fun onCreateViewHolder(viewGroup: ViewGroup, position: Int): RecyclerView.ViewHolder {
val outerHolder = adapter.onCreateViewHolder(viewGroup, position)
if (isIgnore(position))
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val outerHolder = adapter.onCreateViewHolder(viewGroup, viewType)
if(ignoreType != null && ignoreType.contains(viewType)){
return outerHolder
}
return decorateFactory.decorate(outerHolder, this)
}

Expand Down Expand Up @@ -172,7 +173,8 @@ class MultipleAdapter(val adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>
* 判断这个类型是否被忽略
*/
fun isIgnore(position: Int): Boolean {
if (ignoreType == null)
if (ignoreType == null
|| ignoreType.isEmpty())
return false
val type = getItemViewType(position)
return ignoreType.contains(type)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
package com.goyourfly.multiple.adapter.viewholder

import android.os.Handler
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import com.goyourfly.multiple.adapter.MultipleAdapter
import com.goyourfly.multiple.adapter.ViewState

/**
* Created by gaoyufei on 2017/6/8.
*/
abstract class BaseViewHolder(val root: View,
val viewHolder: RecyclerView.ViewHolder,
val adapter: MultipleAdapter) : RecyclerView.ViewHolder(InterceptFrameLayout(root.context,adapter,root)) {
val adapter: MultipleAdapter) : RecyclerView.ViewHolder(EventObserverView(root.context,adapter,root)) {

val onTouchListener = OnTouchListener(adapter,this)
init {
// viewHolder.itemView.setOnTouchListener(onTouchListener)
if(itemView is InterceptFrameLayout){
if(itemView is EventObserverView){
itemView.setLongClickCallback {
Log.d("...","onLongClickCallback")
adapter.onItemLongClick(viewHolder.adapterPosition)
}
itemView.setSelectStateClickCallback {
Expand All @@ -34,69 +26,4 @@ abstract class BaseViewHolder(val root: View,

open fun showStateChanged(toState: Int) {}

class OnTouchListener(val adapter: MultipleAdapter, val holder: RecyclerView.ViewHolder) : View.OnTouchListener {
var startX = 0F
var startY = 0F
var moveX = 0F
var moveY = 0F
var startTime = 0L
val CLICK_ACTION_THRESHHOLD = 20
val CLICK_LONG_TIME = 500L
var isTouching = false
val handler = Handler()
val run = Runnable {
if(isTouching && isAClick(startX,moveX,startY,moveY)){
adapter.onItemLongClick(holder.adapterPosition)
}
}

override fun onTouch(v: View, event: MotionEvent): Boolean {
when (event.action) {
MotionEvent.ACTION_DOWN -> {
isTouching = true
startX = event.x
startY = event.y
moveX = startX
moveY = startY
startTime = System.currentTimeMillis()
handler.postDelayed(run,CLICK_LONG_TIME)
}

MotionEvent.ACTION_MOVE ->{
moveX = event.x
moveY = event.y
}

MotionEvent.ACTION_UP -> {
val endX = event.x
val endY = event.y
if (isTouching && isAClick(startX, endX, startY, endY)) {
if (adapter.showState == ViewState.SELECT) {
adapter.onItemClick(holder.adapterPosition)
} else {
v.performClick()
}
}
isTouching = false
handler.removeCallbacks(run)
}

MotionEvent.ACTION_CANCEL -> {
isTouching = false
handler.removeCallbacks(run)
}
}
return true
}

private fun isAClick(startX: Float, endX: Float, startY: Float, endY: Float): Boolean {
val differenceX = Math.abs(startX - endX)
val differenceY = Math.abs(startY - endY)
if (differenceX > CLICK_ACTION_THRESHHOLD/* =5 */ || differenceY > CLICK_ACTION_THRESHHOLD) {
return false
}
return true
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,36 @@ package com.goyourfly.multiple.adapter.viewholder

import android.content.Context
import android.support.v4.view.MotionEventCompat
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.widget.FrameLayout
import com.goyourfly.multiple.adapter.MultipleAdapter
import com.goyourfly.multiple.adapter.ViewState

/**
* Created by gaoyufei on 2017/7/20.
* 用于检测和拦截长按手势的ViewGroup
*/

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

var startX = 0F
var startY = 0F
var moveX = 0F
var moveY = 0F
var downTime = 0L
var isTouching = false
var isLongClick = false
var touchDownTime = 0L
var mHasPerformedLongPress = false;

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

val run = Runnable {
if (isTouching && !isLongClick && isAClick()) {
// 制作一个假的事件
val event = MotionEvent.obtain(
downTime,
System.currentTimeMillis(),
MotionEvent.ACTION_MOVE,
moveX,
moveY,
0)
isLongClick = true
onTouchEvent(event)
if (isTouching && !mHasPerformedLongPress && isAClick()) {
onLongClick()
}
}

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


override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
Log.d("...", "onIntercept:$ev")
val action = MotionEventCompat.getActionMasked(ev)
var isLongClick = false
when (action) {
MotionEvent.ACTION_DOWN -> {
isLongClick = false
mHasPerformedLongPress = false
isTouching = true
startX = ev.x
startY = ev.y
moveX = ev.x
moveY = ev.y
downTime = System.currentTimeMillis()
handler.postDelayed(run, CLICK_LONG_TIME)
postDelayed(run, CLICK_LONG_TIME)
}
MotionEvent.ACTION_MOVE -> {
moveX = ev.x
Expand All @@ -78,51 +70,48 @@ internal class InterceptFrameLayout(context: Context, val adapter: MultipleAdapt
}
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
isTouching = false
handler.removeCallbacks(run)
removeCallbacks(run)
}
}
return isLongClick || adapter.showState == ViewState.SELECT
}

override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
return super.dispatchTouchEvent(ev)
}

fun onLongClick() {
removeCallbacks(run)
post {
mHasPerformedLongPress = true
onLongClicked?.invoke()
}
}

override fun onTouchEvent(ev: MotionEvent): Boolean {
val action = MotionEventCompat.getActionMasked(ev)
Log.d("...", "onTouch:$ev")
if (isTouching
&& isLongClick
&& action != MotionEvent.ACTION_CANCEL) {
handler.removeCallbacks(run)
handler.post {
onLongClicked?.invoke()
}
return true
}
when (action) {
MotionEvent.ACTION_DOWN -> {
isTouching = true
mHasPerformedLongPress = false
startX = ev.x
startY = ev.y
moveX = ev.x
moveY = ev.y
touchDownTime = System.currentTimeMillis()
}
MotionEvent.ACTION_MOVE -> {
moveX = ev.x
moveY = ev.y
}
MotionEvent.ACTION_UP -> {
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
isTouching = false
handler.removeCallbacks(run)
if (adapter.showState == ViewState.SELECT
&& !isTouchLong()
removeCallbacks(run)
if (action == MotionEvent.ACTION_UP
&& adapter.showState == ViewState.SELECT
&& isAClick()) {
onClick?.invoke()
}
}
MotionEvent.ACTION_CANCEL -> {
isTouching = false
handler.removeCallbacks(run)
}
}
return true
}
Expand All @@ -137,7 +126,6 @@ internal class InterceptFrameLayout(context: Context, val adapter: MultipleAdapt
}

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

private fun isAClick(): Boolean {
val differenceX = Math.abs(startX - moveX)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class CustomViewHolder(root: View,
val selectViewContainer: View,
val selectView: View,
val unSelectView: View) : BaseViewHolder(root, viewHolder, adapter) {
init {
selectView.setOnTouchListener(onTouchListener)
unSelectView.setOnTouchListener(onTouchListener)
}
// init {
// selectView.setOnTouchListener(onTouchListener)
// unSelectView.setOnTouchListener(onTouchListener)
// }


override fun selectStateChanged(state: Int) {
Expand Down Expand Up @@ -53,6 +53,7 @@ class CustomViewHolder(root: View,
}



fun Float.toPx(context: android.content.Context): Int {
return android.util.TypedValue.applyDimension(android.util.TypedValue.COMPLEX_UNIT_DIP, this, context.resources.getDisplayMetrics()).toInt();
}
Expand Down

0 comments on commit 409ab48

Please sign in to comment.