如何在带有webview的滑动菜单导航抽屉中添加“返回”功能

时间:2015-07-11 18:42:42

标签: android android-fragments webview

everbody,请问我在Android应用程序上使用带有片段和webview的侧面菜单导航抽屉,但android后退按钮键无效。这是我的代码

主要活动

公共类MainActivity扩展了Activity {

String[] menutitles;
TypedArray menuIcons;
String[] pageUrl;

// nav drawer title
private CharSequence mDrawerTitle;
private CharSequence mTitle;

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

private List<RowItem> rowItems;
private CustomAdapter adapter;

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

    mTitle = mDrawerTitle = getTitle();

    menutitles = getResources().getStringArray(R.array.titles);
    menuIcons = getResources().obtainTypedArray(R.array.icons);
    pageUrl = getResources().getStringArray(R.array.pageurl);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.slider_list);

    rowItems = new ArrayList<RowItem>();

    for (int i = 0; i < menutitles.length; i++) {
        RowItem items = new RowItem(menutitles[i], menuIcons.getResourceId(
                i, -1), pageUrl[i]);
        rowItems.add(items);
    }

    menuIcons.recycle();

    adapter = new CustomAdapter(getApplicationContext(), rowItems);

    mDrawerList.setAdapter(adapter);

    mDrawerList.setOnItemClickListener(new SlideitemListener());

    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.app_name,
            R.string.app_name) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            invalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        // on first time display view for first nav item
        updateDisplay(0);

    }
}

class SlideitemListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        updateDisplay(position);
    }

}

private void updateDisplay(int position) {

    String url = rowItems.get(position).getPageUrl();

    Fragment fragment = new MyWebViewFragment();

    Bundle bundle = new Bundle();
    bundle.putString("url", url);

    fragment.setArguments(bundle);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit();

    setTitle(menutitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // toggle nav drawer on selecting action bar app icon/title
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar actions click
    switch (item.getItemId()) {
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

/***
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

/**
 * When using the ActionBarDrawerToggle, you must call it during
 * onPostCreate() and onConfigurationChanged()...
 */

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}



@Override
 public void onBackPressed() {
       Fragment WebView = getFragmentManager().findFragmentByTag("WebView");
       if (WebView instanceof MyWebViewFragment) {
              boolean goback = ((MyWebViewFragment)WebView).canGoBack();
              if (!goback)
                super.onBackPressed();
       }
 }

} MyWebViewFragment

公共类MyWebViewFragment扩展了Fragment {

ProgressDialog mProgress;
WebView webview;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.web_fragment, container,
            false);

    Bundle bundle = getArguments();

    String url = bundle.getString("url");

    webview = (WebView) rootView.findViewById(R.id.webview1);

    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);

    mProgress = ProgressDialog.show(getActivity(), "Loading",
            "Please wait for a moment...");
    webview.loadUrl(url);

    webview.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (mProgress.isShowing()) {
                mProgress.dismiss();
            }
        }
    });

    return rootView;
}

public boolean canGoBack() {
    // TODO Auto-generated method stub
    return false;
}

}

当我按下设备上的后退按钮不回来时,请帮助我。感谢

0 个答案:

没有答案