使用导航抽屉项目单击替换片段的内容

Replacing content of the fragment with navigation drawer item click

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

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

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

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

编辑: NavigationDrawerActivity

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 循环中添加以下行

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

检查它可能有效...