Answer the question
In order to leave comments, you need to log in
How to make navigation drawer work in all fragments?
I have a main activity which has a navigation drawer:
public class MainActivity extends ActionBarActivity {
private String[] mScreenTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mScreenTitles = getResources().getStringArray(R.array.screen_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
mDrawerList.setAdapter(new ArrayAdapter<String>(this,R.layout.nav_draw, mScreenTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// Àêòèâèðóåì êíîïêó Home â ïàíåëè Action Bar:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
)
{
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
public void Click88(View view) {
setContentView(R.layout.russia);
}
public void Click120(View view) {
setContentView(R.layout.piter);
}
public void Click3(View view) {
setContentView(R.layout.prigo);
}
public void Click4(View view) {
setContentView(R.layout.sochi);
}
public void Click(View view) {
setContentView(R.layout.so_bul);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Íàñòðàèâàåì äåéñòâèå êíîïêè ïîèñêà:
switch(item.getItemId()) {
case R.id.action_search:
// Ïîêàçûâàåì Toast ñîîáùåíèå î ïîèñêå:
Toast.makeText(this, R.string.action_search, Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_search).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
// Íàñòðàèâàåì ñëóøàòåëÿ íàæàòèé ïî ïóíêòàì ìåíþ ñïèñêà â Navigation Drawer:
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ScreenOne();
break;
case 1:
fragment = new ScreenTwo();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_for_drawer, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mScreenTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e(this.getClass().getName(), "Ïðîèçîøëà îøèáêà!");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
public class S extends Fragment {
public S() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.s, container,
false);
return rootView;
}
}
Answer the question
In order to leave comments, you need to log in
In the activity, do this for each fragment (add a listener)
public class MainActivity extends AppCompatActivity
implements YourNameFragment.OnFragmentInteractionListener
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
private OnFragmentInteractionListener mListener;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question