有没有办法在我的 MainActivity 中为我的 ViewPager 创建的操作栏创建我的菜单项,这样我只创建一次我的 webview 项(后退、前进和刷新按钮)。然后在我的 onOptions.... 然后指定能够处理我的 webview 的菜单项。
我的 ViewPager 中的所有片段都是 webview,我只想要 3 个常量 Actionbar 项来控制正在显示的 webview。而不是每次我的 viewpager 加载另一个片段时都重新创建它们。有任何想法吗???
分段
public class Fragment1 extends SherlockFragment {
WebView web;
static ProgressBar progressBar;
@Override
public View onCreateView(LayoutInflater inf, ViewGroup grp, Bundle icicle) {
View v = inf.inflate(R.layout.activity_main, grp, false);
web = (WebView) v.findViewById(R.id.webView);
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
//Now i know that i need this to use MENU ITEMS in this fragment
setHasOptionsMenu(true);
//
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
web.setWebViewClient(new SpecialWebView(progressBar));
web.setWebChromeClient(new SpecialWebChromeClient(progressBar));
// Progress Bar - End
WebSettings websettings = web.getSettings();
websettings.setBuiltInZoomControls(false);
web.setBackgroundColor(Color.parseColor("#f2f2f2"));
websettings.setUseWideViewPort(true);
websettings.setJavaScriptEnabled(true);
websettings.setAllowFileAccess(true);
websettings.setDomStorageEnabled(true);
websettings.setLoadWithOverviewMode(true);
websettings.setSavePassword(true);
web.loadUrl("http://www.google.com");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater Inflater = getSupportMenuInflater();
Inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem Item) {
final int current = ((ViewPager) mFragments).getCurrentItem();
String currentId = current == 0 ? FRAGMENT1: (current == 1 ? FRAGMENT2: FRAGMENT3);
//WebView web = (WebView) findViewById(currentId);
switch (Item.getItemId()) {
case R.id.menu_back:
web.goBack();
return true;
case R.id.menu_forward:
web.goForward();
return true;
case R.id.menu_refresh:
web.reload();
return true;
}
return super.onOptionsItemSelected(Item);
}
主要活动
public class MainActivity extends SherlockFragmentActivity {
private ViewPager mPager;
private TitlePageIndicator mIndicator;
private MainPagerAdapter mAdapter;
private List<Fragment> mFragments;
private static final String FRAGMENT1 = Fragment1.class.getName();
private static final String FRAGMENT2 = Fragment2.class.getName();
private static final String FRAGMENT3 = SupportFragment.class.getName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_main);
// add fragments
mFragments = new ArrayList<Fragment>();
mFragments.add(Fragment.instantiate(this, FRAGMENT1));
mFragments.add(Fragment.instantiate(this, FRAGMENT2));
mFragments.add(Fragment.instantiate(this, FRAGMENT3));
// adapter
mAdapter = new MainPagerAdapter(getSupportFragmentManager(), mFragments);
// pager
mPager = (ViewPager) findViewById(R.id.view_pager);
mPager.setOffscreenPageLimit(3);
mPager.setAdapter(mAdapter);
// indicator
mIndicator = (TitlePageIndicator) findViewById(R.id.title_indicator);
mIndicator.setViewPager(mPager);
}