2
I started studying Android development a little while ago, I saw that to use a Navigationdrawer the recommended is to use fragments to change a Framelayout in the main Activity, but in the new activities that create, did not want to have to put all the code of the Navigationdrawer again, wanted to know how to create a menu to use in any one.
Does anyone have any tips for me?
EDIT:
Mainactivity.java
public class MainActivity extends GenericActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpDrawerLayout();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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;
}
return super.onOptionsItemSelected(item);
}
}
Genericactivity.java
public class GenericActivity extends AppCompatActivity {
protected DrawerLayout drawerLayout;
protected Toolbar toolbar;
protected boolean drawerLayoutEnable;
protected NavigationView navigationView;
protected ActionBarDrawerToggle toggle;
protected void setUpDrawerLayout(){
this.drawerLayoutEnable = true;
toolbar = (Toolbar)findViewById(R.id.toolbar);
if(toolbar !=null){
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
setUpDrawerToggle();
setUpNavigationView();
}
}
private void setUpNavigationView() {
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener((NavigationView.OnNavigationItemSelectedListener) this);
}
private void setUpDrawerToggle(){
toggle = new ActionBarDrawerToggle(
this,
drawerLayout,
toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState();
}
@Override
public void onBackPressed() {
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
activity_generic.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.rafael.elite.BaseActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout" //Seria este o drawer_layout que eu tenho que referenciar?
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:paddingTop="24dp"
app:headerLayout="@layout/nav_drawer_header"
app:menu="@menu/menu_generic_activity" />
</android.support.v4.widget.DrawerLayout>
Take a look at this OS reply: http://stackoverflow.com/a/19451842/5165061
– Dener