0

我有一个使用片段容器的导航抽屉活动及其中的片段。我有三个片段,我需要其中一个名为locate_login不包含我的导航抽屉。如何实现setDrawerLockMode或禁用导航抽屉

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工作室初学者。

回答

0

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

咱们说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); 
}