• Android——SlidingMenu学习总结


    来源

    SlidingMenu是github上比較火开源库。很强大,不但但是简单的设置实现两側滑动菜单,还能够设置菜单的阴影、渐变色、划动模式等。

    下载地址:https://github.com/jfeinstein10/SlidingMenu


    怎样使用

    要想可以实现SlidingMenu的效果,首先必需要将它作为libary导入到你的project。

    注意slidingMenu_library类库本身自带了android-support-v4.jar文件,而我们自己的project一般也带有这个文件,假设两个文件的SHA-1值不一样,可能会出现

               找不到类的异常。我的做法是删除掉自己项目里面的android-support-v4.jar。




    API演示样例代码

    package com.example.slidingmenu_left;
    
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.Window;
    
    import com.example.slidingmenu_left.fragment.MenuFragment;
    import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
    import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
    
    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();
    		
    		/*
    		 * SlidingMenu.TOUCHMODE_FULLSCREEN  全屏触摸拖拽有效
    		 * SlidingMenu.TOUCHMODE_MARGIN      拖拽边缘有效
    		 * SlidingMenu.TOUCHMODE_NONE        全屏不响应触摸拖拽事件
    		 */
    		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
    		
    		// 设置内容显示页相应的dp大小
    		slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    		
    //		// 设置左側側拉栏目的dp大小
    //		slidingMenu.setBehindWidthRes(140);// 注意这里面140的单位是像素
    		
    		/*
    		 * SlidingMenu.LEFT
    		 * SlidingMenu.RIGHT
    		 * SlidingMenu.LEFT_RIGHT
    		 */
    		// 设置側拉栏目的位置
    		slidingMenu.setMode(SlidingMenu.LEFT);
    		
    		// 给側拉栏目和右側内容页区分开(竖线)
    		slidingMenu.setShadowDrawable(R.drawable.shadow);
    		
    		// 设置(竖线)的宽度
    		slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
    		
    		// fragment去替换布局中节点
    		Fragment fragment = new MenuFragment();
    		getSupportFragmentManager().beginTransaction()// 开启事务
    					   .replace(R.id.menu, fragment, "MENU")//替换布局
    					   .commit();// 提交事务
    		
    	}
    
    	/**
    	 * 当前类中做替换当前显示内容界面的操作
    	 * @author wuseyukui
    	 *
    	 * @param fragment
    	 */
    	public void switchFragment(Fragment fragment) {
    		if (fragment != null) {
    			getSupportFragmentManager().beginTransaction()
    			   			   .replace(R.id.content_frame, fragment, "CONTENT")
    						   .commit();
    						   slidingMenu.toggle();
    		}
    	}
    
    }
    

    package com.example.slidingmenu_left.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.example.slidingmenu_left.MainActivity;
    import com.example.slidingmenu_left.R;
    
    public class MenuFragment extends Fragment {
    	
    	private String tag = "MenuFragment";
    	private View view;
    
    	/**
    	 * 创建对象
    	 */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		Log.i(tag , "=====onCreate");
    	}
    	
    	/**
    	 * 相似:setContentView
    	 * 设置布局
    	 */
    	@Override
    	public View onCreateView(LayoutInflater inflater, ViewGroup container,
    			Bundle savedInstanceState) {
    		Log.i(tag , "=====onCreateView");
    		view = View.inflate(getActivity(), R.layout.list_view, null);
    		return view;
    	}
    	
    	/**
    	 * 数据填充操作
    	 */
    	@Override
    	public void onActivityCreated(Bundle savedInstanceState) {
    		super.onActivityCreated(savedInstanceState);
    		Log.i(tag , "=====onActivityCreated");
    		ListView listView = (ListView)view.findViewById(R.id.list_view);
    		listView.setAdapter(new ArrayAdapter<String>(getActivity(), 
    				android.R.layout.simple_list_item_1, 
    				android.R.id.text1, 
    				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;
    				}
    				
    				swithFragment(fragment);
    			}
    		});
    	}
    	
    	private void swithFragment(Fragment fragment) {
    		if (getActivity() instanceof MainActivity) {
    			((MainActivity)getActivity()).switchFragment(fragment);
    		}
    	}
    
    	private List<String> initData() {
    		ArrayList<String> arrayList = new ArrayList<String>();
    		arrayList.add("栏目1");
    		arrayList.add("栏目2");
    		arrayList.add("栏目3");
    		arrayList.add("栏目4");
    		arrayList.add("栏目5");
    		return arrayList;
    	}
    }
    

    效果图:

    

  • 相关阅读:
    Unable to start adb server: adb server version (32) doesn't match this client (39); killing...
    mysql错误指令:Failed to open file "file_name" error 2/error 22
    爬虫流程概述
    Jupyter Notebook的使用
    markdown语法
    pymysql向表中插入数据
    python创建mysql数据库中的表
    python查询ip地址来源
    Pandas读取csv时设置列名
    程序员面试——位运算
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7190383.html
Copyright © 2020-2023  润新知