Android中使用fragment+slidingmenu实现侧滑

罪过啊,罪过,很久都没更新博客了,今天刚好有时间,然后现在时下流行很多应用都有侧滑功能,不用说,这就是今天的主题,fragment这也是本人第一次使用这个东西,原来一直在2.3的环境下开发,表示相当压抑,一直没时间研究新特征,表示很无奈,不过以后会有新的东西奉献给大家,在大家继续之前,如果有不会使用的fragment的童鞋,先建议看下面一篇文章,里面讲的很详细,所以我这里就不罗嗦了,哈哈

http://blog.csdn.net/aomandeshangxiao/article/details/7671533

下面就开始正餐了,在这以前也参考一些大牛的神笔,比如eoe超版Kris大神,这里也不废话了,先看效果图:

device-2013-06-26-182128

device-2013-06-26-183239

下面就详细分析整个实现过程:

1、 主界面由两个fragment构成:Content+Menu,我们看看Content的代码:

01 public final String tag = this.getClass().getName();
02 public String text = null;
03
04 public ContentFragment(String text) {
05 Log.e(tag, text);
06 this.text = text;
07 }
08
09 @Override
10 public void onCreate(Bundle savedInstanceState) {
11 super.onCreate(savedInstanceState);
12 setRetainInstance(true);
13 Log.e(tag, "onCreate:" + text);
14 }
15
16 @Override
17 public View onCreateView(LayoutInflater inflater, ViewGroup container,
18 Bundle savedInstanceState) {
19 Log.e(tag, "onCreateView:" + text);
20 // inflater the layout
21 View view = inflater.inflate(R.layout.content_layout, null);
22 TextView textView = (TextView) view.findViewById(R.id.content_tv);
23 if (!TextUtils.isEmpty(text)) {
24 textView.setText(text);
25 }
26 return view;
27 }

Content 里面只有一个Textview用来显示内容,我需要在Activity里面告诉它需要显示神马。

我们再来看看Menu里面的代码:

01 public final String tag = this.getClass().getName();
02 public ListView menu_lv = null;
03 public MenuAdapter menuAdapter = null;
04 public Context mContext = null;
05
06 public MenuFragment(Context mContext) {
07 this.mContext = mContext;
08 }
09
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 // TODO Auto-generated method stub
13 super.onCreate(savedInstanceState);
14 }
15
16 @Override
17 public View onCreateView(LayoutInflater inflater, ViewGroup container,
18 Bundle savedInstanceState) {
19 // TODO Auto-generated method stub
20 View menuView = inflater.inflate(R.layout.menu_layout, null);
21 menu_lv = (ListView) menuView.findViewById(R.id.menu_lv);
22 menuAdapter = new MenuAdapter(mContext);
23 menu_lv.setAdapter(menuAdapter);
24 menu_lv.setOnItemClickListener(itemListener);
25 return menuView;
26 }
27
28 public OnItemClickListener itemListener = new OnItemClickListener() {
29
30 @Override
31 public void onItemClick(AdapterView<?> arg0, View arg1, int position,
32 long arg3) {
33 // TODO Auto-generated method stub
34 ((MainActivity)mContext).getSlidingMenu().toggle();
35 Toast.makeText(mContext, menuAdapter.menuStr[position], Toast.LENGTH_SHORT).show();
36 FragmentTransaction ft = ((MainActivity)mContext).getFragmentManager().beginTransaction();
37 ContentFragment cf = new ContentFragment(menuAdapter.menuStr[position]);
38 ft.replace(R.id.main_rl, cf);
39 ft.commit();
40 }
41 };

这里我有个listview,在listview监听里面我拿到的Main的fragment事物管理,然后处理Content进行显示内容的变更。

下面看看listview 配置的 Adapter代码:

01 public String[] menuStr = { "好友推荐", "关于我", "意见反馈", "版本更新" };
02 public int[] viewId = { R.drawable.recommendfriend, R.drawable.about,
03 R.drawable.suggestion, R.drawable.refurbish };
04 public LayoutInflater inflater = null;
05
06 public MenuAdapter(Context context) {
07 inflater = LayoutInflater.from(context);
08 }
09
10 @Override
11 public int getCount() {
12 // TODO Auto-generated method stub
13 return menuStr.length;
14 }
15
16 @Override
17 public Object getItem(int position) {
18 // TODO Auto-generated method stub
19 return menuStr[position];
20 }
21
22 @Override
23 public long getItemId(int position) {
24 // TODO Auto-generated method stub
25 return position;
26 }
27
28 class Holder {
29 ImageView items_iv;
30 TextView items_tv;
31 }
32
33 @Override
34 public View getView(int position, View convertView, ViewGroup parent) {
35 // TODO Auto-generated method stub
36 Holder holder = null;
37 if (convertView == null) {
38 convertView = inflater.inflate(R.layout.menu_items, null);
39 holder = new Holder();
40 holder.items_iv = (ImageView) convertView
41 .findViewById(R.id.items_iv);
42 holder.items_tv = (TextView) convertView
43 .findViewById(R.id.items_tv);
44 convertView.setTag(holder);
45 } else {
46 holder = (Holder) convertView.getTag();
47 }
48 holder.items_iv.setBackgroundResource(viewId[position]);
49 holder.items_tv.setText(menuStr[position]);
50 return convertView;
51 }
52 //释放
53 public void exit() {
54 menuStr = null;
55 inflater = null;
56 }

这里就是Menu显示的内容。

下面来看看Activity里面代码:

01 public FragmentTransaction fraTra = null;
02 public MenuFragment menuFra = null;
03 public ContentFragment contentFra = null;
04
05 @Override
06 public void onCreate(Bundle savedInstanceState) {
07 super.onCreate(savedInstanceState);
08 setContentView(R.layout.activity_main);
09 setBehindContentView(R.layout.menu_layout); // 设置菜单页
10 fraTra = this.getFragmentManager().beginTransaction();
11 menuFra = new MenuFragment(this);
12 contentFra = new ContentFragment("I'm John");
13 fraTra.replace(R.id.menu_fl, menuFra);
14 fraTra.replace(R.id.main_rl, contentFra);
15 fraTra.commit();
16 //
17 SlidingMenu sm = getSlidingMenu(); //滑动菜单
18 sm.setShadowWidth(15); // 阴影宽度
19 sm.setBehindOffset(300); // 菜单与边框的距离
20 sm.setShadowDrawable(R.drawable.shadow); // 滑动菜单渐变
21 sm.setFadeDegree(0.35f); //色度
22 sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN); // 边缘滑动菜单
23 //
24 getActionBar().setDisplayHomeAsUpEnabled(true); //ActionBar(左上角)是否可以点击
25 }
26
27 @Override
28 public boolean onCreateOptionsMenu(Menu menu) {
29 // Inflate the menu; this adds items to the action bar if it is present.
30 getMenuInflater().inflate(R.menu.activity_main, menu);
31 return true;
32 }
33
34 @Override
35 public boolean onOptionsItemSelected(MenuItem item) {
36 switch (item.getItemId()) {
37 case android.R.id.home:
38 //判断是菜单打开还是关闭
39 toggle();
40 return true;
41 }
42 return super.onOptionsItemSelected(item);
43 }

这里主要是Activity对Menu和Content进行了集中处理、设置侧滑菜单的相关属性。

相信大家看到这里都明白了吧。很简单的一个小例子,但是很实用,时间不早了今天就说到这里,有啥疑问,给我留言,谢谢。

差点忘了,附上源码:

http://www.eoeandroid.com/thread-288729-1-1.html

由于gitHub经常访问不了,先暂时放到eoe上面。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值