Hello.

There was the following problem. I connected the library SlidingMenu , created xxx.xml (side menu) and xxx.class, on .xml (the layout that added the button, wrote the following code to .class:

public class Xxx extends Activity implements OnClickListener{ Button buttonxxx; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.xxx); buttonxxx = (Button) findViewById(R.id.buttonxxx); buttonxxx.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.buttonxxx: Intent intent = new Intent(this, Xxxtwo.class); startActivity(intent); break;} } } 

But when you press the button, nothing happens. What could be the problem?

  • what are you waiting for? and where does the side menu? - Gorets
  • Waiting for the button (buttonxxx) to switch to another Activity (Xxxtwo.class). The side menu (xxx) while the button (buttonxxx) is on it, in order to call it up, you need to run your finger across the screen from left to right. It (xxx) is called, there is a button (buttonxxx) on it, but when pressed, it doesn’t react as well. - Sendz
  • Do you want Slidingmenu in the second activity to be active too? The fact is that Slidingmenu involves switching fragments (Fragments) within the same acnivity, and not switching between different activities when choosing one or another Slidingmenu item. If I understand your question correctly, then I can explain in more detail. - Tuhlom
  • Yes, you understood correctly. Explain, please. - Sendz
  • Tell me how to make the listView appear when a certain item is selected from the side menu. There is a ListFragment, but then how to display the side menu itself? - user18464

2 answers 2

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.

  • Ask if that. Just recently he dealt with SlidingMenu - Tuhlom

Button id R.id.buttonxxxa you catch R.id.lschedulecalls

  • I'm sorry. When adding code to a question, I decided to change the name of the button, layout and class. they are not very aesthetic. But there when changed did not notice that id is specified. - Sendz 5:43
  • I didn’t work with SlidingMenu, but I think the problem is that Xxx is inherited from an ordinary Activity, by the way google doped the side menu in saport pretty well. developer.android.com/intl/ru/training/implementing-navigation/...ps even when it’s written Names must be understood for themselves - gadfil 6:02