当前位置:  开发笔记 > 前端 > 正文

Android:Webviews图库

如何解决《Android:Webviews图库》经验,为你挑选了1个好方法。

我正在尝试使用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,然后将事件调度到库中,但这似乎使程序崩溃.

任何线索都表示赞赏.



1> dimitris..:

我对WebViews的Gallery有同样的问题,最后做了以下事情:

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内容,并单击链接,而水平滚动驱动库.

推荐阅读
喜生-Da
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有