I have a nested scrollview
and a button
. I want to hide the button whenever the scrollview
is scrolling and show the button
when scrolling stops regardless of up scroll and down scroll. Someone please help.
Asked
Active
Viewed 1,894 times
-1
![](../../users/profiles/7773009.webp)
Fathima km
- 1,883
- 3
- 12
- 21
-
are you using recyclerview inside nestedscrollview...? – OmiK Aug 27 '19 at 10:14
-
yes. I am using – Fathima km Aug 27 '19 at 10:15
-
then try updated code https://stackoverflow.com/a/57672283/6834114 – OmiK Aug 27 '19 at 10:19
5 Answers
2
Finally I have found the solution.
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
}
})
![](../../users/profiles/7773009.webp)
Fathima km
- 1,883
- 3
- 12
- 21
-
Your answer is right but it will not provide proper effect like hiding and showing fab when scroll view is scrolling – Jay Thummar Nov 20 '19 at 04:47
0
You can use this listener
mScrollView.getViewTreeObserver().addOnScrollChangedListener(
new OnScrollChangedListener() {
@Override
public void onScrollChanged() {
int scrollY = rootScrollView.getScrollY(); // If you are dealing with ScrollView
int scrollX = rootScrollView.getScrollX(); // If you are dealing with HorizontalScrollView
// DO SOMETHING
// E.g : myBtn.setVisibility( View.GONE );
}
});
![](../../users/profiles/5090100.webp)
Kévin Giacomino
- 478
- 4
- 11
-
I wanted to hide the button when scroll starts and show the button when scrolling stops. – Fathima km Aug 27 '19 at 10:07
0
try to use NestedScrollview listner like below
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
}
}
});
here
scrollX = new horizontal scroll position
oldScrollX = previous horizontal scroll position
scrollY = new vertical scroll position
oldScrollY = previous vertical scroll position
UPDATE
If you are using recycler and want to monitor scroll state then try below this may help
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);
}
});
![](../../users/profiles/6834114.webp)
OmiK
- 2,606
- 1
- 19
- 35
-
I wanted to hide the button when scroll starts and show the button when scrolling stops. – Fathima km Aug 27 '19 at 10:08
0
check in this way
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")
}
}
})
![](../../users/profiles/5755569.webp)
satyan_android
- 346
- 3
- 14
-
I wanted to hide the button when scroll starts and show the button when scrolling stops. – Fathima km Aug 27 '19 at 10:08
-
in those conditions pls Use button.setVisibility(GONE); or button.setVisibility(VISIBLE); – satyan_android Aug 27 '19 at 10:21
0
This should solve your request – hide the button when scrolling and show it when the scroll stop.
RecyclerView recyclerView = v.findViewById(R.id.idOfRecyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
// To hide the add button on scroll
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
if (dy > 0 || dy < 0 && addExpenseButton.isShown()) {
button.setVisibility(View.GONE);
}
super.onScrolled(recyclerView, dx, dy);
}
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_IDLE) {
button.setVisibility(View.VISIBLE);
}
super.onScrollStateChanged(recyclerView, newState);
}
});
Here a part of the layout:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipeRefresh"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/headerLayout">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/idOfRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:listSelector="@color/transparent" />
![](../../users/profiles/7861604.webp)
Simone Solfato
- 51
- 3