问题
I have one ScrollView
with one LinearLayout
with TextView
s. I want to detect when ScrollView
is scrolling up or down to hide/show ActionBar
.
回答1:
you need to create a class that extends ScrollView
public class ExampleScrollView extends ScrollView
and then Override onScrollChanged
that gives you the old and new scroll positions and from there you can detect which direction
protected void onScrollChanged(int l, int t, int oldl, int oldt)
回答2:
check this out
scrollView.setOnTouchListener(new View.OnTouchListener() {
float y0 = 0;
float y1 = 0;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
y0 = motionEvent.getY();
if (y1 - y0 > 0) {
Log.i("Y", "+");
AnimateFloat(true);
} else if (y1 - y0 < 0) {
Log.d("Y", "-");
AnimateFloat(false);
}
y1 = motionEvent.getY();
}
return false;
}
});
回答3:
A sure short answer
scroll.setOnScrollChangeListener(new View.OnScrollChangeListener() {
@Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
int x = scrollY - oldScrollY;
if (x > 0) {
//scroll up
} else if (x < 0) {
//scroll down
} else {
}
}
});
来源:https://stackoverflow.com/questions/27297467/detect-if-a-scrollview-is-scrolling-up-or-down-android