Android UI实现SlidingMenu侧滑菜单效果
程序员文章站
2024-02-28 07:59:16
本篇博客给大家分享一个效果比较好的侧滑菜单的demo,实现点击左边菜单切换fragment。
效果如下:
主activity代码:...
本篇博客给大家分享一个效果比较好的侧滑菜单的demo,实现点击左边菜单切换fragment。
效果如下:
主activity代码:
package com.infzm.slidingmenu.demo; import android.os.bundle; import android.support.v4.app.fragment; import android.view.view; import android.view.view.onclicklistener; import android.view.window; import android.widget.imageview; import android.widget.textview; import com.infzm.slidingmenu.demo.fragment.leftfragment; import com.infzm.slidingmenu.demo.fragment.todayfragment; import com.jeremyfeinstein.slidingmenu.lib.slidingmenu; import com.jeremyfeinstein.slidingmenu.lib.app.slidingfragmentactivity; /** * @date 2014/11/14 * @author wuwenjie * @description 主界面 */ public class mainactivity extends slidingfragmentactivity implements onclicklistener { private imageview topbutton; private fragment mcontent; private textview toptextview; @override public void oncreate(bundle savedinstancestate) { requestwindowfeature(window.feature_no_title); // 无标题 super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initslidingmenu(savedinstancestate); topbutton = (imageview) findviewbyid(r.id.topbutton); topbutton.setonclicklistener(this); toptextview = (textview) findviewbyid(r.id.toptv); } /** * 初始化侧边栏 */ private void initslidingmenu(bundle savedinstancestate) { // 如果保存的状态不为空则得到之前保存的fragment,否则实例化myfragment if (savedinstancestate != null) { mcontent = getsupportfragmentmanager().getfragment( savedinstancestate, "mcontent"); } if (mcontent == null) { mcontent = new todayfragment(); } // 设置左侧滑动菜单 setbehindcontentview(r.layout.menu_frame_left); getsupportfragmentmanager().begintransaction() .replace(r.id.menu_frame, new leftfragment()).commit(); // 实例化滑动菜单对象 slidingmenu sm = getslidingmenu(); // 设置可以左右滑动的菜单 sm.setmode(slidingmenu.left); // 设置滑动阴影的宽度 sm.setshadowwidthres(r.dimen.shadow_width); // 设置滑动菜单阴影的图像资源 sm.setshadowdrawable(null); // 设置滑动菜单视图的宽度 sm.setbehindoffsetres(r.dimen.slidingmenu_offset); // 设置渐入渐出效果的值 sm.setfadedegree(0.35f); // 设置触摸屏幕的模式,这里设置为全屏 sm.settouchmodeabove(slidingmenu.touchmode_fullscreen); // 设置下方视图的在滚动时的缩放比例 sm.setbehindscrollscale(0.0f); } @override protected void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); getsupportfragmentmanager().putfragment(outstate, "mcontent", mcontent); } /** * 切换fragment * * @param fragment */ public void switchconent(fragment fragment, string title) { mcontent = fragment; getsupportfragmentmanager().begintransaction() .replace(r.id.content_frame, fragment).commit(); getslidingmenu().showcontent(); toptextview.settext(title); } @override public void onclick(view v) { switch (v.getid()) { case r.id.topbutton: toggle(); break; default: break; } } }
侧边栏菜单fragment
package com.infzm.slidingmenu.demo.fragment; import android.app.activity; import android.os.bundle; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import com.infzm.slidingmenu.demo.mainactivity; import com.infzm.slidingmenu.demo.r; /** * @date 2014/11/14 * @author wuwenjie * @description 侧边栏菜单 */ public class leftfragment extends fragment implements onclicklistener{ private view todayview; private view lastlistview; private view discussview; private view favoritesview; private view commentsview; private view settingsview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public void onattach(activity activity) { super.onattach(activity); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.layout_menu, null); findviews(view); return view; } public void findviews(view view) { todayview = view.findviewbyid(r.id.tvtoday); lastlistview = view.findviewbyid(r.id.tvlastlist); discussview = view.findviewbyid(r.id.tvdiscussmeeting); favoritesview = view.findviewbyid(r.id.tvmyfavorites); commentsview = view.findviewbyid(r.id.tvmycomments); settingsview = view.findviewbyid(r.id.tvmysettings); todayview.setonclicklistener(this); lastlistview.setonclicklistener(this); discussview.setonclicklistener(this); favoritesview.setonclicklistener(this); commentsview.setonclicklistener(this); settingsview.setonclicklistener(this); } @override public void ondestroyview() { super.ondestroyview(); } @override public void ondestroy() { super.ondestroy(); } @override public void onclick(view v) { fragment newcontent = null; string title = null; switch (v.getid()) { case r.id.tvtoday: // 今日 newcontent = new todayfragment(); title = getstring(r.string.today); break; case r.id.tvlastlist:// 往期列表 newcontent = new lastlistfragment(); title = getstring(r.string.lastlist); break; case r.id.tvdiscussmeeting: // 讨论集会 newcontent = new discussfragment(); title = getstring(r.string.discussmeetting); break; case r.id.tvmyfavorites: // 我的收藏 newcontent = new myfavoritesfragment(); title = getstring(r.string.myfavorities); break; case r.id.tvmycomments: // 我的评论 newcontent = new mycommentsfragment(); title = getstring(r.string.mycomments); break; case r.id.tvmysettings: // 设置 newcontent = new mysettingsfragment(); title = getstring(r.string.settings); break; default: break; } if (newcontent != null) { switchfragment(newcontent, title); } } /** * 切换fragment * @param fragment */ private void switchfragment(fragment fragment, string title) { if (getactivity() == null) { return; } if (getactivity() instanceof mainactivity) { mainactivity fca = (mainactivity) getactivity(); fca.switchconent(fragment, title); } } }
上面是核心代码,引入slidingmenu开源库。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 浅析Android手机卫士读取联系人
推荐阅读
-
Android UI实现SlidingMenu侧滑菜单效果
-
Android UI实现SlidingMenu侧滑菜单效果
-
Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码
-
Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码
-
Android使用DrawerLayout实现仿QQ双向侧滑菜单
-
android实现左右侧滑菜单效果
-
Android使用ViewDragHelper实现QQ6.X最新版本侧滑界面效果实例代码
-
Android使用DrawerLayout实现仿QQ双向侧滑菜单
-
Android开源AndroidSideMenu实现抽屉和侧滑菜单
-
Android自定义View 仿QQ侧滑菜单的实现代码