嗨,我是一名学生,正在尝试构建一个应用程序。我想通过在导航抽屉上点击进入主要活动来加载片段。我能够将导航抽屉附加到家庭活动中。但是使用以下代码,我无法在活动中加载片段。 “ loadDashboard”功能工作正常,但“ onNavigationItemSelected”方法无效。请帮助...代码在下面...
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
loadDashboad();
NavigationView nv = findViewById(R.id.navigation_view);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menuitemdashboard:
getSupportFragmentManager().beginTransaction().replace(R.id.viewContainer, new dashboard()).commit();
break;
case R.id.menuitemlogout:
break;
case R.id.menuitemprofile:
break;
case R.id.menuitemreports:
break;
case R.id.menuitemvisits:
getSupportFragmentManager().beginTransaction().replace(R.id.viewContainer, new visits()).commit();
break;
}
return true;
}
});
}
private void loadDashboad() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
dashboard dash = new dashboard();
ft.add(R.id.viewContainer, dash);
ft.commit();
}
}
答案 0 :(得分:0)
您的代码很好,您所缺少的是为导航菜单项提供功能。将以下行替换为要添加的片段
child: Row(children: <Widget>[
Column(children: <Widget>[
Padding(
padding: EdgeInsets.all(5.0),
child: Container(
decoration: new BoxDecoration(
border: new Border(right: BorderSide(color: Colors.black))
),
child: Column(
children: <Widget>[
Text('Points'),
Text('38'),
],
),
),
),
],),
// green bar
Column(children: <Widget>[
Padding(
padding: EdgeInsets.only(right:10.0),
child: Container(
width:ratewidth,
decoration: new BoxDecoration(
border: new Border(bottom: BorderSide(color: Colors.green, width: 5.0))
),
),
)
],),
//yellow bar
Column(children: <Widget>[
Padding(
padding: EdgeInsets.only(right:10.0),
child: Container(
width:ratewidth,
decoration: new BoxDecoration(
border: new Border(bottom: BorderSide(color: Colors.yellow, width: 5.0))
),
),
),
],),
...
],)