博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
slidingmenu + fragment 左右菜单滑动
阅读量:6115 次
发布时间:2019-06-21

本文共 11230 字,大约阅读时间需要 37 分钟。

                      

content_frame.xml

1 
2

 

左侧  menu_frame.xml

1 
2

 

右侧  menu_frame_right.xml

1 
2

 

1 package com.cn.slidingmenu.fragment;  2   3 import com.actionbarsherlock.view.MenuItem;  4 import com.cn.slidingmenu.fragment.R;  5 import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;  6 import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.CanvasTransformer;  7 import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;  8   9 import android.graphics.Canvas; 10 import android.os.Bundle; 11 import android.support.v4.app.Fragment; 12 import android.view.animation.Interpolator; 13  14 /** 15  * slidingmenu + fragment 16  *  17  * @author dr 18  */ 19 public class MainActivity extends SlidingFragmentActivity { 20  21     private SlidingMenu sm; 22     private Fragment contentFragment; 23     private Fragment leftFragment; 24  25     @Override 26     public void onCreate(Bundle savedInstanceState) { 27         super.onCreate(savedInstanceState); 28         setContentView(R.layout.content_frame); 29  30         // 设置SlidingMenu的layout 31         setBehindContentView(R.layout.menu_frame); 32  33         if (savedInstanceState == null) { 34             contentFragment = new Fragment1(); 35         } else { 36             // 取出之前保存的contentFragment 37             contentFragment = this.getSupportFragmentManager().getFragment( 38                     savedInstanceState, "contentFragment"); 39         } 40         // 设置当前的fragment 41         // getSupportFragmentManager JAR包当中的 42         this.getSupportFragmentManager().beginTransaction() 43                 .replace(R.id.content_frame, contentFragment).commit(); 44  45         /** 设置SlidingMenu属性和layout */ 46         // 得到SlidingMenu的对象 47         sm = getSlidingMenu(); 48         // 设置slidingmenu滑动的方式 49         sm.setMode(SlidingMenu.LEFT_RIGHT); 50         // 设置slidingmenu边界的阴影图片 51         sm.setShadowDrawable(R.drawable.shadow); 52         // 设置阴影的宽度 53         sm.setShadowWidthRes(R.dimen.shadow_width); 54         // 设置slidingmenu宽度 55         sm.setBehindOffsetRes(R.dimen.slidingmenu_offset); 56         // 设置slidingmenu宽度 57         // sm.setBehindWidth(400); 58         // 设置滑出slidingmenu范围 59         sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); 60         sm.setMenu(R.layout.menu_frame); 61         // 设置slidingmenu动画 62         sm.setBehindCanvasTransformer(null); 63  64         // 设置menu的fragment 65         leftFragment = new LeftFragment(); 66         this.getSupportFragmentManager().beginTransaction() 67                 .replace(R.id.menu_frame, leftFragment).commit(); 68  69         // 设置右侧的slidingmenu 70         sm.setSecondaryMenu(R.layout.menu_frame_right); 71         sm.setSecondaryShadowDrawable(R.drawable.shadowright); 72  73         // 设置右侧滑出的slidingmenu 74         RightFragment rightFragment = new RightFragment(); 75         this.getSupportFragmentManager().beginTransaction() 76                 .replace(R.id.menu_frame_right, rightFragment).commit(); 77  78         // 设置在slidingmenu页显示ActionBar 79         setSlidingActionBarEnabled(false); 80         // 是ActionBar的图标可以被点击 81         getSupportActionBar().setHomeButtonEnabled(true); 82         // 启用向左的图标 83         getSupportActionBar().setDisplayHomeAsUpEnabled(true); 84  85     } 86  87     @Override 88     public boolean onMenuItemSelected(int featureId, MenuItem item) { 89         switch (item.getItemId()) { 90         case android.R.id.home: 91             // 动态打开或关闭slidingmenu 92             sm.toggle(); 93             break; 94         default: 95             break; 96         } 97         return true; 98     } 99 100     @Override101     protected void onSaveInstanceState(Bundle outState) {102         super.onSaveInstanceState(outState);103         // 1.bundle104         // 2.存放的ID105         // 3.当前要保存的fragment的实例106         this.getSupportFragmentManager().putFragment(outState,107                 "contentFragment", contentFragment);108     }109 110     /**111      * 切换fragment112      * 113      * @param f114      */115     public void switchFragment(Fragment f) {116         contentFragment = f;117         this.getSupportFragmentManager().beginTransaction()118                 .replace(R.id.content_frame, contentFragment).commit();119         sm.toggle();120     }121 122     /**123      * 缩放动画124      */125     private CanvasTransformer canvasTransformer = new CanvasTransformer() {126 127         @Override128         public void transformCanvas(Canvas canvas, float percentOpen) {129             float scale = (float) (percentOpen * 0.25 + 0.75);130             canvas.scale(scale, scale, canvas.getWidth() / 2,131                     canvas.getHeight() / 2);132         }133 134     };135     private static Interpolator interp = new Interpolator() {136         @Override137         public float getInterpolation(float t) {138             t -= 1.0f;139             return t * t * t + 1.0f;140         }141     };142 143     /**144      * 从下网上动画145      */146     private CanvasTransformer canvasTransformer2 = new CanvasTransformer() {147 148         @Override149         public void transformCanvas(Canvas canvas, float percentOpen) {150             canvas.translate(151                     0,152                     canvas.getHeight()153                             * (1 - interp.getInterpolation(percentOpen)));154         }155 156     };157 158     /**159      * 拉伸动画160      */161     private CanvasTransformer canvasTransformer3 = new CanvasTransformer() {162         @Override163         public void transformCanvas(Canvas canvas, float percentOpen) {164             canvas.scale(percentOpen, 1, 0, 0);165 166         }167     };168 169 }

 

1 package com.cn.slidingmenu.fragment; 2  3 import com.cn.slidingmenu.fragment.R; 4 import android.os.Bundle; 5 import android.support.v4.app.Fragment; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.TextView;10 11 public class Fragment1 extends Fragment {12 13     @Override14     public void onCreate(Bundle savedInstanceState) {15         super.onCreate(savedInstanceState);16     }17     @Override18     public View onCreateView(LayoutInflater inflater, ViewGroup container,19             Bundle savedInstanceState) {20         //设置fragment的layout21         View view = inflater.inflate(R.layout.text, null);22         TextView tv = (TextView) view.findViewById(R.id.test);23         tv.setText(Fragment1.class.getSimpleName());24         return view;25     }26     @Override27     public void onActivityCreated(Bundle savedInstanceState) {28         super.onActivityCreated(savedInstanceState);29     }30     31 }

 

1 package com.cn.slidingmenu.fragment; 2  3 import java.util.ArrayList; 4 import java.util.List; 5 import com.cn.slidingmenu.fragment.R; 6 import android.os.Bundle; 7 import android.support.v4.app.Fragment; 8 import android.view.LayoutInflater; 9 import android.view.View;10 import android.view.ViewGroup;11 import android.widget.AdapterView;12 import android.widget.AdapterView.OnItemClickListener;13 import android.widget.ArrayAdapter;14 import android.widget.ListView;15 16 public class LeftFragment extends Fragment implements OnItemClickListener {17 18     private View rootView;19 20     @Override21     public void onCreate(Bundle savedInstanceState) {22         super.onCreate(savedInstanceState);23     }24 25     @Override26     public View onCreateView(LayoutInflater inflater, ViewGroup container,27             Bundle savedInstanceState) {28         // 设置fragment的layout29         rootView = inflater.inflate(R.layout.activity_main, null);30         return rootView;31     }32 33     @Override34     public void onActivityCreated(Bundle savedInstanceState) {35         super.onActivityCreated(savedInstanceState);36         ListView lv = (ListView) rootView.findViewById(R.id.listview);37 38         List
list = initData();39 40 ArrayAdapter
adapter = new ArrayAdapter
(getActivity(),41 android.R.layout.simple_list_item_1, android.R.id.text1, list);42 43 lv.setAdapter(adapter);44 lv.setOnItemClickListener(this);45 }46 47 /**48 * 初始化数据49 * @return50 */51 public List
initData() {52 List
list = new ArrayList
();53 list.add(Fragment1.class.getSimpleName());54 list.add(Fragment2.class.getSimpleName());55 list.add(Fragment3.class.getSimpleName());56 list.add(Fragment4.class.getSimpleName());57 list.add(Fragment5.class.getSimpleName());58 return list;59 }60 61 @Override62 public void onItemClick(AdapterView
parent, View view, int position,63 long id) {64 Fragment f = null;65 switch (position) {66 case 0:67 f = new Fragment1();68 break;69 case 1:70 f = new Fragment2();71 break;72 case 2:73 f = new Fragment3();74 break;75 case 3:76 f = new Fragment4();77 break;78 case 4:79 f = new Fragment5();80 break;81 default:82 f = new Fragment1();83 break;84 }85 switchFragment(f);86 }87 88 /**89 * listview item 点击时切换fragment90 * @param f91 */92 private void switchFragment(Fragment f) {93 if (getActivity() instanceof MainActivity) {94 MainActivity activity = (MainActivity) getActivity();95 activity.switchFragment(f);96 }97 }98 99 }

 

1 package com.cn.slidingmenu.fragment; 2  3 import java.util.ArrayList; 4 import java.util.List; 5 import android.os.Bundle; 6 import android.support.v4.app.Fragment; 7 import android.view.LayoutInflater; 8 import android.view.View; 9 import android.view.ViewGroup;10 import android.widget.ArrayAdapter;11 import android.widget.ListView;12 13 public class RightFragment extends Fragment {14 15     private View rootView;16     17     @Override18     public void onCreate(Bundle savedInstanceState) {19         super.onCreate(savedInstanceState);20     }21     22     @Override23     public View onCreateView(LayoutInflater inflater, ViewGroup container,24             Bundle savedInstanceState) {25         rootView = inflater.inflate(R.layout.activity_main, null);26         return rootView;27     }28     @Override29     public void onActivityCreated(Bundle savedInstanceState) {30         super.onActivityCreated(savedInstanceState);31         ListView lv = (ListView) rootView.findViewById(R.id.listview);32         List
list = initData();33 ArrayAdapter
adapter = new ArrayAdapter
(34 getActivity(), 35 android.R.layout.simple_list_item_1, 36 android.R.id.text1, 37 list);38 39 lv.setAdapter(adapter);40 }41 42 /**43 * 初始化数据44 * @return45 */46 public List
initData() {47 List
list = new ArrayList
();48 list.add(Fragment1.class.getSimpleName());49 list.add(Fragment2.class.getSimpleName());50 list.add(Fragment3.class.getSimpleName());51 list.add(Fragment4.class.getSimpleName());52 list.add(Fragment5.class.getSimpleName());53 return list;54 }55 56 }

 

DEMO下载地址:http://pan.baidu.com/s/1o6v7iX0

文件中三个包,  SlidingMenuFragment21引用library,library引用actionbarsherlock。

 

转载地址:http://zavka.baihongyu.com/

你可能感兴趣的文章
12.通过微信小程序端访问企查查(采集工商信息)
查看>>
WinXp 开机登录密码
查看>>
POJ 1001 Exponentiation
查看>>
HDU 4377 Sub Sequence[串构造]
查看>>
云时代架构阅读笔记之四
查看>>
WEB请求处理一:浏览器请求发起处理
查看>>
Lua学习笔记(8): 元表
查看>>
PHP经典算法题
查看>>
LeetCode 404 Sum of Left Leaves
查看>>
醋泡大蒜有什么功效
查看>>
hdu 5115(2014北京—dp)
查看>>
数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)...
查看>>
PHP读取日志里数据方法理解
查看>>
第五十七篇、AVAssetReader和AVAssetWrite 对视频进行编码
查看>>
Vivado增量式编译
查看>>
一个很好的幻灯片效果的jquery插件--kinMaxShow
查看>>
微信支付签名配置正确,但返回-1,调不出支付界面(有的手机能调起,有的不能)...
查看>>
第二周例行报告
查看>>
多线程条件
查看>>
黄聪:VMware安装Ubuntu10.10【图解】转
查看>>