Search code examples
androidviewandroid-scrollview

Hide view when scrollViews scrolls down


i have linearLayout and scrollView in my activity. I want to make next: when i scroll down - hide linearLayout and when i scroll up - show it. I have read This article and I try to make lie that, but onScrollChanged called all time when i put my finger on screen, and it is bad solution as i think.

Then i try to make next:

public class CustomScrollView extends ScrollView {

Context context;
public void setContext(Context c) {
    this.context = c;
}

public CustomScrollView(Context context) {
    super(context);
}

public CustomScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()){

        case MotionEvent.ACTION_DOWN:
            Log.d("myLogs", "Down ");
            hidePanel();
            break;
        case MotionEvent.ACTION_UP:
            Log.d("myLogs", "Up ");
            showPanel();
            break;

    }
    return super.onTouchEvent(ev);
}
}

But this woes not worked correct, when i move scroll up - i get Up Up Down ect.

I will be glad for any ideas how to make this

Edited: I try:

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_MOVE:
            final int threashold = 70;
            if (mLastY - ev.getY() > threashold) {
                Log.v("teste", "moving up");
                mLastY = ev.getY();
            } else if (mLastY - ev.getY() < -threashold) {
                Log.v("teste", "moving down");
                mLastY = ev.getY();
            }
    }
    return super.onTouchEvent(ev);
}

When i scroll up i got:

09-24 20:22:04.568      497-497/khuta.freeturn V/teste﹕ moving down
09-24 20:22:04.668      497-497/khuta.freeturn V/teste﹕ moving up
09-24 20:22:04.688      497-497/khuta.freeturn V/teste﹕ moving up

When i scroll down i got:

09-24 20:24:44.559      497-497/khuta.freeturn V/teste﹕ moving up
09-24 20:24:44.719      497-497/khuta.freeturn V/teste﹕ moving down
09-24 20:24:44.809      497-497/khuta.freeturn V/teste﹕ moving down

Solution

  • Use the if's do do whatever action you want.

    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mLastY = event.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        final int threashold = 50;
        if(mLastY-motionEvent.getY()>threashold)
        {
            Log.v("teste","moving up");
            mLastY = motionEvent.getY();
        }else if(mLastY-motionEvent.getY()<-threashold){
            Log.v("teste","moving down");
            mLastY = motionEvent.getY();
        }
    }