将片段的内容替换为导航抽屉项目单击



这是我正在尝试做的测试。我不知道它是否可以完成。 我正在尝试做的是,当用户单击导航抽屉项时,我想更改片段的内容。我没有使用不同的片段,实际上我只使用一个片段。

我想要实现的是,当用户单击抽屉菜单项时,片段应根据单击显示数据,为简单起见,只需显示片段中菜单项的名称。

通过使用单个片段,我希望通过不同的菜单项单击获得不同的内容。

导航抽屉中的菜单项是动态创建的,因为它旨在从数据库中显示,并且菜单项的数量可以是可变的。

编辑: 导航抽屉活动

public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
FragmentManager fragmentManager;
Fragment testFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
final Menu menu = navigationView.getMenu();
for (int i = 1; i <= 10; i++) {
menu.add(0,i,0,"Set "+ i);
}
fragmentManager = getSupportFragmentManager();
testFragment = new TestFragment();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();

for (int i = 0; i<10; i++) {
// Handle the camera action
if (id == i){
Bundle bundle = new Bundle();
bundle.putString("SET_ID", "this is test"+i);
// set Fragmentclass Arguments
testFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, testFragment).commit();
}
}

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

}

测试片段

public class TestFragment extends Fragment {
TextView textView;
public TestFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_test, container, false);
String idValue = getArguments().getString("SET_ID");
Log.d("TEST_VALUE***", idValue);
textView = view.findViewById(R.id.just_Text);
textView.setText("Set id is: "+idValue);

return view;
}

}

尝试在onNavigationItemSelected中添加以下行以进行 lopp

testFragment = new TestFragment(); // this line

例:

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

for (int i = 0; i<10; i++) {
// Handle the camera action
if (id == i){
Bundle bundle = new Bundle();
bundle.putString("SET_ID", "this is test"+i);
// set Fragmentclass Arguments
testFragment = new TestFragment(); // add this line in your code.
testFragment.setArguments(bundle);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container, testFragment).commit();
}
}

检查一下它可能会起作用...

最新更新