Android ViewPager + Webview, ClassCastException! Need webview in a viewpager
I am trying to create a horizontal "listview" of webviews. As suggested by a previous answer to a question, I am using the ViewPager library from the compatibility pack.
Unfortunately when I go to add my webview to the view collection, using
((ViewPager) collection).addView((View)mainContent,0);
I get the casting issue.
09-07 20:45:30.690: ERROR/AndroidRuntime(8166): java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.support.v4.view.ViewPager
This crashes the app.
Now it almost makes sense, casting different data types makes things go wrong, but in a ViewPager example app I had, they are casting TextView to the ViewPager which is a view...
I just don't know what other way to actually "store" my views and display them in the app! I have
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) {}
}
this works... I've had to cut some of the webview stuff down as i was getting errors with that.
The main problem you had was using the 'collection' view rather than creating a new one.
package com.msi.awesomepager;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
public class AwesomePagerActivity extends Activity {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 20;
private Context context;
private AwesomePagerAdapter awesomeAdapter;
LayoutInflater inflater ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = this;
inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager) findViewById(R.id.awesomepager);
awesomePager.setAdapter(awesomeAdapter);
}
private class AwesomePagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public Object instantiateItem(View collection, int position) {
View layout;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.maincontentview, null);
WebView mainContent = (WebView)layout.findViewById(R.id.mainContent);
mainContent.getSettings().setJavaScriptEnabled(true);
WebSettings webSettings = mainContent.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebViewClient(new WebViewClient());
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("file:///android_asset/test.html");
((ViewPager) collection).addView((View)layout,0);
return layout;
}
/**
* 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((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)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) {}
}
}
Hope this helps. M
精彩评论