我希望在导航抽屉中使用开关按钮,以便在主布局中添加和删除片段。
这是我的代码 - `menuitem.xml`
这是我的代码 - `menuitem.xml`
<group
android:id="@+id/drawer_group1"
android:checkableBehavior="single">
<item
android:id="@+id/nav_timer"
android:icon="@drawable/ic_timer"
android:title="Timer">
</item>
<item
android:id="@+id/addFragment_Bt"
app:actionViewClass="android.widget.Switch"
android:title="Most Used" />
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings"
android:title="Settings">
</item>
</group>`
MainActivity.class
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
switch (menuItem.getItemId()) {
case R.id.addFragment_Bt:
Switch switchCompat = findViewById(R.id.addMostUsed_Bt);
switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(R.id.Most_Used_Fragment_container);
if (isChecked == true) {
if (fragment != null) {
fragmentManager.popBackStack();
}
}
}
});
break;
}
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
现在我只是试图删除已添加的片段。