【问题标题】:Replacing content of the fragment with navigation drawer item click用导航抽屉项目点击替换片段的内容
【发布时间】:2018-10-23 06:50:59
【问题描述】:

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

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

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

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

编辑: 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;
}

}

【问题讨论】:

  • 你尝试了什么?粘贴您尝试过的代码...
  • @AmitJangid ,我已经用代码更新了问题。我想要实现的是使用动态创建的菜单项更改单个片段的内容。实际上,我想通过单击每个菜单项来显示不同的回收器视图。只是为了测试,我使用的是当用户单击菜单项时要显示的菜单项的 id。

标签: android android-fragments navigation navigation-drawer


【解决方案1】:

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

检查一下它可能会工作......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多