28
2014
06

Android开发学习:切换菜单-点击侧栏进行切换,使用SlidingFragment

Android开发学习

切换菜单,第四种:切换菜单-点击侧栏按钮进行切换,使用SlidingFragment,使用控件FrameLayout

新建xml布局文件:activity_four.xml,使用FrameLayout来显示每个页的内容,代码内容:

<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:background="@color/white"
    android:clickable="true"
    android:orientation="vertical" >

    <include
        layout="@layout/four_top"
        android:focusable="true" />

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

新建第一页xml布局文件:main_left_fragment.xml,用来显示菜单列表,代码内容:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/tools_box_bg"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/llRoot"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <include
            android:id="@+id/four_left_1_BtnLayout"
            layout="@layout/toolbox_item_1" />
       
        <include
            android:id="@+id/four_left_2_BtnLayout"
            layout="@layout/toolbox_item_2" />

        <include
            android:id="@+id/four_left_3_BtnLayout"
            layout="@layout/toolbox_item_3" />
       
        <include
            android:id="@+id/four_left_4_BtnLayout"
            layout="@layout/toolbox_item_4" />

        <include
            android:id="@+id/four_left_5_BtnLayout"
            layout="@layout/toolbox_item_5" />
    </LinearLayout>

</ScrollView>

 每一页的布局文件就不一一列出

使用线性布局LinearLayout,资源文件中的四个按钮使用布局图片文件layout_1_title.xml、layout_2_title.xml、layout_3_title.xml、layout_4_title.xml、layout_5_title.xml、activitty_four.xml,在此就不一一列出

五个按钮点击后对应五个布局及页面:使用Fragment、SlidingMenu,包括:FourActivity.java、FourTab1Activity.java、FourTab2Activity.java、FourTab3Activity.java、FourTab4Activity.java、FourTab5Activity.java、FourLeftFragment.java、FourRightFragment.java、SlidingActivityBase.java、SlidingActivityHelper.java、、SlidingFragmentActivity.java,在此也不一一列出

实现FourActivity.java代码:

package com.zhengdecai.switchmenu;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

import com.TabActivity.FourLeftFragment;
import com.TabActivity.FourRightFragment;
import com.TabActivity.FourTab1Activity;
import com.publicClass.FourSlidingMenu;
import com.publicClass.SlidingFragmentActivity;

/**
 * 切换菜单:使用Fragment、FourSlidingMenu,包括:FourActivity.java、FourTab1Activity.java、FourTab2Activity.java、FourTab3Activity.java、FourTab4Activity.java、FourTab5Activity.java
 * FourLeftFragment.java、FourRightFragment.java、SlidingActivityBase.java、SlidingActivityHelper.java、、SlidingFragmentActivity.java
 *
 * @author 郑德才
 *
 */
public class FourActivity  extends SlidingFragmentActivity  implements OnClickListener{
 protected FourSlidingMenu leftRightSlidingMenu;
 private ImageButton ivTitleBtnLeft;
 private ImageButton ivTitleBtnRight;
 private Fragment mContent;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  initLeftRightSlidingMenu();
  setContentView(R.layout.activity_four);
  initView();
 }
 
 
 private void initView() {
  ivTitleBtnLeft = (ImageButton)this.findViewById(R.id.ivTitleBtnLeft);
  ivTitleBtnLeft.setOnClickListener(this);
  ivTitleBtnRight = (ImageButton)this.findViewById(R.id.ivTitleBtnRight);
  ivTitleBtnRight.setOnClickListener(this);
 }

 private void initLeftRightSlidingMenu() {
  mContent = new FourTab1Activity();
  getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, mContent).commit();
  setBehindContentView(R.layout.main_left_layout);
  FragmentTransaction leftFragementTransaction = getSupportFragmentManager().beginTransaction();
  Fragment leftFrag = new FourLeftFragment();
  leftFragementTransaction.replace(R.id.main_left_fragment, leftFrag);
  leftFragementTransaction.commit();
  // customize the SlidingMenu
  leftRightSlidingMenu = getSlidingMenu();
  leftRightSlidingMenu.setMode(FourSlidingMenu.LEFT_RIGHT);// 设置是左滑还是右滑,还是左右都可以滑,我这里只做了左滑
  leftRightSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);// 设置菜单宽度
  leftRightSlidingMenu.setFadeDegree(0.35f);// 设置淡入淡出的比例
  leftRightSlidingMenu.setTouchModeAbove(FourSlidingMenu.TOUCHMODE_FULLSCREEN);//设置手势模式
  leftRightSlidingMenu.setShadowDrawable(R.drawable.four_shadow);// 设置左菜单阴影图片
  leftRightSlidingMenu.setFadeEnabled(true);// 设置滑动时菜单的是否淡入淡出
  leftRightSlidingMenu.setBehindScrollScale(0.333f);// 设置滑动时拖拽效果
  
  leftRightSlidingMenu.setSecondaryMenu(R.layout.main_right_layout);
  FragmentTransaction rightFragementTransaction = getSupportFragmentManager().beginTransaction();
  Fragment rightFrag = new FourRightFragment();
  leftFragementTransaction.replace(R.id.main_right_fragment, rightFrag);
  rightFragementTransaction.commit();
 }
 
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
  case R.id.ivTitleBtnLeft:
   leftRightSlidingMenu.showMenu();
   break;
  case R.id.ivTitleBtnRight:
   leftRightSlidingMenu.showSecondaryMenu(true);
   break;
  default:
   break;
  }
  
 }


 public void switchContent(Fragment fragment) {
  mContent = fragment;
  getSupportFragmentManager()
  .beginTransaction()
  .replace(R.id.content_frame, fragment)
  .commit();
  getSlidingMenu().showContent();
 }
 
}

模拟器运行效果图:

 

实际效果参考:http://apk.91.com/Soft/Android/com.zhengdecai.www-2.html

 

« 上一篇下一篇 »

评论列表:

1.东北大米  2014/9/10 9:40:58 回复该留言
博主是技术大牛吧,好厉害,完全看不懂。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。