Android: Web View Gallery

I am trying to create a horizontal scrollable set of web views using a gallery widget. The problem is that I cannot scroll through the views, which of course works for image galleries. Following the cookbook code, in onCreate () I activity:

g = (Gallery) findViewById(R.id.chapter_browser);
g.setAdapter(new WebViewAdapter(this));

The adapter then creates web views and returns them for the requested indexes:

   public class WebViewAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

        private String[] pages = {
                "test1.html",
                "test2.html",
                "test3.html",
                "test4.html"
        };

        public WebViewAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return pages.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            WebView i = new WebView(mContext);

            i.loadUrl("file:///android_asset/" + pages[position]);
            i.setBackgroundResource(mGalleryItemBackground);
            i.setWebViewClient(new WebViewClient());
            i.setLayoutParams(new Gallery.LayoutParams(100,100));
            i.setInitialScale(100);
            i.setFocusable(false);
            i.setClickable(false);

            return i;
        }
    }

The problem is that WebView insists on consuming touch events even though the attribute is clickabledisabled. I tried creating an OnTouchEventListener for the views and then sending the events to the gallery, but this is just a program crash.

Any hints appreciated.

+5
source share
2

Gallery WebViews, :

public class MyGallery extends Gallery
{

    private final int slop;
    private float initialX;
    private float initialY;

    public MyGallery(Context context, AttributeSet attrs)
        {
        super(context, attrs);

        slop = ViewConfiguration.get(context).getScaledTouchSlop();
        }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
        {
        return false;
        }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
        {
        switch (ev.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                /*
                 * Kludge: Both the gallery and the child need to see the
                 * gesture, until we know enough about it to decide who gets it.
                 */
                onTouchEvent(ev);

                initialX = ev.getX();
                initialY = ev.getY();

                return false;

            case MotionEvent.ACTION_MOVE:
                float distX = Math.abs(ev.getX() - initialX);
                float distY = Math.abs(ev.getY() - initialY);

                if (distY > distX && distY > slop)
                    /* Vertical scroll, child takes the gesture. */
                    return false;

                /*
                 * If a horizontal scroll, we take the gesture, otherwise keep
                 * peeking.
                 */
                return distX > slop;

            default:
                return false;
            }
        }

}

- , .

+6

All Articles