当前位置:   article > 正文

利用DrawLayout和Fragment实现左右侧滑菜单_android侧滑菜单栏实现:drawerlayout+fragment

android侧滑菜单栏实现:drawerlayout+fragment

利用Fragment实现界面左右滑动后显示侧滑的菜单,效果图如下:

方案一:利用

方案一:一个布局完成(左右抽屉布局与中间布局在同一个布局文件中)

布局:draw_activity_layouot.xml

  1. <android.support.v4.widget.DrawerLayout
  2. android:id="@+id/main_activity_drawerLayout"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="@android:color/white"
  7. android:orientation="vertical">
  8. <FrameLayout
  9. android:id="@+id/main_activity_container"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent">
  12. </FrameLayout>
  13. <!--左边抽屉,在这里一定要设置layout_gravity="start" ,并且宽度不能超过320dp-->
  14. <LinearLayout
  15. android:id="@+id/left_draw_layout"
  16. android:layout_width="100dp"
  17. android:layout_height="match_parent"
  18. android:layout_gravity="start"
  19. android:background="@android:color/holo_blue_dark"
  20. android:orientation="vertical">
  21. <LinearLayout
  22. android:id="@+id/layout1"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content">
  25. <ImageView
  26. android:layout_width="80dip"
  27. android:layout_height="80dip"
  28. android:src="@drawable/a"/>
  29. </LinearLayout>
  30. <LinearLayout
  31. android:id="@+id/layout2"
  32. android:layout_width="wrap_content"
  33. android:layout_height="wrap_content">
  34. <ImageView
  35. android:layout_width="80dip"
  36. android:layout_height="80dip"
  37. android:src="@drawable/b"/>
  38. </LinearLayout>
  39. <LinearLayout
  40. android:id="@+id/layout3"
  41. android:layout_width="wrap_content"
  42. android:layout_height="wrap_content">
  43. <ImageView
  44. android:layout_width="80dip"
  45. android:layout_height="80dip"
  46. android:src="@drawable/c"/>
  47. </LinearLayout>
  48. </LinearLayout>
  49. <!--右边抽屉,在这里一定要设置layout_gravity="end" ,并且宽度不能超过320dp-->
  50. <LinearLayout
  51. android:id="@+id/right_draw_layout"
  52. android:layout_width="100dp"
  53. android:layout_height="match_parent"
  54. android:layout_gravity="end"
  55. android:background="@android:color/holo_green_dark"
  56. android:orientation="vertical">
  57. <!--此处可设置右边抽屉的布局-->
  58. </LinearLayout>
  59. </android.support.v4.widget.DrawerLayout>
  1. <span style="font-family: Arial, Helvetica, sans-serif;">
  2. </span>
