我有這個(noob?)的問題,但我現在無法弄清楚了好幾個小時。使用ViewPagerIndicator(viewpager)與多個webview的
我使用ViewPagerIndicator(基於ViewPager)。我需要有3頁,其中每個頁面都包含具有特定網址的網頁視圖。
我怎樣才能讓這個運行?我所有的嘗試都以靜態結束,當我有3頁加載了ONE和THE SAME網址時。我嘗試了很多方法 - 但我對代碼的理解似乎缺乏一些基礎知識。
我的代碼:
public class WebPagerProfileActivity extends Activity {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 3;
private Context context;
private ViewPagerAdapter awesomeAdapter;
LayoutInflater inflater ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.webpagerprofile);
context = this;
inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
awesomeAdapter = new ViewPagerAdapter(context);
awesomePager = (ViewPager) findViewById(R.id.viewpager);
TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicator);
awesomePager.setAdapter(awesomeAdapter);
indicator.setViewPager(awesomePager);}
和適配器:
在交換機public class ViewPagerAdapter extends PagerAdapter implements TitleProvider
{ private static String[] titles = new String[]
{
"Profile",
"Friends",
"Statistics"
};
private final Context context;
public ViewPagerAdapter(Context context)
{
this.context = context;
}
//@Override
public String getTitle(int position)
{
return titles[position];
}
@Override
public int getCount()
{
return titles.length;
}
@Override
public Object instantiateItem(View pager, int position)
{
View layout = null;
switch (position) {
case 0: {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.webprofile, null);
WebView mainContent = (WebView)layout.findViewById(R.id.webview);
mainContent.getSettings().setJavaScriptEnabled(true);
mainContent.getSettings().setJavaScriptEnabled(true);
mainContent.getSettings().setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebViewClient(new WebViewClient());
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("http://www.wp.pl");
}
case 1: { LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.webfriends, null);
WebView mainContent = (WebView)layout.findViewById(R.id.webviewfriends);
mainContent.getSettings().setJavaScriptEnabled(true);
mainContent.getSettings().setJavaScriptEnabled(true);
mainContent.getSettings().setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebViewClient(new WebViewClient());
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("http://www.onet.pl");
}
case 2: { LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.webstatistics, null);
WebView mainContent = (WebView)layout.findViewById(R.id.webviewstatistics);
mainContent.getSettings().setJavaScriptEnabled(true);
mainContent.getSettings().setJavaScriptEnabled(true);
mainContent.getSettings().setBuiltInZoomControls(true);
mainContent.requestFocusFromTouch();
mainContent.setWebViewClient(new WebViewClient());
mainContent.setWebChromeClient(new WebChromeClient());
mainContent.loadUrl("http://www.google.pl");
}
}
((ViewPager) pager).addView((View)layout,0);
return layout;
}
@Override
public void destroyItem(View pager, int position, Object view)
{
//((ViewPager)pager).removeView((WebView)view);
}
@Override
public boolean isViewFromObject(View view, Object object)
{
return view.equals(object);
}
@Override
public void finishUpdate(View view) {}
@Override
public void restoreState(Parcelable p, ClassLoader c) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View view) {}
}
謝謝,謝謝,謝謝。我討厭這樣的錯誤,因此我失去了幾小時,呃! – 2012-02-16 14:26:49
太棒了。你能接受我們的答案嗎? – 2012-02-16 14:29:22
當然,我只是不知道它是如何工作的;) – 2012-02-16 14:49:15