本博客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;
@Override
public 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拷贝到对应的文件夹即可。