Android导航抽屉中的多个Webview的背景

时间:2014-01-08 00:10:07

标签: android webview android-webview

目前正在使用导航抽屉的

* 每个标签都有一个webview。我能够加载页面和导航,但我不能回去。回来关闭应用程序。以下onbackpressed仅适用于1 webview 1 webview1(后面适用于此),但其他webview后关闭了应用程序。有没有办法将多个webview添加到onbackpressed *

public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    ****@Override
public void onBackPressed() {
    WebView webView1;
    webView1 = (WebView) findViewById(R.id.webView1);
    // If there is history in the web view, go back
    if (webView1.canGoBack()) {
        webView1.goBack();
        return;
    }
    //Handle anything else
    super.onBackPressed();
}****



    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;

    /**
     * Used to store the last screen title. For use in {@link #restoreActionBar()}.
     */
    private CharSequence mTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
                .commit();
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.title_section1);
                break;
            case 2:
                mTitle = getString(R.string.title_section2);
                break;
            case 3:
                mTitle = getString(R.string.title_section3);
                break;
            case 4:
                mTitle = getString(R.string.title_section4);
                break;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            // Only show items in the action bar relevant to this screen
            // if the drawer is not showing. Otherwise, let the drawer
            // decide what to show in the action bar.
            getMenuInflater().inflate(R.menu.main, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }




    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_OBJECT = "object";

        private WebView webView;



        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_OBJECT, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        public PlaceholderFragment() {
        }



         @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            Bundle args = getArguments();
            int position = args.getInt(ARG_OBJECT);

            int tabLayout = 1;
            switch (position) {
                case 1:
                    tabLayout = R.layout.fragment_main;
                    break;
                case 2:
                    tabLayout = R.layout.fashion;
                    break;
                case 3:
                    tabLayout = R.layout.entertainment;

                    break;
                case 4:
                    tabLayout = R.layout.computer;
                    break;
            }

            View rootView = inflater.inflate(tabLayout, container, false);

            **webView = (WebView) rootView.findViewById(R.id.webView1);
            if (webView != null) {
                WebSettings webSettings = webView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                webView.setWebViewClient(new WebViewClient());
                webView.loadUrl("http://headgone.net/shop/index.php/motorsports");


            }
            webView = (WebView) rootView.findViewById(R.id.fashion);
            if (webView != null) {
                WebSettings webSettings = webView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                webView.setWebViewClient(new WebViewClient());
                webView.loadUrl("http://headgone.net/shop/index.php/headgone-clothing");


            }
            webView = (WebView) rootView.findViewById(R.id.entertainment);
            if (webView != null) {
                WebSettings webSettings = webView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                webView.setWebViewClient(new WebViewClient());
                webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
                webView.loadUrl("http://www.headgone.net/entertainment/index.php/homepage");

            }
            webView = (WebView) rootView.findViewById(R.id.computer);
            if (webView != null) {
                WebSettings webSettings = webView.getSettings();
                webSettings.setJavaScriptEnabled(true);
                webView.setWebViewClient(new WebViewClient());
                webView.loadUrl("http://headgone.net/entertainment/index.php/computer-repairs");**

            }


            return rootView;

        }



}}

1 个答案:

答案 0 :(得分:0)

在按钮中尝试此操作。

Fragment fragment;
            FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction();
fragment = new YourXMLWebViewName();
            fragmentTransaction.replace(R.id.currentXMLId, fragment).addToBackStack(null).commit();