Android_SlidingMenu开源项目_导航框架实现侧滑
来源:互联网 发布:软件开发自学 编辑:程序博客网 时间:2024/06/01 08:50
本博客Github上的Demo链接:https://github.com/chengbiao1314/android_frame_slidingmenu.git
用SlidingMenu实现侧滑一般分为4步:
1、导入SlidingMenu的开源项目库(menu_library),并关联上项目
2、布局文件写一个menulayout(侧滑部分的布局)和一个homelayout(主页的布局)
3、写一个主Activity继承SlidingFragmentActivity,加载两部分布局文件,并且设置相应的变量(此时侧滑效果已经可以出来)
4、在主Activity中给导航添加事件,实现Fragment之间的切换,并实现几个Fragment
代码分析:
1、导入SlidingMenu的开源项目库(menu_library),并关联上项目
2、布局文件写一个menulayout(侧滑部分的布局)和一个homelayout(主页的布局)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="15dip" android:orientation="vertical" android:background="@drawable/left_back"> <TextView android:id="@+id/sliding_menu_one" android:clickable="true" android:focusable="true" android:onClick="sliding_menu_click" android:layout_width="wrap_content" android:layout_height="100dip" android:layout_marginBottom="30dip" android:textSize="30sp" android:textColor="@android:color/white" android:text="Fragment1" /> <TextView android:id="@+id/sliding_menu_two" android:clickable="true" android:focusable="true" android:onClick="sliding_menu_click" android:layout_width="wrap_content" android:layout_height="100dip" android:layout_marginBottom="30dip" android:textSize="30sp" android:textColor="@android:color/white" android:text="Fragment2" /> <TextView android:id="@+id/sliding_menu_three" android:clickable="true" android:focusable="true" android:onClick="sliding_menu_click" android:layout_width="wrap_content" android:layout_height="100dip" android:layout_marginBottom="30dip" android:textSize="30sp" android:textColor="@android:color/white" android:text="Fragment3" /> </LinearLayout>homelayout
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/content" ></FrameLayout>
3、写一个主Activity继承SlidingFragmentActivity,加载两部分布局文件,并且设置相应的变量(此时侧滑效果已经可以出来)
public class MainActivity extends SlidingFragmentActivity{public static SlidingMenu sm;private FragmentOne myFragmentOne;private FragmentTwo myFragmentTwo;private FragmentThree myFragmentThree;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setBehindContentView(R.layout.sliding_left_framelayout);setContentView(R.layout.sliding_home_framelayout);sm = getSlidingMenu();sm.setMode(SlidingMenu.LEFT);sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);sm.setShadowDrawable(R.drawable.sliding_menu_shadow);sm.setShadowWidthRes(R.dimen.sliding_menu_shadow_width);sm.setBehindWidthRes(R.dimen.sliding_menu_width);if(myFragmentOne == null)myFragmentOne = new FragmentOne();switchFragment(myFragmentOne);}……4、在主Activity中给导航添加事件,实现Fragment之间的切换
public static void mytoggle(){sm.toggle();}public void switchFragment(Fragment fragment) {mytoggle();getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();}public void sliding_menu_click(View view){switch(view.getId()){case R.id.sliding_menu_one:if(myFragmentOne == null)myFragmentOne = new FragmentOne();switchFragment(myFragmentOne);Toast.makeText(this, "have running...", 0).show();break;case R.id.sliding_menu_two:if(myFragmentTwo == null)myFragmentTwo = new FragmentTwo();switchFragment(myFragmentTwo);Toast.makeText(this, "have running...", 0).show();break;case R.id.sliding_menu_three:if(myFragmentThree == null)myFragmentThree = new FragmentThree();switchFragment(myFragmentThree);Toast.makeText(this, "have running...", 0).show();break;}}}
实现Fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@android:color/darker_gray"> <TextView android:layout_width="fill_parent" android:layout_height="50dip" android:layout_marginBottom="20dip" android:gravity="center" android:textSize="30sp" android:textColor="@android:color/white" android:text="Fragment1"/></LinearLayout>
public class FragmentOne extends Fragment {private View view;public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {view = inflater.inflate(R.layout.frag_one, null);return view;}}
PS:当然为了方便代码管理,也可以将开源框架集成到自己的项目中去。只需要将依赖库中的两个src源码包、layout布局文件slidingmenumain.xml、drawable中的sliding_menu_shadow.xml、values中的attrs.xml、dimens.xml和ids.xml拷贝到对应的文件夹即可。
0 0
- Android_SlidingMenu开源项目_导航框架实现侧滑
- Android开源框架ViewPageIndicator和ViewPager实现Tab导航
- Android开源框架ViewPageIndicator和ViewPager实现Tab导航
- Android中使用开源框架PagerSlidingTabStrip实现导航标题
- 【简单项目框架一】Fragment实现的底部导航
- 【简单项目框架一】Fragment实现的底部导航
- Android_SlidingMenu详解
- Android_SlidingMenu练习
- 黑马商城项目_导航部分
- bootstrap实现垂直导航框架
- bootstrap实现垂直导航框架
- bootstrap实现垂直导航框架
- FragmentController实现项目中底部导航栏切换的UI框架
- iOS支持全屏侧滑导航栏(完全自定义)开源项目
- Android两级导航菜单栏的实现--FragmentTabHost结合ViewPager与Android 开源项目PagerSlidingTabStrip
- Android两级导航菜单栏的实现--FragmentTabHost结合ViewPager与Android 开源项目PagerSlidingTabStrip
- 使用框架集实现页面导航功能
- Fragment 搭建框架 实现 导航 切换
- CSS:box-sizing的使用
- SpriteKit与SceneKit
- Hbase shell详情
- 打印1到最大的n位数
- PHP字符串函数之 strstr stristr strchr strrchr
- Android_SlidingMenu开源项目_导航框架实现侧滑
- poj 备忘录
- ACM ProblemB
- USACO-Section 3.2 Feed Ratios(枚举)
- hdu4825
- MVC 与 SSH(个人总结)
- android弹出软键盘,view整体上移问题
- java 的float型数据保存到数据库中变成了整数
- ACM第一次练习—1006G