First you need to initialize Slidingmenu in MainActivity:
public class MainActivity extends BaseActivity{ private Fragment mContent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //В content_frame должен быть просто FrameLayout. Так же, как и в slidingmenu_frame setContentView(R.layout.content_frame); if (savedInstanceState != null) mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent"); if (mContent == null) mContent = new Fragment1(); // View основной, видимой части setContentView(R.layout.content_frame); getSupportFragmentManager() .beginTransaction() .replace(R.id.content_frame, mContent) .commit(); // установка View для SlidingMenu setBehindContentView(R.layout.slidingmenu_frame); getSupportFragmentManager() .beginTransaction() .replace(R.id.menu_frame, new MenuFragment()) .commit(); // настройка SlidingMenu getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); getSlidingMenu().setBehindOffset(70); } //Также нужно добавить метод для замены фрагментов. Он будет заменять фрагменты при выборе того или иного пункта SlidingMenu public void switchContent(Fragment fragment) { mContent = fragment; getSupportFragmentManager() .beginTransaction() .replace(R.id.content_frame, fragment) .commit(); getSlidingMenu().showContent(); } }
It would also be nice to add some code to call SlidingMenu:
@Override public void onBackPressed() { if ( getSlidingMenu().isMenuShowing()) { getSlidingMenu().toggle(); } else { super.onBackPressed(); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ( keyCode == KeyEvent.KEYCODE_MENU ) { this.getSlidingMenu().toggle(); return true; } return super.onKeyDown(keyCode, event); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: this.getSlidingMenu().toggle(); return true; default: return super.onOptionsItemSelected(item); } }
Now is the time to do the menu. As you can see, when initializing the menu, we call the MenuFragment class. You need to create a new MenuFragment class. This is what it should be about:
public class MenuFragment extends ListFragment { private String menuItems[]; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ return inflater.inflate(R.layout.list, null); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //Тут задаем имена для элементов SlidingMenu. В папке values нужно создать соответсвующие ресурсы menuItems = getResources().getStringArray(R.array.screens); /**Создадим кастомный адаптер, если мы хотим для каждого пункта SlidingMenu в придачу к тексту установить иконку. Если иконка вам не нужна, используйте обычный ArrayAdapter<String>.*/ MyCustomAdapter myAdapter = new MyCustomAdapter(this.getActivity(), R.layout.slidingmenu_item, menuItems); setListAdapter(myAdapter); } //Загрузка того или иного фрагмента при выборе соответствующего элемента меню @Override public void onListItemClick(ListView lv, View v, int position, long id) { Fragment newContent = null; switch (position) { case 0: newContent = new FirstFragment(); break; case 1: newContent = new SecondFragment(); break; case 2: newContent = new ThirdFragment(); break; } if (newContent != null) switchFragment(newContent); } //Теперь сам метод switchFragment, осуществляющий замену фрагментов. private void switchFragment(Fragment fragment) { if (getActivity() == null) return; if (getActivity() instanceof MainActivity) { MainActivity fca = (MainActivity) getActivity(); fca.switchContent(fragment); } } //А вот и наш кастомный адаптер для установки иконок к каждому пункту меню public class MyCustomAdapter extends ArrayAdapter<String> { Context context_; public MyCustomAdapter(Context context, int textViewResourceId, String[] objects) { super(context, textViewResourceId, objects); // TODO Auto-generated constructor stub context_ = context; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub return getCustomView(position, convertView, parent); } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub return getCustomView(position, convertView, parent); } public View getCustomView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub LayoutInflater inflater = LayoutInflater.from(context_); View row = inflater.inflate(R.layout.slidingmenu_item, parent, false); TextView label = (TextView) row.findViewById(R.id.row_title); label.setText(menuItems[position]); ImageView icon = (ImageView) row.findViewById(R.id.row_icon); //Вместо MenuItem1 ваше название пункта меню, вместо image1 - ваша картинка if (menuItems[position].equalsIgnoreCase("MenuItem1")) { icon.setImageResource(R.drawable.image1); } else if (menuItems[position].equalsIgnoreCase("MenuItem2")) { icon.setImageResource(R.drawable.image2); } else if (menuItems[position].equalsIgnoreCase("MenuItem3")) { icon.setImageResource(R.drawable.image3); } return row; } } }
Now you can create classes FirstFragment, SecondFragment and ThirdFragment, the following Fragment, and fill them with content. They will be the three "screens" that can be switched depending on the choice of a particular SlidingMenu item.