代码改变世界

Android中slidingmenu的使用

2014-12-29 01:19  一切尽在掌握  阅读(323)  评论(0编辑  收藏  举报
public class MainActivity extends SlidingFragmentActivity {

	private SlidingMenu slidingMenu;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置内容布局
		setContentView(R.layout.content);  
		// 设置侧拉条目布局
		setBehindContentView(R.layout.menu_frame);  
		// 获取侧拉栏目对象
		slidingMenu = getSlidingMenu();
		// 设置哪种方式可以拉出侧栏
		// Controls whether the SlidingMenu can be opened with a swipe gesture.
		// Options are TOUCHMODE_MARGIN, TOUCHMODE_FULLSCREEN, or TOUCHMODE_NONE
		// TOUCHMODE_NONE的应用场景举例:当导航切换到不需要侧拉时设置NONE即可
		// TOUCHMODE_MARGIN拖拽边缘有效
		// TOUCHMODE_FULLSCREEN 全屏拖拽有效
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//		// 设置内容显示页对应的dp大小
		slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);  
//		// 设置左侧侧拉栏目宽度,和设置内容显示页相对应,注意单位为px
//		// slidingMenu.setBehindWidth(140);
//		// 设置侧拉栏目所在的位置  LEFT_RIGHT LEFT
		slidingMenu.setMode(SlidingMenu.LEFT);
//		// 给侧拉栏目和左侧内容页区分开(加线)
		slidingMenu.setShadowDrawable(R.drawable.shadow);
		slidingMenu.setShadowWidthRes(R.dimen.shadow_width);

 使用shape画出分割线shadow.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <gradient
        android:endColor="#5Aff0000"
        android:centerColor="#2Dff0000"
        android:startColor="#00ff0000" />
    <!-- 颜色渐变  由白边  00 为完全透明深色 -->
</shape>

使用fragment创建的view替换布局

        //Fragment在android 3.0才推出,所以应导入suport-v4向下兼容
        MenuFragment menuFragment = new MenuFragment();
        //获取fragment管理器
        getSupportFragmentManager()
        //开启事物
        .beginTransaction()
        //通过id找到布局,fragment创建的view替换指定的布局,这里为setBehindContentView设置的布局中的layout
        //最后一个参数为TAG唯一标识fragment对象
        .replace(R.id.menu, menuFragment, "MENU")
        .commit();

创建右侧拖拽出菜单栏

		//右边的菜单栏
		//基本的两个操作
		slidingMenu.setSecondaryMenu(R.layout.menu_frame_right);
		// 给侧拉栏目和左侧内容页区分开(加线)
		slidingMenu.setSecondaryShadowDrawable(R.drawable.shadow);
		
		RightMenuFragment rightMenuFragment = new RightMenuFragment();
		//获取fragment管理器
		getSupportFragmentManager()
		//开启事物
		.beginTransaction()
		//通过id找到布局,fragment
		.replace(R.id.menu_right, rightMenuFragment, "MENU_RIGHT")
		.commit();

 RightMenuFragment使用ArrayAdapter

package com.example.myslidingmenu_left.fragment;

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

import com.example.myslidingmenu_left.MainActivity;
import com.example.myslidingmenu_left.R;
import com.example.myslidingmenu_left.fragment.Fragment1;
import com.example.myslidingmenu_left.util.LogUtil;

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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class RightMenuFragment extends Fragment {
    
    private String tag = "MenuFragment";
    private View view;

    /**
     * 当前类创建时候调用
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.i(tag , "onCreate");
    }
    
    /**
     * 相当于activity中的setContentView,将布局加载到类中去
     * 布局加载到类中
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        LogUtil.i(tag , "onCreateView");
//        TextView view = new TextView(getActivity());
//        view.setText("这是左侧侧拉条目");
        //用listview填充左侧菜单栏
        view = inflater.inflate(R.layout.list_view, null);
        return view;
    }
    
    /**
     * 数据填充操作在这个方法中,即当布局文件填充去
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        LogUtil.i(tag , "onActivityCreated");
        super.onActivityCreated(savedInstanceState);
        ListView listview = (ListView) view.findViewById(R.id.list_view);
        listview.setAdapter(new ArrayAdapter<String>(
                getActivity(), 
                android.R.layout.simple_list_item_1, //相当于在getView中使用的listview的item
                android.R.id.text1, //相当于item中的一个控件id
                initData())//传递填充当前适配器的一些集合
                );
        
        listview.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                Fragment fragment = null;
                switch (position) {
                case 0:
                    fragment = new Fragment1();
                    break;
                    
                case 1:
                    fragment = new Fragment2();
                    break;
                    
                case 2:
                    fragment = new Fragment3();
                    break;
                    
                case 3:
                    fragment = new Fragment4();
                    break;
                    
                case 4:
                    fragment = new Fragment5();
                    break;

                default:
                    break;
                }
                switchFragment(fragment);
            }
        });
    }


    /**
     * 切换fragment,如果上下文是MainActivity,则去调用他的切换fragment方法
     * @param fragment
     */
    private void switchFragment(Fragment fragment) {
        if(getActivity() instanceof MainActivity){
            //如果上下文是MainActivity
            ((MainActivity)getActivity()).switchFragment(fragment);
        }
        getActivity()
        .getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.content_frame, fragment)
        .commit();
    }
    
    /**
     * 填充适配器数据的集合
     * @return
     */
    private List<String> initData() {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("fragment1");
        arrayList.add("fragment2");
        arrayList.add("fragment3");
        arrayList.add("fragment4");
        arrayList.add("fragment5");
        return arrayList;
    }
}

布局文件R.layout.list_view:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    

</ListView>