Android:Webviews图库

时间:2011-05-04 03:16:12

标签: android webview gallery

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

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

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

   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坚持使用触摸事件,尽管设置了clickable属性。我尝试为视图创建一个OnTouchEventListener,然后将事件调度到库中,但这似乎使程序崩溃。

任何线索都表示赞赏。

2 个答案:

答案 0 :(得分:6)

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

}

这使我可以垂直滚动网页内容,点击链接,而水平滚动则可以驱动图库。

答案 1 :(得分:0)

我建议您显示不同WebView的快照,而只显示中心的真实WebView作为活动快照。我只能想象在加载大量网页的同时显示多个WebView的资源。

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

相关问题