我正在尝试创建webviews的水平“列表视图”。正如之前对问题的回答所建议的那样,我正在使用兼容包中的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) {}
}
答案 0 :(得分:6)
这是有效的...我不得不削减一些webview的东西,因为我得到了错误。
您遇到的主要问题是使用“集合”视图而不是创建新视图。
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) {}
}
}
希望这会有所帮助。 M