我想在Recyclerview中的addOnScrollListener的帮助下,如果列表向下滚动,则将搜索栏的可见性设置为可见,如果列表向上滚动,则设置为不可见。
应用条件dy>0和dy<0的闪烁。请帮帮我。
recyclerOverView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy < 0)
exFab.shrink();
else if (dy > 0)
exFab.extend();
}
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
});
}
检查一下,希望它能起作用。
class MainActivity : AppCompatActivity() {
private val TAG = "MainActivity"
private val SEARCH_BAR_HEIGHT = dpToPx(70) //replace it with view height
private val scrollListener = object : RecyclerView.OnScrollListener() {
var isSearchVisible = false
var isAnimating = false
var lastPosition = -1
var currentPosition = 0
var isScrollingUp = false
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
currentPosition =
(recyclerView.layoutManager as LinearLayoutManager).findFirstCompletelyVisibleItemPosition()
Log.d(TAG, "current position = $currentPosition last Position = $lastPosition")
if (currentPosition < lastPosition) {
//scroll up
if (!isScrollingUp || isSearchVisible || isAnimating) {
isScrollingUp = true
lastPosition = currentPosition
return
}
isAnimating = true
isScrollingUp = true
animate(searchView, 0, SEARCH_BAR_HEIGHT, object : AnimationListener {
override fun onAnimationCompleted() {
isAnimating = false
isSearchVisible = true
}
override fun onAnimationCanceled() {
isAnimating = false
}
})
} else if (currentPosition > lastPosition) {
//scroll down
if (isScrollingUp || !isSearchVisible || isAnimating) {
isScrollingUp = false
lastPosition = currentPosition
return
}
isAnimating = true
isScrollingUp = false
animate(searchView, SEARCH_BAR_HEIGHT, 0, object : AnimationListener {
override fun onAnimationCompleted() {
isAnimating = false
isSearchVisible = false
searchView.visibility = View.GONE
}
override fun onAnimationCanceled() {
isAnimating = false
}
})
}
lastPosition = currentPosition
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
recyclerView.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = ItemAdapter()
addOnScrollListener(scrollListener)
}
}
fun animate(view: View, fromHeight: Int, toHeight: Int, callback: AnimationListener?) {
view.clearAnimation()
view.visibility = View.VISIBLE
val valueAnimator: ValueAnimator = ValueAnimator.ofInt(fromHeight, toHeight)
valueAnimator.addUpdateListener { animation ->
val layoutParam = view.layoutParams
layoutParam.height = animation.animatedValue as Int
view.layoutParams = layoutParam
}
valueAnimator.addListener(object : Animator.AnimatorListener {
override fun onAnimationRepeat(p0: Animator?) {
}
override fun onAnimationEnd(p0: Animator?) {
callback?.onAnimationCompleted()
}
override fun onAnimationCancel(p0: Animator?) {
callback?.onAnimationCanceled()
}
override fun onAnimationStart(p0: Animator?) {
}
})
valueAnimator.interpolator = DecelerateInterpolator()
valueAnimator.duration = 200
valueAnimator.start()
}
interface AnimationListener {
fun onAnimationCompleted()
fun onAnimationCanceled()
}
}