我正在尝试使用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,然后将事件调度到库中,但这似乎使程序崩溃.
任何线索都表示赞赏.
我对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内容,并单击链接,而水平滚动驱动库.