Android:Web 视图库

发布于 2024-11-05 08:57:24 字数 1537 浏览 4 评论 0原文

我正在尝试使用 Gallery 小部件构建一组水平滚动的 webview。问题是我无法滑动视图,这当然适用于图像库。 按照食谱代码,在活动的 onCreate() I 中:

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

然后适配器创建 WebView 并返回它们以获取请求的索引:

   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;
        }
    }

问题似乎是 WebView 坚持使用触摸事件,尽管设置了其可点击事件> 属性关闭。我尝试为视图创建一个 OnTouchEventListener,然后将事件分派到图库,但这似乎会使程序崩溃。

任何线索表示赞赏。

I am trying to build a horizontally scrolling set of webviews, using the Gallery widget. The problem is that I cannot swipe the views, which of course works for galleries of images.
Following cookbook code, in the activity's onCreate() I:

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

Then the adapter creates the webviews and returns them for the requested indices:

   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 seems to be that the WebView insists on consuming touch events, despite setting its clickable attribute off. I tried creating an OnTouchEventListener for the views and then dispatching then events to the gallery, but that just seems to crash the program.

Any clues appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半步萧音过轻尘 2024-11-12 08:57:24

我对 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;
            }
        }

}

这让我可以垂直滚动 Web 内容,然后单击链接,而水平滚动则驱动画廊。

I had the same problem with a Gallery of WebViews, and ended up doing the following:

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;
            }
        }

}

This lets me scroll the web content vertically, and click on links, while horizontal scrolls drive the gallery.

丢了幸福的猪 2024-11-12 08:57:24

我建议您改为显示不同 WebView 的快照,并且仅将中心的真实 WebView 作为活动视图。我只能想象在加载大量网页的情况下同时显示多个 WebView 会消耗多少资源。

http://developer.android.com/reference/android /webkit/WebView.html#capturePicture%28%29

I would suggest you to display snapshots of the different WebViews instead, and only the real WebView in the center as the active one. I can only imagine the resources it would consume displaying several WebViews at the same time with heavy web pages loaded.

http://developer.android.com/reference/android/webkit/WebView.html#capturePicture%28%29

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文