Study Notes of SlidingMenu, an open-source Android Project (2)
The usage of SlidingMenu has been introduced earlier: Study Notes of SlidingMenu, an open-source Android Project (I). Next, let's take a deeper look and switch to the corresponding page based on the Menu of the slide item.
Directory structure:
Click Bluetooth to switch to the corresponding interface.
Key code
MainActivity. java
Package com. dzt. slidingmenudemo; import android. app. fragment; import android. app. fragmentManager; import android. app. fragmentTransaction; import android. OS. bundle; import android. util. log; import com. dzt. slidingmenudemo. fragment. export thfragment; import com. dzt. slidingmenudemo. fragment. displayFragment; import com. dzt. slidingmenudemo. fragment. homeFragment; import com. dzt. slidingmenudemo. fragment. menuFragmen T; import com. dzt. slidingmenudemo. fragment. wifiFragment; import com. dzt. slidingmenudemo. fragment. menuFragment. onMenuListOnItemClickListener; import com. slidingmenu. lib. slidingMenu; import com. slidingmenu. lib. app. slidingActivity;/*** switch to the corresponding Fragment based on different menus ** @ author Administrator **/public class MainActivity extends SlidingActivity implementsOnMenuListOnItemClickListener {private SlidingMenu mChan NelMenu; @ Overridepublic void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); // set the title setTitle of the title bar (test SlidingMenu); setContentView (R. layout. content_frame); initChannelMenu ();} private void initChannelMenu () {setBehindContentView (R. layout. menu_frame); // create the SlidingMenu object mChannelMenu = getSlidingMenu (); // set the position of the slide menu, which is on the left. When the menu is pulled, mChannelMenu is displayed on the left. setMode (SlidingMenu. LEFT); // set the touch range. Here, the full screen mChannelMenu is set. setTouchModeAbove (sregistringmenu. TOUCHMODE_FULLSCREEN); // set the shadow width. Check the second one on the top and place it on the right. There is a shadow transition. This is the mChannelMenu. setShadowWidthRes (R. dimen. shadow_width); // specifies the shadow effect. You can set an image or a color transition mChannelMenu. setShadowDrawable (R. drawable. shadow); // set the spacing between the backend and the sliding column and the original interface. setBehindOffsetRes (R. dimen. slidingmenu_offset); // border angle. mChannelMenu indicates the border. setFadeDegree (0.35f); // sets the mChannelMenu mode for the touch screen. setTouchModeAbove (sregistringmenu. TOUCHMODE_FULLSCREEN); // sets the content of the SlidingMenu FragmentTransaction fragmentTransaction = GetFragmentManager (). beginTransaction (); MenuFragment menuFragment = new MenuFragment (); menuFragment. setOnMenuListOnItemClickListener (this); fragmentTransaction. replace (R. id. menu_frame, menuFragment); fragmentTransaction. replace (R. id. content_frame, new HomeFragment (); fragmentTransaction. commit () ;}@ Overridepublic void onBackPressed () {if (mChannelMenu. isMenuShowing () {// hide the SlidingMenu. The Content here is my Main ActivitymChannelMenu. showContent ();} else {super. onBackPressed () ;}@overridepublic void onSelectItem (int position, String title) {// TODO Auto-generated method stubSystem. out. println (onSelectItem title = + title); Fragment fragment = null; switch (position) {case 0: fragment = new HomeFragment (); break; case 1: fragment = new WifiFragment (); break; case 2: fragment = new incluthfragment (); break; case 3: Fragment = new DisplayFragment (); break; default: break;} if (fragment! = Null) {FragmentManager fragmentManager = getFragmentManager (); fragmentManager. beginTransaction (). replace (R. id. content_frame, fragment ). commit (); // update selected item and title, then close the drawersetTitle (title); mChannelMenu. showContent ();} else {// error in creating fragmentLog. e (MainActivity, Error in creating fragment );}}}
Slide out of Fragment MenuFragment. java
package com.dzt.slidingmenudemo.fragment;import java.util.ArrayList;import android.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ListView;import com.dzt.slidingmenudemo.MenuListAdapter;import com.dzt.slidingmenudemo.R;import com.dzt.slidingmenudemo.domain.MenuItem;public class MenuFragment extends Fragment implements OnItemClickListener {private ListView mLv;private String[] mMenuTitles;private ArrayList mMenuItem;private MenuListAdapter mAdapter;private OnMenuListOnItemClickListener mListener;public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {View rootView = inflater.inflate(R.layout.menu_fragment, container,false);initWidgets(rootView);return rootView;}private void initWidgets(View rootView) {mLv = (ListView) rootView.findViewById(R.id.left_menu);mMenuTitles = getResources().getStringArray(R.array.menu_title);mMenuItem = new ArrayList();for (int i = 0; i < mMenuTitles.length; i++) {MenuItem item = null;if (i == 2) {item = new MenuItem(mMenuTitles[i], R.drawable.ic_launcher,18, true);} else if (i == 4) {item = new MenuItem(mMenuTitles[i], R.drawable.ic_launcher,40, true);} else {item = new MenuItem(mMenuTitles[i], R.drawable.ic_launcher);}mMenuItem.add(item);}mAdapter = new MenuListAdapter(getActivity(), mMenuItem);mLv.setAdapter(mAdapter);mLv.setOnItemClickListener(this);}@Overridepublic void onItemClick(AdapterView
parent, View view, int position,long id) {// TODO Auto-generated method stubmLv.setItemChecked(position, true);mLv.setSelection(position);if (mListener != null) {mListener.onSelectItem(position, mMenuTitles[position]);}}public void setOnMenuListOnItemClickListener(OnMenuListOnItemClickListener listener) {mListener = listener;}public interface OnMenuListOnItemClickListener {public void onSelectItem(int position, String title);}}
After you click Menu, the listener for clicking is implemented in MainActivity, and the corresponding page will be switched.