Skip to content Skip to sidebar Skip to footer

Two Directional Scroll View

I would like to have a linearlayout with a header section on top and a webview below. The header will be short and the webview may be longer and wider than the screen. What is the

Solution 1:

Is a ScrollView nested inside a HorizontalScrollView a good idea?

Yes, and no.

Yes, my understanding is that ScrollView and HorizontalScrollView can be nested.

No, AFAIK, neither ScrollView nor HorizontalScrollView work with WebView.

I suggest that you have your WebView fit on the screen.

Solution 2:

there is another way. moddified HorizontalScrollView as a wrapper for ScrollView. normal HorizontalScrollView when catch touch events don't forward them to ScrollView and you only can scroll one way at time. here is solution:

package your.package;

import android.widget.HorizontalScrollView;
import android.widget.ScrollView;
import android.view.MotionEvent;
import android.content.Context;
import android.util.AttributeSet;

publicclassWScrollViewextendsHorizontalScrollView
{
    publicScrollView sv;
    publicWScrollView(Context context)
    {
        super(context);
    }

    publicWScrollView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

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

    @OverridepublicbooleanonTouchEvent(MotionEvent event)
    {
        boolean ret = super.onTouchEvent(event);
        ret = ret | sv.onTouchEvent(event);
        return ret;
  }

    @OverridepublicbooleanonInterceptTouchEvent(MotionEvent event)
    {
        boolean ret = super.onInterceptTouchEvent(event);
        ret = ret | sv.onInterceptTouchEvent(event);
        return ret;
    }
}

using:

@OverridepublicvoidonCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

/*BIDIRECTIONAL SCROLLVIEW*/ScrollViewsv=newScrollView(this);
        WScrollViewhsv=newWScrollView(this);
        hsv.sv = sv;
/*END OF BIDIRECTIONAL SCROLLVIEW*/RelativeLayoutrl=newRelativeLayout(this);
        rl.setBackgroundColor(0xFF0000FF);
        sv.addView(rl, newLayoutParams(500, 500));
        hsv.addView(sv, newLayoutParams(WRAP_CONTENT, MATCH_PARENT /*or FILL_PARENT if API < 8*/));
        setContentView(hsv);
    }

Solution 3:

Two years further down the line I think the open source community might have to your rescue: 2D Scroll View.

Edit: The Link doesn't work anymore but here is a link to an old version of the blogpost;

Solution 4:

I searched really long to make this work and finally found this thread here. wasikuss' answer came quite close to the solution, but still it did not work properly. Here is how it works very well (at least for me (Android 2.3.7)). I hope, it works on any other Android version as well.

Create a class called VScrollView:

package your.package.name;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.ScrollView;


publicclassVScrollViewextendsScrollView {
    publicHorizontalScrollView sv;

    publicVScrollView(Context context) {
        super(context);
    }

    publicVScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

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

    @OverridepublicbooleanonTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        sv.dispatchTouchEvent(event);
        returntrue;
    }

    @OverridepublicbooleanonInterceptTouchEvent(MotionEvent event) {
        super.onInterceptTouchEvent(event);
        sv.onInterceptTouchEvent(event);
        returntrue;
    }
}

Your layout should look like:

<your.package.name.VScrollViewandroid:id="@+id/scrollVertical"android:layout_width="fill_parent"android:layout_height="fill_parent" ><HorizontalScrollViewandroid:id="@+id/scrollHorizontal"android:layout_width="fill_parent"android:layout_height="wrap_content" ><TableLayoutandroid:id="@+id/table"android:layout_width="wrap_content"android:layout_height="wrap_content"android:clickable="false"android:stretchColumns="*" ></TableLayout></HorizontalScrollView></your.package.name.VScrollView>

In your activity, you should do something like:

hScroll = (HorizontalScrollView) findViewById(R.id.scrollHorizontal);vScroll = (VScrollView) findViewById(R.id.scrollVertical);vScroll.sv = hScroll;

... and that's how it works. At least for me.

Solution 5:

There is an easy workaround: In you activity get a reference to the outer scrollView (I'm going to assume a vertical scrollview) and a reference to the first child of that scroll view.

ScrollviewscrollY= (ScrollView)findViewById(R.id.scrollY);
LinearLayoutscrollYChild= (LinearLayout)findViewById(R.id.scrollYChild);

@OverridepublicbooleandispatchTouchEvent(MotionEvent event) {
    scrollYChild.dispatchTouchEvent(event);
    scrollY.onTouchEvent(event);
    returntrue;
}

One could argue that this solution is a bit hacky. But it has worked great for me in several applications!

Post a Comment for "Two Directional Scroll View"