Skip to content

Commit

Permalink
ElasticLayout增加OnScrollListener
Browse files Browse the repository at this point in the history
  • Loading branch information
lang-v committed Oct 18, 2020
1 parent ba893c0 commit 8f019ee
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,33 +89,9 @@ class ElasticLayout @JvmOverloads constructor(
field = value
}

// //弹回动画锁
// private val lock = ReentrantLock()

//事件监听
private var listener: OnEventListener? = null

// //加载完成的回调
// private val refreshCallBack = object : PullCallBack {
// override fun over() {
//// isRefreshing = false
// postDelayed({
// isRefreshing = false
//// springBack(getScrollOffset(), 300)
// }, 300)
// }
// }
// private val loadCallBack = object : PullCallBack {
// override fun over() {
// postDelayed({
// isLoading = false
// },300)
//// isLoading = false
//// postDelayed({
//// springBack(getScrollOffset(), 300)
//// }, 300)
// }
// }
private var scrollListener:OnScrollListener?=null

init {
//禁止裁剪布局,使得在页面外的view依然能显示
Expand Down Expand Up @@ -239,6 +215,7 @@ class ElasticLayout @JvmOverloads constructor(
super.scrollBy(x, 0)
}
if (isLoadingOrRefreshing()) return
scrollListener?.onScrolled(x,y)
val scrollOffset = getScrollOffset()
//更新控件header,footer状态
if (scrollOffset < 0) {
Expand Down Expand Up @@ -394,10 +371,14 @@ class ElasticLayout @JvmOverloads constructor(
animator!!.duration = animTime
val scrollOffset = getScrollOffset()
animator!!.addUpdateListener { animation ->
if (orientation == VERTICAL)
if (orientation == VERTICAL) {
scrollListener?.onScrolled(0,scrollOffset+(animation.animatedValue as Int)-getScrollOffset())
scrollTo(scrollX, scrollOffset + animation.animatedValue as Int)
else
}
else {
scrollListener?.onScrolled(scrollOffset+(animation.animatedValue as Int)-getScrollOffset(),0)
scrollTo(scrollOffset + animation.animatedValue as Int, scrollY)
}
}
animator!!.addListener(object : Animator.AnimatorListener {
override fun onAnimationRepeat(animation: Animator?) {}
Expand Down Expand Up @@ -479,6 +460,10 @@ class ElasticLayout @JvmOverloads constructor(
this.listener = listener
}

fun setOnScrollListener(listener:OnScrollListener){
scrollListener = listener
}

/**
* 适配器基类 Header Footer都是派生于它
*/
Expand Down Expand Up @@ -532,4 +517,12 @@ class ElasticLayout @JvmOverloads constructor(
fun onLoad()
}

interface OnScrollListener{
/**
* @param dx x变化值 移动的长度
* @param dy y变化值 移动的长度
*/
fun onScrolled(dx:Int,dy:Int)
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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 All @@ -11,10 +13,10 @@ import sl.view.elasticviewlibrary.ElasticLayout
import sl.view.elasticviewlibrary.R

open class BaseFooter(private val context: Context, offset:Int):ElasticLayout.FooterAdapter(offset) {
private lateinit var view: View
private val icon by lazy { view.findViewById<ImageView>(R.id.img) }
private val progressBar by lazy { view.findViewById<ProgressBar>(R.id.progressBar) }
private val text by lazy { view.findViewById<TextView>(R.id.text) }
protected lateinit var view: View
protected val icon by lazy { view.findViewById<ImageView>(R.id.img) }
protected val progressBar by lazy { view.findViewById<ProgressBar>(R.id.progressBar) }
protected val text by lazy { view.findViewById<TextView>(R.id.text) }

//icon的方向
private val DIRECTION_DOWN = true
Expand Down Expand Up @@ -53,6 +55,7 @@ open class BaseFooter(private val context: Context, offset:Int):ElasticLayout.Fo
icon.visibility = View.INVISIBLE
super.onDo()
}

override fun overDo(msg:String) {
text.text = msg
progressBar.visibility = View.INVISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import sl.view.elasticviewlibrary.ElasticLayout
import sl.view.elasticviewlibrary.R

open class BaseHeader(private val context: Context, offset: Int) : ElasticLayout.HeaderAdapter(offset) {
private lateinit var view: View
private val icon by lazy { view.findViewById<ImageView>(R.id.img) }
private val progressBar by lazy { view.findViewById<ProgressBar>(R.id.progressBar) }
private val text by lazy { view.findViewById<TextView>(R.id.text) }
protected lateinit var view: View
protected val icon by lazy { view.findViewById<ImageView>(R.id.img) }
protected val progressBar by lazy { view.findViewById<ProgressBar>(R.id.progressBar) }
protected val text by lazy { view.findViewById<TextView>(R.id.text) }

//icon的方向
private val DIRECTION_DOWN = true
Expand Down

0 comments on commit 8f019ee

Please sign in to comment.