滑动菜单

滑动菜单


package com.heima.news;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Menu;
import android.view.Window;

import com.heima.news.fragment.Fragment1;
import com.heima.news.fragment.MenuFragment;
import com.heima.news.fragment.RightMenuFragment;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity {
    private SlidingMenu sm;
	/**
     * 1 得到滑动菜单
     * 2 设置滑动菜单是在左边出来还是右边出来
     * 3 设置滑动菜单出来之后,内容页,显示的剩余宽度
     * 4 设置滑动菜单的阴影 设置阴影,阴影需要在开始的时候,特别暗,慢慢的变淡
     * 5 设置阴影的宽度
     * 6 设置滑动菜单的范围
     */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setBehindContentView(R.layout.menu);
		setContentView(R.layout.content);
		
		Fragment fragment1 = new Fragment1();
		getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment1).commit();
		
		
		sm = getSlidingMenu();
		//2 设置滑动菜单是在左边出来还是右边出来
		//参数可以设置左边LEFT,也可以设置右边RIGHT ,还能设置左右LEFT_RIGHT
		sm.setMode(SlidingMenu.LEFT_RIGHT);
		//3 设置滑动菜单出来之后,内容页,显示的剩余宽度
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		//4 设置滑动菜单的阴影 设置阴影,阴影需要在开始的时候,特别暗,慢慢的变淡
		sm.setShadowDrawable(R.drawable.shadow);
		//5 设置阴影的宽度
		sm.setShadowWidth(R.dimen.shadow_width);
		//6 设置滑动菜单的范围
		// 第一个参数 SlidingMenu.TOUCHMODE_FULLSCREEN 可以全屏滑动
		// 第二个参数 SlidingMenu.TOUCHMODE_MARGIN 只能在边沿滑动
		// 第三个参数 SlidingMenu.TOUCHMODE_NONE 不能滑动
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		
		//创建fragment
		MenuFragment menuFragment = new MenuFragment();
		//获取fragment的管理者
		getSupportFragmentManager()
		//开启事物
		.beginTransaction()
		//替换
		.replace(R.id.menu_frame, menuFragment, "Menu")
		//提交
		.commit();
		
		
		/**
		 * 让右侧也能划出菜单。
		 */
		sm.setSecondaryMenu(R.layout.right_menu);
		sm.setSecondaryShadowDrawable(R.drawable.shadowright);
		RightMenuFragment rightMenuFragment = new RightMenuFragment();
		getSupportFragmentManager().beginTransaction().replace(R.id.right_menu_frame, rightMenuFragment).commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
    /**
     *方法D
     *回调
     */
	public void switchFragment(Fragment f){
		getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, f).commit();
		//自动切换
		sm.toggle();
	}
}
package com.heima.news.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.heima.news.MainActivity;
import com.heima.news.R;

public class MenuFragment extends Fragment implements OnItemClickListener {

	private View view;
	private String tag = "MenuFragment";

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		Log.i(tag, "onActivityCreated");
		/**
		 * 一个listView布局文件
		 * 在onCreateView方法由填充器加载进来
		 */
		ListView list_view = (ListView) view.findViewById(R.id.list_view);
		//listView的适配器
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
				android.R.layout.simple_list_item_1, android.R.id.text1,
				initData());
		list_view.setAdapter(adapter);
		list_view.setOnItemClickListener(this);
	}

	private List<String> initData() {
		List<String> list = new ArrayList<String>();
		list.add("fragment1");
		list.add("fragment2");
		list.add("fragment3");
		list.add("fragment4");
		list.add("fragment5");
		return list;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i(tag, "onCreate");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.i(tag, "onCreateView");
		view = LayoutInflater.from(getActivity()).inflate(R.layout.list_view,
				null);
		return view;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Fragment f  = null;
		switch (position) {
		case 0:
			f  =new Fragment1();
			break;
			
		case 1:
			f  =new Fragment2();
			break;
		case 2:
			f  =new Fragment3();
			break;
		case 3:
			f  =new Fragment4();
			break;
		case 4:
			f  =new Fragment5();
			break;
		}
        switchFragment(f);
	}

	private void switchFragment(Fragment f) {
		// TODO Auto-generated method stub
		if(f != null){
			if(getActivity() instanceof MainActivity){
				((MainActivity)getActivity()).switchFragment(f);
			}
		}
	}

}
frame1,其他页面都是一样的。


package com.heima.news.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Fragment1 extends Fragment {

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		TextView textview = new TextView(getActivity());
		textview.setText(Fragment1.class.getSimpleName());
		return textview;
	}

}

需要加载两个开源的组件。。


你可能感兴趣的:(滑动菜单)