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.
Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.
Is Android System WebView spyware? Many curious people think that it is a spy app, which it is not. It is a browser software integrated into an operating system and allows programs to access websites. You can access any kind of material by using the assistance of WebView.
The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.
This interface was deprecated in API level 12. This interface is now obsolete.
I had the same problem with a Gallery of WebViews, and ended up doing the following:
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;
}
}
}
This lets me scroll the web content vertically, and click on links, while horizontal scrolls drive the gallery.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With