zoukankan      html  css  js  c++  java
  • 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

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Java基础-学习笔记(十)——内部类(嵌套类)
    Java基础-学习笔记(九)——static关键字
    Java基础-学习笔记(八)——函数的参数传递
    JS获取当前时间转成时间戳,并比较两个时间戳得出相差的天数
    tp6的with关联使用(删查),insertAll批量增加
    Vue父子组件的相互调用方法与参数
    Vue父子组件之间的通信
    Vue组价的基本使用
    PHP接入图片文字识别AIP
    EC6模块化的导入和导出
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4612250.html
Copyright © 2011-2022 走看看