我正在尝试弄清楚如何直接显示 memberFragment.class 片段而不是 MainActivity ,而不在导航栏上选择它。
我的代码:
public class AdminActivity extends AppCompatActivity {
private DrawerLayout mdrawerl;
private ActionBarDrawerToggle mtoggle;
private FirebaseAuth auth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
auth = FirebaseAuth.getInstance();
mdrawerl = (DrawerLayout) findViewById(R.id.drawer);
mtoggle = new ActionBarDrawerToggle(this,mdrawerl,R.string.open, R.string.close);
mdrawerl.addDrawerListener(mtoggle);
NavigationView nvDrawer = (NavigationView) findViewById(R.id.nv) ;
mtoggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setupDrawerContent(nvDrawer);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mtoggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
public void selectItemDrawer(MenuItem menuItem) {
auth = FirebaseAuth.getInstance();
Fragment myFragment = null;
Class fragmentClass;
switch (menuItem.getItemId()){
case R.id.signout:
auth.getInstance().signOut();
Intent i = new Intent(AdminActivity.this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
return;
}
switch (menuItem.getItemId()){
case R.id.members:
fragmentClass = memberFragment.class;
break;
case R.id.transactions:
fragmentClass = transaction.class;
break;
default:
fragmentClass = member.class;
}
try {
myFragment = (Fragment) fragmentClass.newInstance();
}
catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flcontent,myFragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
mdrawerl.closeDrawers();
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
selectItemDrawer(item);
return false;
}
});
}
这里的问题是,在启动活动时,它会在活动本身而不是片段 memberFragment 上启动,作为默认值。
答案 0 :(得分:1)
尝试使用以下代码,将以下代码放在onCreate()
:
memberFragment frag = new memberFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.flcontent, frag)
.commit();
完整代码应该是这样的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
auth = FirebaseAuth.getInstance();
mdrawerl = (DrawerLayout) findViewById(R.id.drawer);
mtoggle = new ActionBarDrawerToggle(this,mdrawerl,R.string.open, R.string.close);
mdrawerl.addDrawerListener(mtoggle);
NavigationView nvDrawer = (NavigationView) findViewById(R.id.nv) ;
mtoggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setupDrawerContent(nvDrawer);
memberFragment frag = new memberFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.flcontent, frag)
.commit();
}
答案 1 :(得分:1)
您无法在没有活动的情况下启动片段,但可以在活动启动后突然启动片段。
为此,请在活动onCreate和 从NavigationView 中选择最重要的部分菜单项进行简单的片段交易。
复制以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
auth = FirebaseAuth.getInstance();
mdrawerl = (DrawerLayout) findViewById(R.id.drawer);
mtoggle = new ActionBarDrawerToggle(this,mdrawerl,R.string.open,
R.string.close);
mdrawerl.addDrawerListener(mtoggle);
NavigationView nvDrawer = (NavigationView) findViewById(R.id.nv) ;
mtoggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setupDrawerContent(nvDrawer);
memberFragment frag = new memberFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.flcontent, frag)
.commit();
nvDrawer.getMenu().getItem(0).setChecked(true);
}