gpt4 book ai didi

Android:Webview 画廊

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:34:16 26 4
gpt4 key购买 nike

我正在尝试使用 Gallery 小部件构建一组水平滚动的 Web View 。问题是我无法滑动 View ,这当然适用于图像库。按照食谱代码,在 Activity 的 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 属性设置为关闭。我尝试为 View 创建一个 OnTouchEventListener,然后将事件分派(dispatch)到画廊,但这似乎会使程序崩溃。

感谢任何线索。

最佳答案

我在使用 WebView 库时遇到了同样的问题,并最终执行了以下操作:

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

}

这让我可以垂直滚动网页内容并点击链接,而水平滚动则驱动画廊。

关于Android:Webview 画廊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5878094/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com