效果如上
slidingmenu是github上的开源项目,今天就简单实现一下
由于slidingmenu还用到了actionbarsherlock这个开源项目,所以导入项目的时候需要导入actionbarsherlock项目和slidingmenu项目,如下图
都已经打到包中了,导入的时候按照以下顺序导入,actionbarsherlock->slidingmenu->侧滑栏5
主要设置的类贴一下,其他的就不贴了,自己下载完看吧
package com.example.cehualan5;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
public class MainActivity extends SlidingFragmentActivity implements OnClickListener{
private SlidingMenu mSlidingMenu;
private ImageButton z_btn,y_btn;
private Fragment mContext;
public static TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
initSlidingMenu();
setContentView(R.layout.ui_activity);
initView();
}
private void initSlidingMenu(){
mSlidingMenu = getSlidingMenu();
mSlidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
mSlidingMenu.setBehindOffsetRes(R.dimen.offset);
mSlidingMenu.setFadeDegree(0.5f);
mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
mSlidingMenu.setFadeEnabled(true);
mSlidingMenu.setBehindScrollScale(0.5f);
mContext = new Fragment1();
getSupportFragmentManager().beginTransaction().replace(R.id.main_context, mContext).commit();
setBehindContentView(R.layout.zuo_layout);
mSlidingMenu.setShadowDrawable(R.drawable.shadow);
getSupportFragmentManager().beginTransaction().replace(R.id.zuo_fragment, new Zuo_Fragment()).commit();
mSlidingMenu.setSecondaryMenu(R.layout.you_layout);
mSlidingMenu.setSecondaryShadowDrawable(R.drawable.shadow);
getSupportFragmentManager().beginTransaction().replace(R.id.you_fragment, new You_Fragment()).commit();
}
private void initView(){
z_btn = (ImageButton)findViewById(R.id.z_btn);
z_btn.setOnClickListener(this);
y_btn = (ImageButton)findViewById(R.id.y_btn);
y_btn.setOnClickListener(this);
mTextView = (TextView)findViewById(R.id.mingchen);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.z_btn:
mSlidingMenu.showMenu(true);
break;
case R.id.y_btn:
mSlidingMenu.showSecondaryMenu(true);
break;
default:
break;
}
}
public void switchContent(Fragment fragment){
mContext = fragment;
getSupportFragmentManager().beginTransaction().replace(R.id.main_context, fragment).commit();
getSlidingMenu().showContent();
}
}
源码下载