Android ViewPager + Webview, ClassCastException! Necesita una vista web en un viewpager

Estoy tratando de crear un "listview" horizontal de webviews. Como sugirió una respuesta anterior a una pregunta, estoy usando la biblioteca ViewPager del paquete de compatibilidad.

Desafortunadamente, cuando voy a añadir mi webview a la colección de vistas,

((ViewPager) collection).addView((View)mainContent,0);

Tengo el problema de casting.

09-07 20:45:30.690: ERROR/AndroidRuntime(8166): java.lang.ClassCastException: android.webkit.WebView cannot be cast to android.support.v4.view.ViewPager

Esto bloquea la aplicación.

Ahora casi tiene sentido, el lanzamiento de diferentes tipos de datos hace que las cosas salgan mal, pero en una aplicación de ejemplo de ViewPager que tenía, están lanzando TextView a ViewPager que es una vista …

Simplemente no sé qué otra forma de "almacenar" mis vistas y mostrarlas en la aplicación. yo tengo

 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) {} } 

esto funciona … He tenido que cortar algunas de las cosas webview abajo como yo estaba recibiendo errores con eso.

El principal problema que tuvo fue usar la vista de "colección" en lugar de crear una nueva.

 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) {} } } 

Espero que esto ayude. METRO

  • Android WebView eliminarCookie
  • Cómo autenticar servicio web sap con ksoap2
  • Vista web de Android: lee las cookies
  • Imágenes borrosas en el navegador Android de stock
  • Android webview tiene borde blanco alrededor de él y cómo puedo deshacerme de él?
  • WebView no se dibuja, WARN / webcore (5336): No se puede obtener la viewWidth después del primer diseño
  • Obtener el precio de un sitio web minorista
  • Android WebView: bloqueo tras la carga de url
  • Problema con el zoom de texto de WebView en Android
  • Configurar una base de datos de servidor en línea y codificar mi aplicación nativa de Android para interactuar con ella
  • ¿Cómo puedo inhabilitar todos los eventos táctiles en todos los niños de un ViewGroup?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.