我在添加操作栏项来控制我的 Viewpager 屏幕时遇到了麻烦,我希望能够根据拉起的页面来控制 viewpager。我真的只有 3 个 web 视图,我想添加操作栏项以通过操作栏(刷新、后退、前进)控制 web 视图。提前感谢您提供的任何帮助。谢谢
代码
public class AwesomePagerActivity extends SherlockActivity {
private ViewPager awesomePager;
private static int NUM_AWESOME_VIEWS = 3;
private Context context;
private AwesomePagerAdapter awesomeAdapter;
LayoutInflater inflater ;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the action bar.
getSupportActionBar().show();
context = this;
inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager) findViewById(R.id.pager);
awesomePager.setOffscreenPageLimit(3);
awesomePager.setAdapter(awesomeAdapter);
//Title Page
//Get a reference to the TitlePageIndicator
TitlePageIndicator awesomeIndicator = (TitlePageIndicator) findViewById(R.id.indicator);
// Attach adapter to ViewPager
awesomePager.setAdapter(awesomeAdapter);
// Attach ViewPager to the indicator
awesomeIndicator.setViewPager(awesomePager);
}
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//super.onCreateOptionsMenu(menu);
//getMenuInflater().inflate(R.menu.activity_enlighten_webview_secondary, menu);
MenuInflater Inflater=getSupportMenuInflater();
Inflater.inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
WebView webView = (WebView) awesomePager.getChildAt(awesomePager.getCurrentItem());
switch (item.getItemId())
{
case R.id.menu_back:
webView.goBack();
Toast.makeText(AwesomePagerActivity.this, "Now ", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
private class AwesomePagerAdapter extends PagerAdapter{
private String[] titles = new String[]{"Dashboard", "Community", "Support"};
public int getCount() {
return NUM_AWESOME_VIEWS;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position % titles.length];
}
public Object instantiateItem(View collection, int position) {
WebView web = new WebView(AwesomePagerActivity.this);
View layout = null;
switch (position) {
case 0: {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.activity_main, null);
// changed from WebView web = (WebView)layout.findViewById(R.id.webView1);
layout.findViewById(R.id.webView);
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(false);
websettings.setUseWideViewPort(true);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
websettings.setSavePassword(true);
web.requestFocusFromTouch();
web.setWebViewClient(new WebViewClient());
web.setWebChromeClient(new WebChromeClient());
web.loadUrl("https://google.com");
}
break;
case 1: { LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.activity_community, null);
// WebView web = (WebView)layout.findViewById(R.id.webView2);
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(true);
websettings.setUseWideViewPort(true);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
websettings.setSavePassword(true);
web.requestFocusFromTouch();
web.setWebViewClient(new WebViewClient());
web.setWebChromeClient(new WebChromeClient());
web.loadUrl("http://developer.android.com");
}
break;
case 2: { LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = mInflater.inflate(R.layout.activity_support, null);
// WebView web = (WebView)layout.findViewById(R.id.webView3);
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(true);
websettings.setUseWideViewPort(false);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
websettings.setSavePassword(true);
web.requestFocusFromTouch();
web.setWebViewClient(new WebViewClient());
web.setWebChromeClient(new WebChromeClient());
web.loadUrl("http://www.xda-developers.com");
}
break;
}
((ViewPager) collection).addView((View)layout,0);
return layout;
}
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
public void finishUpdate(View arg0) {}
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
public Parcelable saveState() {
return null;
}
public void startUpdate(View arg0) {}
}
}
根据建议更新代码