• Android开发 侧边滑动菜单栏SlidingMenu结合Fragment


    SlidingMenu是一个开源项目, https://github.com/jfeinstein10/SlidingMenu 。功能是创建侧边滑动菜单栏,效果类似人人Android客户端,可点击按钮或是在屏幕上划动来展开,收缩侧边的菜单。下面是Demo效果图:
    device-2013-06-25-143031
    简单介绍一下:整个demo由三个Fragment,一个Activity组成,菜单一个Fragment,包含两个按钮,在内容区域切换显示FragmentA和FragmentB.
    项目用了AndroidAnnotations框架。
    MainActivity:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    package com.pocketdigi.slidingmenudemo;
     
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentActivity;
     
    import com.googlecode.androidannotations.annotations.AfterViews;
    import com.googlecode.androidannotations.annotations.EActivity;
    import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
    import com.pocketdigi.fragment.FragmentA_;
    import com.pocketdigi.fragment.FragmentB_;
    import com.pocketdigi.fragment.MenuFragment_;
     
     
    @EActivity(R.layout.layout_content)
    public class MainActivity extends FragmentActivity {
    Fragment fragmentA, fragmentB, menuFragment;
    SlidingMenu menu;
     
    @AfterViews
    public void afterViews() {
    //添加左侧菜单
    menuFragment = new MenuFragment_();
    menu = new SlidingMenu(this);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setShadowDrawable(R.drawable.shadow);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    //设置拉出菜单后,上层内容留下的宽度,即这个宽度+菜单宽度=屏幕宽度
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    //设置隐藏或显示菜单时,菜单渐变值,0,不变,1黑色,值为0-1
    menu.setFadeDegree(0f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
    menu.setMenu(R.layout.layout_menu);
     
    getSupportFragmentManager().beginTransaction()
    .replace(R.id.layout_menu, menuFragment).commit();
     
    //显示FragmentA
    showFragmentA();
    }
     
    /**
     * 捕捉返回键,如果当前显示菜单,刚隐藏
     */
    @Override
    public void onBackPressed() {
    if (menu.isMenuShowing()) {
    menu.showContent();
    } else {
    super.onBackPressed();
    }
    }
     
    public void showFragmentA()
    {
    if(fragmentA==null)
    {
    fragmentA = new FragmentA_();
    }
    if(!fragmentA.isVisible())
    {
    getSupportFragmentManager().beginTransaction()
    .replace(R.id.layout_content, fragmentA).commit();
    }
    menu.showContent(true);
    }
     
    public void showFragmentB()
    {
    if(fragmentB==null)
    {
    fragmentB = new FragmentB_();
    }
    if(!fragmentB.isVisible())
    {
    getSupportFragmentManager().beginTransaction()
    .replace(R.id.layout_content, fragmentB).commit();
    }
    menu.showContent(true);
    }
     
     
    }

    MenuFragment.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    package com.pocketdigi.fragment;
     
    import android.support.v4.app.Fragment;
    import android.widget.Button;
     
    import com.googlecode.androidannotations.annotations.AfterViews;
    import com.googlecode.androidannotations.annotations.Click;
    import com.googlecode.androidannotations.annotations.EFragment;
    import com.googlecode.androidannotations.annotations.ViewById;
    import com.pocketdigi.slidingmenudemo.MainActivity;
    import com.pocketdigi.slidingmenudemo.R;
     
    @EFragment(R.layout.fragment_menu)
    public class MenuFragment extends Fragment {
    @ViewById
    Button btn_fragmentA,btn_fragmentB;
    MainActivity mainActivity;
     
    @AfterViews
    public void afterViews()
    {
    mainActivity=(MainActivity)getActivity();
    }
     
    @Click
    public void btn_fragmentA()
    {
    mainActivity.showFragmentA();
    }
    @Click
    public void btn_fragmentB()
    {
    mainActivity.showFragmentB();
    }
    }
  • 相关阅读:
    redis数据结构
    XMAPP 的安装与配置
    Android 工具类 异常处理类CrashHandler
    Android Config通用类来记录信息
    Android AppUtil通用类
    Android 用Chrome浏览器打开url 自定义样式
    Diycode开源项目 如何解决InputMethodManager造成的内存泄漏问题
    Diycode开源项目 SitesListFragment分析
    Diycode开源项目 NodeListFragment分析
    Diycode开源项目 搭建可以具有下拉刷新和上拉加载的Fragment
  • 原文地址:https://www.cnblogs.com/Free-Thinker/p/3581506.html
Copyright © 2020-2023  润新知