Android开源项目SlidingMenu的学习笔记(二)

在前面已经介绍了SlidingMenu的用法:Android开源项目SlidingMenu的学习笔记(一),接下来再深入学习下,根据滑出项的Menu切换到对应的页面

目录结构:




点击Bluetooth可以切换到相应的界面


关键代码

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.BluetoothFragment;
import com.dzt.slidingmenudemo.fragment.DisplayFragment;
import com.dzt.slidingmenudemo.fragment.HomeFragment;
import com.dzt.slidingmenudemo.fragment.MenuFragment;
import com.dzt.slidingmenudemo.fragment.WifiFragment;
import com.dzt.slidingmenudemo.fragment.MenuFragment.OnMenuListOnItemClickListener;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingActivity;

/**
 * 根据不同的Menu切换到相应的Fragment
 * 
 * @author Administrator
 * 
 */
public class MainActivity extends SlidingActivity implements
		OnMenuListOnItemClickListener {

	private SlidingMenu mChannelMenu;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 设置标题栏的标题
		setTitle("测试SlidingMenu");
		setContentView(R.layout.content_frame);
		initChannelMenu();
	}

	private void initChannelMenu() {

		setBehindContentView(R.layout.menu_frame);
		// 创建SlidingMenu对象
		mChannelMenu = getSlidingMenu();
		// 设置侧滑栏菜单位置,这里在左边。拉动菜单时,会从左边弹出
		mChannelMenu.setMode(SlidingMenu.LEFT);
		// 设置触摸的范围,这里设置全屏
		mChannelMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// 设置阴影的宽度,查看上面第二张效果图,靠右边的位置,有一个阴影过渡。就是这个东西
		mChannelMenu.setShadowWidthRes(R.dimen.shadow_width);
		// 这里是阴影效果,可以设置图片或者一个颜色过渡
		mChannelMenu.setShadowDrawable(R.drawable.shadow);
		// 设置后面间距,侧滑栏和原来界面间距
		mChannelMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		// 边框的角度,这里指边界地方
		mChannelMenu.setFadeDegree(0.35f);
		// 设置触摸屏幕的模式
		mChannelMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// 设置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();
	}

	@Override
	public void onBackPressed() {
		if (mChannelMenu.isMenuShowing()) {
			// 隐藏SlidingMenu,这里的Content就是我们的主Activity
			mChannelMenu.showContent();
		} else {
			super.onBackPressed();
		}
	}

	@Override
	public void onSelectItem(int position, String title) {
		// TODO Auto-generated method stub
		System.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 BluetoothFragment();
			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 drawer
			setTitle(title);
			mChannelMenu.showContent();
		} else {
			// error in creating fragment
			Log.e("MainActivity", "Error in creating fragment");
		}
	}

}
滑出的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<MenuItem> 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<MenuItem>();
		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);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		mLv.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);
	}
}
点击Menu后,在MainActivity中实现了点击的监听器,就会切换到相应的页面

相关代码:http://download.csdn.net/detail/deng0zhaotai/7862727

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。