分类目录归档:Android

Android SlidingMenu Fragment侧滑菜单实现

本文主要参考自 http://my.eoe.cn/kris/archive/1605.html, 修正了文章中的一些错误。

https://github.com/JakeWharton/ActionBarSherlock
https://github.com/jfeinstein10/SlidingMenu

基于以上第三方侧滑控件。

第三方库支持

1、
把ActionBarSherlock.zip包内的library引到eclipse中
把SlidingMenu.zip包内的library和example引到eclipse中。
把ActionBarSherlock和library设置为lib项目。
在library中,引入ActionBarSherlock库项目。删除library项目的libs中的“android-support-v4.jar”文件。设置Android Build Target为Google API (4.0).
在example中,引入ActionBarSherlock、library库项目,删除example项目的libs中的“android-support-v4.jar”文件。设置Android Build Target为Android 4.0.
2、
修改library项目中的。以下文件。
SlidingActivity.java、SlidingFragmentActivity.java、 SlidingPreferenceActivity.java
修改前内容
    SlidingActivity extends Activity
    SlidingFragmentActivity extends FragmentActivity
    SlidingPreferenceActivity extends PreferenceActivity

修改后内容
    SlidingActivity extends SherlockActivity
    SlidingFragmentActivity extends SherlockFragmentActivity
    SlidingPreferenceActivity extends SherlockPreferenceActivity

代码
MainActivity.java

package com.stamhe.slidingmenudemo;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuInflater;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;

import android.os.Bundle;
import android.app.FragmentTransaction;
//import android.view.Menu;
//import android.view.MenuItem;

//public class MainActivity extends Activity {
public class MainActivity extends SlidingActivity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("SlidingMenu Demo");
//		setContentView(R.layout.activity_main);
		setContentView(R.layout.frame_content);
		
		// set the behind view
		setBehindContentView(R.layout.frame_menu);	// 设置SlidingMenu的布局
		FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
		MenuFragment menuFragment = new MenuFragment();
		fragmentTransaction.replace(R.id.menu, menuFragment);
		fragmentTransaction.replace(R.id.content, new ContentFragment("Welcome"), "Welcome");
		fragmentTransaction.commit();
		
		// customize the SlidingMenu
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidth(50);
		sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffset(360);	// 设置SlidingMenu打开后,右边留下的宽度
		sm.setFadeDegree(0.35f);
		// 设置sliding menu的几种手势模式
		// TOUCHMODE_FULLSCREEN 全屏模式,在正文布局中通过手势也可以打开SlidingMenu
		// TOUCHMODE_MARGIN 边缘模式,如果想打开sliding,你需要在屏幕边缘滑动才可以打开sliding menu
		// TOUCHMODE_NONE 自然是不能通过手势打开啦
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		
		// 使用左上方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.
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case android.R.id.home:
			// toggle就是SlidingMenu自动判断是打开还是关闭
			toggle();
//			getSlidingMenu().showMenu();	// show menu
//			getSlidingMenu().showContent();	// show content
			return true;
		}
		
		return super.onOptionsItemSelected(item);
	}

}

MenuFragment.java


package com.stamhe.slidingmenudemo;

import android.app.FragmentManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.Preference.OnPreferenceClickListener;
import android.util.Log;

public class MenuFragment extends PreferenceFragment implements OnPreferenceClickListener {
	int index = -1;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.menu);
		setRetainInstance(true);
		findPreference("a").setOnPreferenceClickListener(this);
		findPreference("b").setOnPreferenceClickListener(this);
		findPreference("n").setOnPreferenceClickListener(this);
	}
	
	@Override
	public boolean onPreferenceClick(Preference preference) {
		// TODO Auto-generated method stub
		String key = preference.getKey();
		Log.i("stamhe", "key = " + key + " index = " + String.valueOf(index));
		if("a".equals(key)){
			if(index == 0){
				((MainActivity)getActivity()).getSlidingMenu().toggle();
				return true;
			}
			
			index = 0;
			FragmentManager fragmentManager = ((MainActivity)getActivity()).getFragmentManager();
			ContentFragment contentFragment = (ContentFragment)fragmentManager.findFragmentByTag("A");
			fragmentManager.beginTransaction().replace(R.id.content, contentFragment == null ? new ContentFragment("This is A Menu") : contentFragment, "A").commit();
		} else if("b".equals(key)){
			if(index == 1){
				((MainActivity)getActivity()).getSlidingMenu().toggle();
				return true;
			}
			
			index = 1;
			FragmentManager fragmentManager = ((MainActivity)getActivity()).getFragmentManager();
			ContentFragment contentFragment = (ContentFragment)fragmentManager.findFragmentByTag("B");
			fragmentManager.beginTransaction().replace(R.id.content, contentFragment == null ? new ContentFragment("This is B Menu") : contentFragment, "B").commit();
		} else if("n".equals(key)){
			if(index == 2){
				((MainActivity)getActivity()).getSlidingMenu().toggle();
				return true;
			}
			
			index = 2;
			FragmentManager fragmentManager = ((MainActivity)getActivity()).getFragmentManager();
			ContentFragment contentFragment = (ContentFragment)fragmentManager.findFragmentByTag("C");
			fragmentManager.beginTransaction().replace(R.id.content, contentFragment == null ? new ContentFragment("This is C Menu") : contentFragment, "C").commit();
		}
		
		((MainActivity)getActivity()).getSlidingMenu().toggle();
		return false;
	}

}

ContentFragment.java

package com.stamhe.slidingmenudemo;

import android.app.Fragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ContentFragment extends Fragment {
	String text = "";
	public ContentFragment(){
	}
	
	public ContentFragment(String text){
		Log.i("stamhe", text);
		this.text = text;
	}
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
		Log.i("stamhe", "onCreate: " + text);
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		Log.i("stamhe", "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;
	}
	
}

资源文件
res/drawable/shadow.xml




    


res/layout/activity_main.xml



    



res/layout/fragment_text.xml



    
    


res/layout/frame_content.xml



    


res/layout/frame_menu.xml



    


res/menu/main.xml



    


res/xml/menu.xml