我正在尝试创建一个水平的“listview”网页视图。如之前回答过的问题所建议的,我使用了兼容包中的ViewPager库。
但是当我添加我的webview到视图集合中时,使用
((ViewPager) collection).addView((View)mainContent,0);
我遇到了转换问题。
09-07 20:45:30.690: ERROR/AndroidRuntime(8166): java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.support.v4.view.ViewPager 这会导致应用程序崩溃。
现在几乎可以理解,将不同的数据类型进行强制转换会导致错误,但在一个ViewPager示例应用程序中,他们将TextView强制转换为ViewPager,它也是一个视图...我只是不知道其他存储视图并在应用程序中显示它们的方式!
但是当我添加我的webview到视图集合中时,使用
((ViewPager) collection).addView((View)mainContent,0);
我遇到了转换问题。
09-07 20:45:30.690: ERROR/AndroidRuntime(8166): java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.support.v4.view.ViewPager 这会导致应用程序崩溃。
现在几乎可以理解,将不同的数据类型进行强制转换会导致错误,但在一个ViewPager示例应用程序中,他们将TextView强制转换为ViewPager,它也是一个视图...我只是不知道其他存储视图并在应用程序中显示它们的方式!
private class AwesomePagerAdapter extends PagerAdapter{
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
ViewPager viewPager = (ViewPager) collection;
collection = layoutInflater.inflate(R.layout.maincontentview, null);
WebView mainContent = (WebView)collection.findViewById(R.id.mainContent);
JSInterface jsInterface = new JSInterface(mainContent);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
//jsInterface.fit(width, height);
//jsInterface.flip(width, height);
WebSettings webSettings = mainContent.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginsEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setSupportZoom(false);
mainContent.setVerticalScrollBarEnabled(false);
mainContent.setHorizontalScrollBarEnabled(false);
/*mainContent.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return(event.getAction() == MotionEvent.ACTION_MOVE);
}
});*/
mainContent.setWebViewClient(new WebViewClient()
{
@Override
public void onPageFinished(WebView view, String url)
{
/*mainContent.loadUrl("javascript:var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.src = 'jquery.min.js';" +
"document.getElementsByTagName('head')[0].appendChild(script);");*/
WebView mainContent = (WebView)view.findViewById(R.id.mainContent);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
mainContent.loadUrl("javascript:fit("+width+","+height+");");
mainContent.loadUrl("javascript:flip("+width+","+height+");");
}
});
mainContent.setWebChromeClient(new WebChromeClient()
{
@Override
public boolean onJsAlert(WebView view, String url, String message, JsResult result)
{
Log.e("alert triggered", message);
return false;
}
});
//use load file from string, article[i].html
mainContent.loadUrl("file:///android_asset/cache_manifest_test.html");
((ViewPager) collection).addView((View)mainContent,0);
return mainContent;
}
/**
* Remove a page for the given position. The adapter is responsible
* for removing the view from its container, although it only must ensure
* this is done by the time it returns from {@link #finishUpdate()}.
*
* @param container The containing View from which the page will be removed.
* @param position The page position to be removed.
* @param object The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((WebView) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((WebView)object);
}
/**
* Called when the a change in the shown pages has been completed. At this
* point you must ensure that all of the pages have actually been added or
* removed from the container as appropriate.
* @param container The containing View which is displaying this adapter's
* page views.
*/
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}