#android #android-recyclerview #scrollbar #searchbar
#Android #android-recyclerview #полоса прокрутки #панель поиска
Вопрос:
Я хочу установить видимость панели поиска видимой, если список прокручивается вниз, и видимость пропала, если список прокручивается вверх с помощью addOnScrollListener в Recyclerview.
применение условия dy> 0 и dy<0 вызывает мерцание. Пожалуйста, помогите мне.
Ответ №1:
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);
}
});
}
проверьте это, надеюсь, это сработает.
Ответ №2:
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()
}
}