2
votes

I'm working right now in order to disable horizontal scrolling of horizontalscrollview (id: ordermenu) which has scrollview(i mean vertical scrollview. id of scrollview's child relativelayout: category01layout, category02layout, ...) as children.

Here is what it looks like in XML:

HorizontalScrollView(ordermenu)
- LinearLayout
  - ScrollView
    - RelativeLayout(category01layout)
  - ScrollView
    - RelativeLayout(category02layout)
...
  - ScrollView
    - RelativeLayout(category08layout)

The screen shows one category layout at a time, and when I press prev/next button, it sets horizontalscrollview.smoothScrollTo() to change the category layout.

So when I run the app and scroll, my finger is scrolling both HorizontalScrollView and ScrollView. But I don't want my finger to make both these two listen the onScrollChanged. I only want ScrollView to listen to my finger to 'only scroll vertically'. Since I don't know the way to do this, ScrollView scrolls vertically in a smooth way only if I scroll in an exact vertical direction. Otherwise my finger scrolls HorizontalScrollView also, what I didn't expect.

So is there any way to disable horizontal scrolling and only enable vertical scrolling?

I previously used this method (create a class which extends HorizontalScrollView)

@Override
protected void onScrollChanged(int x, int y, intoldx, intoldy){
if(Math.abs(x - oldx) <= 50){
mScrollable = false;
}
else {
mScrollable = false;

}
return;
}

But this just caused a lot of burden for the UI, making vertically scrolling ScrollView too stiff and slow. So please give me another way than this.

Thanks in advance.

4

4 Answers

0
votes

I have the following method in my subclass of HorizontalScrollView that allows me to scroll both vertically and horizontally. Basically if it detects vertical scrolling it gives up the touches. Play with the min distance depending on how responsive it feels to you.

 private float SWIPE_VERT_MIN_DISTANCE = 100;
 private float startlocation = 0;
    private float diffY = 0;
    private boolean vertScroll = false;

    public boolean onInterceptTouchEvent (MotionEvent ev){
        boolean ret = super.onInterceptTouchEvent(ev);



        switch(ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                startlocation = ev.getY();
                vertScroll = false;
                break;
            case MotionEvent.ACTION_MOVE:
                diffY = Math.abs(startlocation - ev.getY());
                if(diffY > SWIPE_VERT_MIN_DISTANCE)
                    vertScroll = true;
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        if(ret){
            if(vertScroll)
                return false;
            else
                return true;
        }

        return ret;
    }
0
votes

I think what you really want here is a ViewPager. Incidentally, Google's Android devs mention the following rules often:

  1. You do not put scroll views inside other scroll views.
  2. You do not put list views inside scroll views.
0
votes

Apart from your this problem i suggest you to take a View Flipper.

Then take the layouts(those layouts which will be changed after clicking the next/prev button) in it.

Now when the next button will be clicked do viewFlipper.showNext()/viewFlipper.showPrevious() with animation(like right in/left out).

Thus you can show a smooth scrall over layouts. Try it. I do it in this way. It should work.

0
votes

Try to use view switcher,In which there is no need to scroll views inside other scroll views and list views inside scroll views.

I wish you solve your problem very soon.