Восстановление гамбургера
Добавлено: 09 дек 2016, 18:36
Здравствуйте когда я перехожу в фрагмент я меняю гамбургер на кнопку таким образом:
и при клике хочу вернуть Гамбургер, но ничего не выходит, подскажите что не так
Мой MainActivity
Код: Выделить всё
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
// activity = (MainActivity)getActivity();
// activity.InitBar();
((MainActivity)getActivity()).toolbar = (Toolbar) getActivity().findViewById(toolbar); // получаем тулбар
// слушаем клик по стрелке
((MainActivity)getActivity()).toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).onBackPressed();
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view=inflater.inflate(R.layout.fragment_blank, container, false);
((MainActivity)getActivity()).getSupportActionBar().setHomeButtonEnabled(false);
((MainActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); // меняем гамбургер на стрелку назад
return view;
}
Мой MainActivity
Код: Выделить всё
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Support support;
FragmentTransaction fTrans;
ActionBarDrawerToggle toggle;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
support = new Support();
}
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
//return true;
}
else if (id == R.id.home)
{
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
fTrans = getFragmentManager().beginTransaction();
if (id == R.id.nav_camera) {
// Handle the camera action
fTrans.replace(R.id.frgmCont, support);
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
fTrans.addToBackStack(null);
fTrans.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}