我有一个nested scrollview
和一个button
。我想在scrollview
滚动时隐藏按钮,并在滚动停止时显示button
,而与上滚动和下滚动无关。有人帮忙。
答案 0 :(得分:2)
最后我找到了解决方法。
scrollView.setOnTouchListener(object : View.OnTouchListener {
override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
when (p1!!.getAction()) {
MotionEvent.ACTION_SCROLL, MotionEvent.ACTION_MOVE -> {
closeCaseButton.visibility = View.INVISIBLE
}
MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
closeCaseButton.visibility = View.VISIBLE
}
}
return false
}
})
答案 1 :(得分:0)
您可以使用此侦听器
getResourceAsStream()
答案 2 :(得分:0)
尝试使用如下所示的NestedScrollview列表器
nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if(scrollY>oldScrollY){
//your visibility code here
}
}
});
此处
scrollX =新的水平滚动位置
oldScrollX =上一个水平滚动位置
scrollY =新的垂直滚动位置
oldScrollY =先前的垂直滚动位置
更新
如果您正在使用回收站并希望监视滚动状态,请尝试以下操作,这可能有帮助
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int scrollState) {
super.onScrollStateChanged(recyclerView, scrollState);
switch(scrollState) {
case 2: // SCROLL_STATE_FLING
//hide button here
break;
case 1: // SCROLL_STATE_TOUCH_SCROLL
//hide button here
break;
case 0: // SCROLL_STATE_IDLE
//show button here
break;
default:
//show button here
break;
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
}
});
答案 3 :(得分:0)
以这种方式检查
nested_scroll.setOnScrollChangeListener(object : NestedScrollView.OnScrollChangeListener() {
fun onScrollChange(v: NestedScrollView, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
if (scrollY > oldScrollY) {
Log.e("#####", "Scroll Down")
} else if (scrollY < oldScrollY) {
Log.e("#####", "Scroll Up")
} else if (scrollY == 0) {
Log.e("#####", "Top Scroll")
} else if (scrollY == v.getMeasuredHeight() - v.getChildAt(0).getMeasuredHeight()) {
Log.e("#####", "Bottom Scroll")
}
}
})