<span style="font-family: Arial, Helvetica, sans-serif;">LDrawActivity</span>

  1. package learnbroadcast.ld.com.broadcastdemo;
  2. import android.os.Bundle;
  3. import android.support.v4.app.FragmentActivity;
  4. import android.support.v4.app.FragmentManager;
  5. import android.support.v4.widget.DrawerLayout;
  6. import android.support.v4.widget.DrawerLayout.DrawerListener;
  7. import android.view.Gravity;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.Toast;
  11. public class LDrawActivity extends FragmentActivity implements DrawerListener ,OnClickListener{
  12. private DrawerLayout mDrawerLayout;//抽屉菜单
  13. private boolean isOpenDrawlayout=false;//是否打开抽屉
  14. private FragmentManager fragmentManager=getSupportFragmentManager();;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.draw_activity_layouot);
  19. mDrawerLayout = (DrawerLayout) findViewById(R.id.main_activity_drawerLayout);
  20. initDrawLayout();
  1. //初始化中间布局
  2. Fragment3 fragment3=new Fragment3();
  3. fragmentManager.beginTransaction().replace(R.id.main_activity_container, fragment3).commit();
  4. }
  5. //左边抽屉监听
  6. private void initDrawLayout() {
  7. mDrawerLayout.setDrawerListener(this);
  8. findViewById(R.id.layout1).setOnClickListener(this);
  9. findViewById(R.id.layout2).setOnClickListener(this);
  10. findViewById(R.id.layout3).setOnClickListener(this);
  11. }
  12. // ----------------------实现抽屉的监听事件------------------------
  13. @Override
  14. public void onDrawerSlide(View drawerView, float slideOffset) {
  15. }
  16. @Override
  17. public void onDrawerOpened(View drawerView) {
  18. isOpenDrawlayout=true;
  19. }
  20. @Override
  21. public void onDrawerClosed(View drawerView) {
  22. isOpenDrawlayout=false;
  23. }
  24. @Override
  25. public void onDrawerStateChanged(int newState) {
  26. }
  27. @Override
  28. public void onClick(View v) {
  29. switch(v.getId()){
  30. case R.id.layout1:
<pre name="code" class="java">               //点击的时候切换中间的布局
Toast.makeText(LDrawActivity.this, "点击了第一个美女", Toast.LENGTH_SHORT).show(); Fragment1 fragment1=new Fragment1();
 
  1. fragmentManager.beginTransaction().replace(R.id.main_activity_container, fragment1).commit();
  2. mDrawerLayout.closeDrawer(Gravity.LEFT);
  3. break;
  4. case R.id.layout2:
<pre name="code" class="java">             //点击的时候切换中间的布局
Toast.makeText(LDrawActivity.this,"点击了第二个美女",Toast.LENGTH_SHORT).show(); Fragment2 fragment2=new Fragment2(); fragmentManager.beginTransaction().replace(R.id.main_activity_container, fragment2).commit(); mDrawerLayout.closeDrawer(Gravity.LEFT); break; case R.id.layout3: Toast.makeText(LDrawActivity.this,"点击了第三个美女",Toast.LENGTH_SHORT).show(); mDrawerLayout.closeDrawer(Gravity.LEFT); break; } }}
 

点击下方的左边按钮就会显示左边的侧滑按钮,向右滑动手势也可以打开左侧的侧滑菜单,同理右边的侧滑菜单也是同样的,具体结构如下:

方案二:左右抽屉布局独立出来,通过代码加到主界面

MainActivity extends FragmentActivity

3个XML布局文件

activity_main       中间主界面布局

left_menu_layout    左边侧滑菜单布局

