如何在导航抽屉android中实现背压方法?

时间:2014-03-11 07:21:01

标签: android android-fragments navigation-drawer

我使用SherlockFragment创建了导航抽屉。抽屉里有两个项目 -

  1. 片段1
  2. Fragment2
  3. 我想实现如下,如果用户在Fragment2上按下后退按钮,那么他/她应该被重定向到fragment1。

        public class Drawer extends SherlockFragmentActivity {
    DrawerLayout mDrawerLayout;
    ListView mDrawerList;
    ActionBarDrawerToggle mDrawerToggle;
    MenuListAdapter mMenuAdapter;
    String[] title;
    String[] subtitle;
    int[] icon;
    Fragment fragment1 = new Fragment1();
    Fragment fragment2 = new Fragment2();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drawer_main);
        title = new String[] { "Post a Squib", "Edit Profile"};
        icon = new int[] { R.drawable.action_about, R.drawable.action_settings};
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
        GravityCompat.START);
        mMenuAdapter = new MenuListAdapter(this, title, icon);
        mDrawerList.setAdapter(mMenuAdapter);
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,
                R.string.drawer_close) {
    
            public void onDrawerClosed(View view) {
                // TODO Auto-generated method stub
                super.onDrawerClosed(view);
            }
            public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
    
                super.onDrawerOpened(drawerView);
            }
        };
    
        mDrawerLayout.setDrawerListener(mDrawerToggle);
    
        if (savedInstanceState == null) {
            selectItem(0);
        }
    }
    @Override
    public void onBackPressed() {
    // TODO Auto-generated method stub
    //  super.onBackPressed();
    if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
        mDrawerLayout.closeDrawer(mDrawerList);
    }else{
    
        //i want changes in this part
        // currently when user is in second fragment/first fragment and press back button then dialogue box is appeared
        //But I'd like to have such like user press back button in fragment2 it should be redirected to fragment1.
    
        final Builder builder = new Builder(this);
        builder.setTitle("Attention");
        builder.setMessage("Do you really want to exit?");
        builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
            @Override
            public void onClick(final DialogInterface dialog, final int which) {
                     Drawer.this.finish();
                    dialog.dismiss();    
           }
       });
        builder.setNegativeButton(android.R.string.cancel,
                new OnClickListener() {
                    @Override
                    public void onClick(final DialogInterface dialog,
                            final int which) {
                                                  dialog.dismiss();
                    }
                });
        final AlertDialog dialog = builder.create();
        dialog.show();
    }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
                mDrawerLayout.closeDrawer(mDrawerList);
            } else {
                mDrawerLayout.openDrawer(mDrawerList);
            }
        }
        return super.onOptionsItemSelected(item);
    }
    // The click listener for ListView in the navigation drawer
    private class DrawerItemClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            selectItem(position);
        }
    }
    private void selectItem(int position) {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(R.id.content_frame, fragment1);
            break;
        case 1:
            ft.replace(R.id.content_frame, fragment2);
            break;
        }
        ft.commit();
        mDrawerList.setItemChecked(position, true);
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    }
    

1 个答案:

答案 0 :(得分:0)

我没有在sherelock libs上工作,但我工作了一点。

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) 
    {

    case android.R.id.home:
         // Do whatever you want, e.g. finish()
        finish();
         break;

      }
    return true;
}

}

希望它会对你有所帮助。

如果您需要详细信息,请点击以下链接。

How to disable back button pressed in android fragment class

Actionbarsherlock back button and smartphone back button

Android ActionBarSherlock On Top Icon Back