欢迎你们 我有滚动视图,如果它向上或向下我想要滚动方向
因为我想在滚动到底部时隐藏操作栏标签(如Twitter应用程序)
试过这个但没有用
wrap_layout.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
y = event.getY();
break;
case MotionEvent.ACTION_UP:
Log.d("scroll", event.getY() - y + "");
break;
default:
break;
}
return false;
}
});
答案 0 :(得分:1)
最简单的方法是创建一个像这样的自定义ScrollView
public class ObservableScrollView extends ScrollView{
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}
然后ovverride onScrollChanged()方法
@Override
protected void onScrollChanged(int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
super.onScrollChanged(scrollX, scrollY, oldScrollX, oldScrollY);
if(scrollY<oldScrollY){
//vertical scrolling down
}else{
//vertical scrolling upp
}
if(scrollX<oldScrollX){
//horizontal scrolling down
}else{
//horizontal scrolling upp
}
}
最后你可以创建一个自定义界面来传递这些发明
答案 1 :(得分:0)
创建自定义scrollview;
public class CustomScrollView extends ScrollView {
private float mTouchPosition;
private float mReleasePosition;
public interface OnScrollViewListener {
void onScrollChanged(CustomScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY);
void scrollUp();
void scrollDown();
}
private OnScrollViewListener mOnScrollViewListener;
public CustomScrollView(Context context) {
this(context, null, 0);
}
public CustomScrollView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
mOnScrollViewListener.onScrollChanged( this, l, t, oldl, oldt );
super.onScrollChanged(l, t, oldl, oldt);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mTouchPosition = event.getY();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
mReleasePosition = event.getY();
if (mTouchPosition - mReleasePosition > 0) {
mOnScrollViewListener.scrollDown();
} else {
mOnScrollViewListener.scrollUp();
}
}
return super.onTouchEvent(event);
}
public void setOnScrollViewListener(OnScrollViewListener l) {
this.mOnScrollViewListener = l;
}
}
并添加监听器;
yourCustomScrollView.setOnScrollViewListener(new CustomScrollView.OnScrollViewListener() {
@Override
public void onScrollChanged(CustomScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
}
@Override
public void scrollUp() {
Timber.e("SCROLL UP");
}
@Override
public void scrollDown() {
Timber.e("SCROLL DOWN");
}
});
答案 2 :(得分:0)
您只需将ScrollListener设置为您的scrollView并按以下方式进行处理即可。
nsv.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
if (!nsv.canScrollVertically(1))
{
// bottom of scroll view
}
//reached at top of scrollView
if (!nsv.canScrollVertically(-1)) {
// top of scroll view
}
//scrolling up and down
if(scrollY<oldScrollY){
//vertical scrolling down
}else{
//vertical scrolling upp
}
if(scrollX<oldScrollX){
//horizontal scrolling down
}else{
//horizontal scrolling upp
}});