Android:Web 视图库
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 WebViews 画廊也遇到了同样的问题,最终执行了以下操作:
这让我可以垂直滚动 Web 内容,然后单击链接,而水平滚动则驱动画廊。
I had the same problem with a Gallery of WebViews, and ended up doing the following:
This lets me scroll the web content vertically, and click on links, while horizontal scrolls drive the gallery.
我建议您改为显示不同
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
WebView
s instead, and only the realWebView
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