我正在所有选项卡上使用 webview 创建一个选项卡式应用程序。每个选项卡都转到不同的 url,用户可以在其中单击链接,它会向他们显示应用程序中的链接。但是,我无法实现后退按钮,以便用户可以返回上一页。这是我的 MainActivity.java:
package com.theprospectordaily;
import java.util.Locale;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
import android.content.Intent;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
WebView myWebView;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the three primary sections of the app. We use a
* {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
CollectionPagerAdapter mCollectionPagerAdapter;
/**
* The {@link android.support.v4.view.ViewPager} that will display the
* object collection.
*/
ViewPager mViewPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an adapter that when requested, will return a fragment
// representing an object in
// the collection.
//
// ViewPager and its adapters use support library fragments, so we must
// use
// getSupportFragmentManager.
mCollectionPagerAdapter = new CollectionPagerAdapter(
getSupportFragmentManager());
// Set up action bar.
final ActionBar actionBar = getActionBar();
// Specify that we will be displaying tabs in the action bar.
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Set up the ViewPager, attaching the adapter and setting up a listener
// for when the
// user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mCollectionPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select
// the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if
// we have a reference to the
// Tab.
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mCollectionPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter.
// Also specify this Activity object, which implements the
// TabListener interface, as the
// listener for when this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mCollectionPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the primary sections of the app.
*/
public class CollectionPagerAdapter extends FragmentPagerAdapter {
final int NUM_ITEMS = 5; // number of tabs
public CollectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = new TabFragment();
Bundle args = new Bundle();
args.putInt(TabFragment.ARG_OBJECT, i);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return NUM_ITEMS;
}
@Override
public CharSequence getPageTitle(int position) {
String tabLabel = null;
switch (position) {
case 0:
tabLabel = getString(R.string.label1);
break;
case 1:
tabLabel = getString(R.string.label2);
break;
case 2:
tabLabel = getString(R.string.label3);
break;
case 3:
tabLabel = getString(R.string.label4);
break;
case 4:
tabLabel = getString(R.string.label5);
}
return tabLabel;
}
}
/**
* A fragment that launches other parts of the demo application.
*/
public static class TabFragment extends Fragment {
public static final String ARG_OBJECT = "object";
private WebView webView;
private Bundle webViewBundle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webViewBundle = new Bundle();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
int position = args.getInt(ARG_OBJECT);
int tabLayout = 0;
switch (position) {
case 0:
tabLayout = R.layout.tab1;
break;
case 1:
tabLayout = R.layout.tab2;
break;
case 2:
tabLayout = R.layout.tab3;
break;
case 3:
tabLayout = R.layout.tab4;
break;
case 4:
tabLayout = R.layout.tab5;
break;
}
View rootView = inflater.inflate(tabLayout, container, false);
webView = (WebView) rootView.findViewById(R.id.webView1);
if (webView != null) {
webView.setWebViewClient(new WebViewClient());
if (webViewBundle == null || webViewBundle.isEmpty()) {
webView.loadUrl("http://www.theprospectordaily.com/category/news/");
} else {
webView.restoreState(webViewBundle);
webViewBundle.clear();
}
}
webView = (WebView) rootView.findViewById(R.id.webView2);
if (webView != null) {
webView.setWebViewClient(new WebViewClient());
if (webViewBundle == null || webViewBundle.isEmpty()) {
webView.loadUrl("http://www.theprospectordaily.com/category/entertainment/");
} else {
webView.restoreState(webViewBundle);
webViewBundle.clear();
}
}
webView = (WebView) rootView.findViewById(R.id.webView3);
if (webView != null) {
webView.setWebViewClient(new WebViewClient());
if (webViewBundle == null || webViewBundle.isEmpty()) {
webView.loadUrl("http://www.theprospectordaily.com/category/sports/");
} else {
webView.restoreState(webViewBundle);
webViewBundle.clear();
}
}
webView = (WebView) rootView.findViewById(R.id.webView4);
if (webView != null) {
webView.setWebViewClient(new WebViewClient());
if (webViewBundle == null || webViewBundle.isEmpty()) {
webView.loadUrl("http://www.theprospectordaily.com/category/multimedia/");
} else {
webView.restoreState(webViewBundle);
webViewBundle.clear();
}
}
webView = (WebView) rootView.findViewById(R.id.webView5);
if (webView != null) {
webView.setWebViewClient(new WebViewClient());
if (webViewBundle == null || webViewBundle.isEmpty()) {
webView.loadUrl("http://www.theprospectordaily.com/category/perspectives/");
} else {
webView.restoreState(webViewBundle);
webViewBundle.clear();
}
}
return rootView;
}
}
}
这是我第一次学习如何编码,所以我知道它到处都是,而且可能有很多错误。我为此提前道歉!