这是我要尝试的测试。我不知道是否可以做到。 我想做的是,当用户单击“导航抽屉”项时,我想更改片段的内容。我没有使用不同的片段,实际上我只使用了一个片段。
我要实现的是,当用户单击抽屉菜单项时,该片段应按单击显示数据,为简单起见,片段中的菜单项仅是名称。
通过使用单个片段,我想通过单击不同的菜单项获得不同的内容。
“导航抽屉”中的菜单项是动态创建的,旨在从数据库中显示出来,并且菜单项的数量可以可变。
编辑: 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;
}
}
TestFragment
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;
}
}
答案 0 :(得分:2)
尝试在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();
}
}
检查此方法是否可行...
答案 1 :(得分:0)
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.btn1) {
// Handle the btn1 action
} else if (id == R.id.btn2) {
txt_fragment.settext("...");
or whatever you want to change!
// Handle the btn2 action
} else if (id == R.id....) {
}
.
.
.
drawer.closeDrawer(GravityCompat.END);
return true;
}