right_menu_layout   右边侧滑菜单布局

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. >
  6. <android.support.v4.widget.DrawerLayout
  7. android:id="@+id/main_drawer_layout"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. >
  11. <!-- 中间内容 -->
  12. <FrameLayout
  13. android:id="@+id/main_content_frame_parent"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent" >
  16. </FrameLayout>
  17. <!-- 左侧滑动栏 -->
  18. <RelativeLayout
  19. android:id="@+id/main_left_drawer_layout"
  20. android:layout_width="240dp"
  21. android:layout_height="match_parent"
  22. android:layout_gravity="left"
  23. android:background="@android:color/transparent"
  24. android:paddingBottom="50dip" >
  25. </RelativeLayout>
  26. <!-- 右侧滑动栏 -->
  27. <RelativeLayout
  28. android:id="@+id/main_right_drawer_layout"
  29. android:layout_width="240dp"
  30. android:layout_height="match_parent"
  31. android:layout_gravity="right"
  32. android:background="@android:color/transparent"
  33. android:paddingBottom="50dp" />
  34. </android.support.v4.widget.DrawerLayout>
  35. <Button android:id="@+id/left_btn"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_alignParentBottom="true"
  39. android:layout_alignParentLeft="true"
  40. android:text="左边"/>
  41. <Button android:id="@+id/right_btn"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:layout_alignParentBottom="true"
  45. android:layout_alignParentRight="true"
  46. android:text=" 右边"/>
  47. </RelativeLayout>

  1. <pre name="code" class="java">package com.example.ldclient;
  2. import android.annotation.SuppressLint;
  3. import android.os.Bundle;
  4. import android.support.v4.app.ActionBarDrawerToggle;
  5. import android.support.v4.app.FragmentActivity;
  6. import android.support.v4.widget.DrawerLayout;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. import android.widget.Button;
  10. import android.widget.ImageView;
  11. import android.widget.LinearLayout;
  12. import android.widget.RelativeLayout;
  13. import android.widget.TextView;
  14. //
  15. @SuppressWarnings("deprecation")
  16. @SuppressLint({ "ClickableViewAccessibility", "InflateParams", "HandlerLeak" })
  17. public class MainActivity extends FragmentActivity {
  18. private RelativeLayout main_left_drawer_layout;
  19. private RelativeLayout main_right_drawer_layout;
  20. private ActionBarDrawerToggle drawerbar; // 抽屉菜单对象
  21. // 左侧菜单资源属性
  22. private ImageView backQuan;
  23. private ImageView userPhoto; // 用户头像
  24. private RelativeLayout mUserPhotoLayout;
  25. private RelativeLayout mUserMoney;
  26. private LinearLayout mMyOrderLayout;
  27. private LinearLayout mRecentViewLayout;
  28. private LinearLayout mMyMessageLayout;
  29. private LinearLayout mHelpLayout;
  30. private LinearLayout mAboutUsLayout;
  31. private LinearLayout mMyBugLayout;
  32. private DrawerLayout drawerLayout;
  33. private Button left_btn,right_btn;
  34. private TextView balance;
  35. @Override
  36. protected void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_main);
  39. initView();
  40. initEvent();
  41. }
  42. @Override
  43. protected void onResume() {
  44. super.onResume();
  45. }
  46. // 实例化view
  47. public void initView() {
  48. drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
  49. initLeftLayout();
  50. initRightLayout();
  51. left_btn = (Button) findViewById(R.id.left_btn);
  52. left_btn.setOnClickListener(new OnClickListener() {
  53. public void onClick(View v) {
  54. openLeftLayout();
  55. }
  56. });
  57. right_btn = (Button) findViewById(R.id.right_btn);
  58. right_btn.setOnClickListener(new OnClickListener() {
  59. @Override
  60. public void onClick(View v) {
  61. openRightLayout();
  62. }
  63. });
  64. // 中间替换fragment
  65. // mMainFragment = new TEMainFragment(isNotification);
  66. // FragmentManager fragmentManager = getSupportFragmentManager();
  67. // FragmentTransaction f_transaction = fragmentManager.beginTransaction();
  68. // f_transaction.replace(R.id.main_content_frame_parent, mMainFragment);
  69. // f_transaction.commit();
  70. }
  71. private void initEvent() {
  72. drawerbar = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.app_name, R.string.app_name) {
  73. @Override
  74. public void onDrawerOpened(View drawerView) {
  75. super.onDrawerOpened(drawerView);
  76. }
  77. @Override
  78. public void onDrawerClosed(View drawerView) {
  79. super.onDrawerClosed(drawerView);
  80. }
  81. };
  82. drawerLayout.setDrawerListener(drawerbar);
  83. drawerbar.setDrawerIndicatorEnabled(false);
  84. }
  85. public void initLeftLayout() {
  86. // 设置透明
  87. // drawerLayout.setScrimColor(0x00000000);
  88. main_left_drawer_layout = (RelativeLayout) findViewById(R.id.main_left_drawer_layout);
  89. View view = getLayoutInflater().inflate(R.layout.left_menu_layout, null);
  90. backQuan = (ImageView) view.findViewById(R.id.back_quan);
  91. userPhoto = (ImageView) view.findViewById(R.id.user_photo);
  92. balance = (TextView) view.findViewById(R.id.balance);
  93. mUserPhotoLayout = (RelativeLayout) view.findViewById(R.id.user_photo_layout);
  94. mUserMoney = (RelativeLayout) view.findViewById(R.id.user_money);
  95. mMyOrderLayout = (LinearLayout) view.findViewById(R.id.my_order_layout);
  96. mRecentViewLayout = (LinearLayout) view.findViewById(R.id.recently_view_layout);
  97. mMyMessageLayout = (LinearLayout) view.findViewById(R.id.my_message_layout);
  98. mHelpLayout = (LinearLayout) view.findViewById(R.id.help_layout);
  99. mAboutUsLayout = (LinearLayout) view.findViewById(R.id.about_us_layout);
  100. mMyBugLayout = (LinearLayout) view.findViewById(R.id.my_bug_layout);
  101. main_left_drawer_layout.addView(view);
  102. }
  103. /**
  104. * 右边边菜单
  105. */
  106. public void initRightLayout() {
  107. main_right_drawer_layout = (RelativeLayout)findViewById(R.id.main_right_drawer_layout);
  108. View view = getLayoutInflater().inflate(R.layout.right_menu_layout, null);
  109. main_right_drawer_layout.addView(view);
  110. }
  111. /**
  112. * 打开左边菜单
  113. */
  114. public void openLeftLayout() {
  115. if (drawerLayout.isDrawerOpen(main_left_drawer_layout)) {
  116. drawerLayout.closeDrawer(main_left_drawer_layout);
  117. } else {
  118. drawerLayout.openDrawer(main_left_drawer_layout);
  119. drawerLayout.closeDrawer(main_right_drawer_layout);
  120. }
  121. }
  122. /**
  123. * 打开右边菜单
  124. */
  125. public void openRightLayout() {
  126. if (drawerLayout.isDrawerOpen(main_right_drawer_layout)) {
  127. drawerLayout.closeDrawer(main_right_drawer_layout);
  128. } else {
  129. drawerLayout.openDrawer(main_right_drawer_layout);
  130. drawerLayout.closeDrawer(main_left_drawer_layout);
  131. }
  132. }
  133. // /**
  134. // * 关闭滑动效果
  135. // */
  136. // public void closeSlide() {
  137. // drawerLayout.removeView(left_menu_layout);
  138. // }
  139. //
  140. // /**
  141. // * 关闭左菜单
  142. // */
  143. // public void closeLeftMenu() {
  144. // drawerLayout.closeDrawer(left_menu_layout);
  145. // }
  146. //
  147. // /**
  148. // * 代开滑动效果
  149. // */
  150. // public void openSlide() {
  151. // drawerLayout.removeView(left_menu_layout);
  152. // drawerLayout.addView(left_menu_layout);
  153. // }
  154. public MainActivity getMainActivity() {
  155. return this;
  156. }
  157. @Override
  158. protected void onDestroy() {
  159. super.onDestroy();
  160. }
  161. }

 左右的侧滑菜单可以自己定义,自定义侧滑菜单的样式,当然侧滑菜单中控件也可以进行事件监听,进一步处理,此处不详述。中间的Fragment 也是自定义,使用的时候只要调用折断代码即可: 

  1. // 中间替换fragment
  2. // mMainFragment = new TEMainFragment(isNotification);
  3. // FragmentManager fragmentManager = getSupportFragmentManager();
  4. // FragmentTransaction f_transaction = fragmentManager.beginTransaction();
  5. // f_transaction.replace(R.id.main_content_frame_parent, mMainFragment);
  6. // f_transaction.commit();

其中的 TEMainFragment是中间部分显示的内容。

其中的initEvent()中的方法是系统默认的一个打开菜单的方法,实际应用过程中可以忽略,即可以不用initEvent()方法也可以实现功能,完全没有影响






声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/279608
推荐阅读
相关标签
  

闽ICP备14008679号