看看我的活動。
public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private Toolbar toolbar;
private ActionBarDrawerToggle toogle;
private ListView navList;
private SwitchCompat langSwitch;
private DashBoardCompanyInfoModel mDashBoardCompanyInfoModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
navList = (ListView) findViewById(R.id.menu_list_view_list);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
langSwitch = (SwitchCompat) findViewById(R.id.lang_switch);
Locale current = getResources().getConfiguration().locale;
if (current.getLanguage().equalsIgnoreCase("nl")) {
langSwitch.setChecked(true);
} else {
langSwitch.setChecked(false);
}
switchColor(langSwitch.isChecked());
langSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
switchColor(b);
changeLanguage();
final Intent refresh = new Intent(MainActivity.this, MainActivity.class);
startActivity(refresh);
finish();
}
});
toogle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toogle);
toogle.syncState();
final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
toogle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Utils.hideSoftKeyboard(MainActivity.this);
onBackPressed();
}
});
initComponent();
setUpDrawer();
replaceFragment(new DashboardFragment());
}
private void switchColor(boolean checked) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
langSwitch.getThumbDrawable().setColorFilter(checked ? Color.WHITE : Color.WHITE, PorterDuff.Mode.MULTIPLY);
final int color = ContextCompat.getColor(this, R.color.temp_login);
langSwitch.getTrackDrawable().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
}
}
private void setUpDrawer() {
View header = (View) getLayoutInflater().inflate(R.layout.header_view, null);
navList.addHeaderView(header);
final ArrayList<NavigationMenuModel> data = new ArrayList<>();
final NavigationMenuModel model = new NavigationMenuModel();
model.setMenuName(getString(R.string.about_us));
model.setMenuResource(R.drawable.ic_info_white);
data.add(model);
final NavigationMenuModel model1 = new NavigationMenuModel();
model1.setMenuName(getString(R.string.contact_us));
model1.setMenuResource(R.drawable.ic_email_white);
data.add(model1);
final NavigationMenuModel model2 = new NavigationMenuModel();
model2.setMenuName(getString(R.string.log_out));
model2.setMenuResource(R.drawable.ic_logout);
data.add(model2);
final NavigationListAdapter adapter = new NavigationListAdapter(this, android.R.layout.simple_spinner_item, data);
navList.setAdapter(adapter);
adapter.notifyDataSetChanged();
navList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 1:
addContactUsAndAboutUsFragment(getString(R.string.about_us));
break;
case 2:
addContactUsAndAboutUsFragment(getString(R.string.contact_us));
break;
case 3:
displayLogoutDialog();
break;
}
}
});
}
private void initComponent() {
}
public void addFragment(final Fragment newFragment, final Fragment hideFragment) {
getFragmentManager()
.beginTransaction()
.add(R.id.content_main_container, newFragment, newFragment.getClass().getSimpleName())
.hide(hideFragment)
.addToBackStack(hideFragment.getClass().getSimpleName())
.commit();
}
public void replaceFragment(final Fragment newFragment) {
getFragmentManager()
.beginTransaction()
.replace(R.id.content_main_container, newFragment, newFragment.getClass().getSimpleName())
.commit();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toogle.onOptionsItemSelected(item))
return true;
switch (item.getItemId()) {
case android.R.id.home:
Utils.hideSoftKeyboard(this);
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
finish();
}
return true;
// break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void setToolBar(String title, boolean back_flag) {
getSupportActionBar().setTitle(title);
toolbar.getMenu().clear();
if (back_flag) {
toogle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
toogle.setDrawerIndicatorEnabled(true);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}
private void addContactUsAndAboutUsFragment(final String title) {
final Bundle bundle = new Bundle();
bundle.putString(Constants.KEY_WEB_VIEW_TITLE, title);
final AboutUsFragment aboutUsFragment = new AboutUsFragment();
aboutUsFragment.setArguments(bundle);
addFragment(aboutUsFragment, getFragmentManager().findFragmentById(R.id.content_main_container));
}
public Toolbar getToolbar() {
return toolbar;
}
private void displayLogoutDialog() {
final AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle(getString(R.string.app_name));
dialog.setMessage(R.string.logout_message);
dialog.setCancelable(false);
dialog.setPositiveButton(getString(R.string.dialog_yes), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
final PreferenceUtils preferenceUtils = new PreferenceUtils(MainActivity.this);
preferenceUtils.clearPreference();
final Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
});
dialog.setNegativeButton(getString(R.string.dialog_no), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
dialog.show();
}
public DashBoardCompanyInfoModel getmDashBoardCompanyInfoModel() {
return mDashBoardCompanyInfoModel;
}
public void setmDashBoardCompanyInfoModel(DashBoardCompanyInfoModel mDashBoardCompanyInfoModel) {
this.mDashBoardCompanyInfoModel = mDashBoardCompanyInfoModel;
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
changeLanguage();
}
private void changeLanguage() {
if (langSwitch.isChecked()) {
final Locale myLocale = new Locale("nl");
final Resources res = getResources();
final DisplayMetrics dm = res.getDisplayMetrics();
final Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
} else {
final Locale myLocale = new Locale("en");
final Resources res = getResources();
final DisplayMetrics dm = res.getDisplayMetrics();
final Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
}
是的,你應該使用片段爲。因爲對於其他屏幕,您不需要創建活動。我的建議是,你應該使用它的片段。 –