frame_menu.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/menu" android:layout_width="match_parent" android:layout_height="match_parent" />
frame_content.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" />
MainActivity.java
public class MainActivity extends SlidingActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("SlidingMenu Demo"); setContentView(R.layout.frame_content); // set the Behind View setBehindContentView(R.layout.frame_menu); FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction(); MenuFragment menuFragment = new MenuFragment(); fragmentTransaction.replace(R.id.menu, menuFragment); fragmentTransaction.replace(R.id.content, new ContentFragment("Welcome")); fragmentTransaction.commit(); // customize the SlidingMenu SlidingMenu sm = getSlidingMenu(); sm.setShadowWidth(50); sm.setShadowDrawable(R.drawable.shadow); sm.setBehindOffset(60); sm.setFadeDegree(0.35f); //设置slding menu的几种手势模式 //TOUCHMODE_FULLSCREEN 全屏模式,在content页面中,滑动,可以打开sliding menu //TOUCHMODE_MARGIN 边缘模式,在content页面中,如果想打开slding ,你需要在屏幕边缘滑动才可以打开slding menu //TOUCHMODE_NONE 自然是不能通过手势打开啦 sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); //使用左上方icon可点,这样在onOptionsItemSelected里面才可以监听到R.id.home getActionBar().setDisplayHomeAsUpEnabled(true); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: //toggle就是程序自动判断是打开还是关闭 toggle(); // getSlidingMenu().showMenu();// show menu // getSlidingMenu().showContent();//show content return true; } return super.onOptionsItemSelected(item); } }
MenuFragment.java
public class MenuFragment extends PreferenceFragment implements OnPreferenceClickListener{ int index = -1; @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); //set the preference xml to the content view addPreferencesFromResource(R.xml.menu); //add listener findPreference("a").setOnPreferenceClickListener(this); findPreference("b").setOnPreferenceClickListener(this); findPreference("n").setOnPreferenceClickListener(this); } @Override public boolean onPreferenceClick(Preference preference) { String key = preference.getKey(); if("a".equals(key)) { //if the content view is that we need to show . show directly if(index == 0) { ((MainActivity)getActivity()).getSlidingMenu().toggle(); return true; } //otherwise , replace the content view via a new Content fragment index = 0; FragmentManager fragmentManager = ((MainActivity)getActivity()).getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content, new ContentFragment("This is A Menu")) .commit(); }else if("b".equals(key)) { if(index == 1) { ((MainActivity)getActivity()).getSlidingMenu().toggle(); return true; } index = 1; FragmentManager fragmentManager = ((MainActivity)getActivity()).getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content, new ContentFragment("This is B Menu")) .commit(); }else if("n".equals(key)) { if(index == 2) { ((MainActivity)getActivity()).getSlidingMenu().toggle(); return true; } index = 2; FragmentManager fragmentManager = ((MainActivity)getActivity()).getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content, new ContentFragment("This is N Menu")) .commit(); } //anyway , show the sliding menu ((MainActivity)getActivity()).getSlidingMenu().toggle(); return false; } }
ContentFragment.java
public class ContentFragment extends Fragment { String text = null; public ContentFragment() { } public ContentFragment(String text) { Log.e("Krislq", text); this.text = text; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); Log.e("Krislq", "onCreate:"+text); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.e("Krislq", "onCreateView:"+ text); //inflater the layout View view = inflater.inflate(R.layout.fragment_text, null); TextView textView =(TextView)view.findViewById(R.id.textView); if(!TextUtils.isEmpty(text)) { textView.setText(text); } return view; } }