Skip to content

Commit

Permalink
适配器中增加onRelease、onCancel两个方法,提升了灵活性
Browse files Browse the repository at this point in the history
  • Loading branch information
lang-v committed Oct 19, 2020
1 parent 8f019ee commit 3fb33f2
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@ import android.animation.TimeInterpolator
import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.core.view.NestedScrollingParent2
import androidx.core.view.ViewCompat
import java.lang.Math.pow
import kotlin.math.abs
import kotlin.math.pow
import kotlin.math.tanh

class ElasticLayout @JvmOverloads constructor(
context: Context,
Expand Down Expand Up @@ -111,7 +108,6 @@ class ElasticLayout @JvmOverloads constructor(
}) != 0
}


/**
* @param consumed 记录parent消耗的距离,consumed[0]-->X [1]-->y
* 如果parent消耗完,那么child就不会继续处理了
Expand Down Expand Up @@ -189,25 +185,41 @@ class ElasticLayout @JvmOverloads constructor(
if (headerAdapter != null && scrollOffset < 0 && scrollOffset <= -headerAdapter!!.offset) {
springBack(scrollOffset + headerAdapter!!.offset, animTimeShort)
if (isLoadingOrRefreshing()) return
isRefreshing = true
headerAdapter!!.onDo()
listener?.onRefresh()
headerAdapter!!.onRelease()
if(cancel){
cancel = false
headerAdapter!!.onCancel()
springBack(getScrollOffset(),cancelAnimationTime)
}else {
isRefreshing = true
headerAdapter!!.isDoing = true
headerAdapter!!.onDo()
listener?.onRefresh()
}
return
}
//达到加载条件
if (footerAdapter != null && scrollOffset > 0 && scrollOffset >= footerAdapter!!.offset) {
springBack(scrollOffset - footerAdapter!!.offset, animTimeShort)
if (isLoadingOrRefreshing()) return
footerAdapter!!.isDoing = true
isLoading = true
footerAdapter!!.onDo()
listener?.onLoad()
footerAdapter!!.onRelease()
if(cancel){
cancel = false
footerAdapter!!.onCancel()
springBack(getScrollOffset(),cancelAnimationTime)
}else {
footerAdapter!!.isDoing = true
isLoading = true
footerAdapter!!.onDo()
listener?.onLoad()
}
return
}
springBack(scrollOffset, animTimeLong)
}

override fun scrollBy(x: Int, y: Int) {
if(scrollListener?.preOnScrolled(scrollX,scrollY,x,y)!!)return
//根据布局选择移动水平垂直
if (orientation == VERTICAL) {
super.scrollBy(0, y)
Expand Down Expand Up @@ -283,7 +295,7 @@ class ElasticLayout @JvmOverloads constructor(
*/
fun headerRefreshStop(msg: String) {
if (headerAdapter == null) return
headerAdapter!!.overDo(msg)
headerAdapter!!.onDone(msg)
postDelayed({
springBack(getScrollOffset(), animTimeLong)
}, 300)
Expand All @@ -294,7 +306,7 @@ class ElasticLayout @JvmOverloads constructor(
*/
fun footerLoadStop(msg: String) {
if (footerAdapter == null) return
footerAdapter!!.overDo(msg)
footerAdapter!!.onDone(msg)
postDelayed({
springBack(getScrollOffset(), animTimeLong)
}, 300)
Expand Down Expand Up @@ -346,8 +358,20 @@ class ElasticLayout @JvmOverloads constructor(
dampingTemp = damping / count
}

private var animator: ValueAnimator? = null
private var cancel = false
private var cancelAnimationTime = 100L

/**
* 取消所有事件,View直接弹回 调用adapter.onCancel()
* @param animTime 弹回的时间
*/
fun cancelLoading(animTime: Long){
if(cancel)return
cancel = true
cancelAnimationTime = animTime
}

private var animator: ValueAnimator? = null
//弹回动画
@Synchronized
private fun springBack(offset: Int, animTime: Long) {
Expand Down Expand Up @@ -473,11 +497,6 @@ class ElasticLayout @JvmOverloads constructor(
/**在这里生成一个view,这个view将会被放置到列表主体的尾部*/
abstract fun getContentView(viewGroup: ViewGroup): View

// /**设置回调,通过它通知ElasticView加载完成,执行完成动画*/
// fun setPullCallBack(callBack: PullCallBack) {
// this.callBack = callBack
// }

/**
* 滑动进度
* @param progress in 0 .. offset 当超过offset时依旧会继续调用知道松开手指
Expand All @@ -490,15 +509,27 @@ class ElasticLayout @JvmOverloads constructor(
/**释放加载*/
open fun releaseToDo() {}

/**释放手指时调用*/
open fun onRelease(){}

/**加载中*/
open fun onDo() {
isDoing = true
}

/**
* 与onDo对应 在还没加载之前调用了cancel
*/
open fun onCancel(){

}

/**加载完成,通知ElasticView播放完成动画*/
open fun overDo(msg: String) {
open fun onDone(msg: String) {
isDoing = false
}


}

abstract class HeaderAdapter(offset: Int) : BaseAdapter(offset) {}
Expand All @@ -508,7 +539,6 @@ class ElasticLayout @JvmOverloads constructor(
fun over()
}


interface OnEventListener {
//下拉刷新
fun onRefresh()
Expand All @@ -518,11 +548,20 @@ class ElasticLayout @JvmOverloads constructor(
}

interface OnScrollListener{

/**
* @param scrollX
* @param scrollY 当前滑动到的位置x ,y
*
* @param dx
* @param dy 还未滑动的偏移值
* @return true 拦截滑动事件
*/
fun preOnScrolled(scrollX:Int,scrollY:Int,dx: Int,dy: Int):Boolean
/**
* @param dx x变化值 移动的长度
* @param dy y变化值 移动的长度
*/
fun onScrolled(dx:Int,dy:Int)
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package sl.view.elasticviewlibrary.base

import android.content.Context
import android.opengl.Visibility
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -56,11 +54,11 @@ open class BaseFooter(private val context: Context, offset:Int):ElasticLayout.Fo
super.onDo()
}

override fun overDo(msg:String) {
override fun onDone(msg:String) {
text.text = msg
progressBar.visibility = View.INVISIBLE
icon.visibility = View.VISIBLE
direction = DIRECTION_UP
super.overDo(msg)
super.onDone(msg)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,26 @@ open class BaseHeader(private val context: Context, offset: Int) : ElasticLayout
super.releaseToDo()
}

override fun onRelease() {
super.onRelease()
}

override fun onCancel() {
super.onCancel()
}

override fun onDo() {
text.text = "正在更新"
progressBar.visibility = View.VISIBLE
icon.rotation = 0f
icon.visibility = View.INVISIBLE
super.onDo()
}
override fun overDo(msg:String) {
override fun onDone(msg:String) {
text.text = msg
progressBar.visibility = View.INVISIBLE
icon.visibility = View.VISIBLE
direction = DIRECTION_DOWN
super.overDo(msg)
super.onDone(msg)
}
}
2 changes: 1 addition & 1 deletion elasticviewlibrary/src/main/res/layout/base_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
android:id="@+id/img"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:padding="5dp"
android:scaleType="fitXY"
android:src="@drawable/ic_arrow_downward_gray_50dp"
android:contentDescription="loading" />
Expand Down

0 comments on commit 3fb33f2

Please sign in to comment.