【问题标题】:How to implement setDrawerLockMode or disable navigation drawer如何实现 setDrawerLockMode 或禁用导航抽屉
【发布时间】:2017-08-17 00:23:31
【问题描述】:

通过使用片段容器,我有一个导航抽屉活动,其中包含片段。我在其中包含三个片段,我需要其中一个名为 locate_login 的片段不包含我的导航抽屉。

MainActivity.java

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); //nav drawer

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

        //Set the fragment initially
        locate_login fragment = new locate_login();
        android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragment);
        fragmentTransaction.commit();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

    public void onSelectFragment(View v){ //ignore this
        Fragment newFragment;

        if(v == findViewById(R.id.trigger)){
            newFragment = new locate_after();
        }
        else{
            newFragment = new locate_before();
        }

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        drawer.closeDrawer(GravityCompat.START);
        return true;

}

} 

这是我的 locate_login 片段

public class locate_login extends Fragment{

    public locate_login () {
        // Required empty public constructor

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.locate_login, container, false);
        // Inflate the layout for this fragment
        return rootView;

    }

}

由于我是 Android Studio 的初学者,如果能详细回答我将不胜感激。

【问题讨论】:

    标签: android android-fragments android-activity navigation-drawer


    【解决方案1】:

    为一些常量参数创建一个类。声明一些常量变量,例如:

    比如说StringConstant.java:

    public static final int LOCK_DRAWER = 1;
    public static final int SHOW_DRAWER = 2;
    

    从抽屉导航时,使用Intent 对象:

    如果要显示抽屉,请使用:

    intent.putExtra("SHOW_DRAWER", StringConstant.SHOW_DRAWER);
    

    或

    如果您不想显示抽屉,请使用:

    intent.putExtra("SHOW_DRAWER", StringConstant.LOCK_DRAWER);
    

    从fragment 类中检索意图,你说locate_login。

    int status = getActivity.getIntent().getIntExtra("SHOW_DRAWER", 0);
    

    检查状态:

    if (status == String.Constant.LOCK_DRAWER){
        lockDrawer();
    }
    
    public void lockDrawer() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    }
    

    解锁抽屉:

    public void unlockDrawer() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 2013-07-27
      • 2015-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多