SlidingMenu——方式1
1、初始化
SlidingMenu
对象
2、设置
SlidingMenu
的菜单模式。 只有左侧菜单,只有右侧菜单,或者双侧菜单
3、设置SlidingMenu显示的菜单布局
4、设置菜单的显示方式
5、设置滑动菜单的隐藏方式
6、设置菜单显示宽度
7、将菜单附加到当前Activity中进行显示
注意,
1、寻找菜单中控件,一定要在布局加入后,才要findViewById,否则会出现空指针异常
2、如果菜单中有点击事件,则不可以将菜单的显示设为内容滑动(两者有冲突)
功能:
1、因为可以加入布局,所以菜单中可以随意定义显示的布局样式
2、可以设置菜单的显示,隐藏方式(边缘滑动,内容滑动,两者都不可以)
3、可以设置显示方式(只有左侧菜单,只有右侧菜单,两侧都有)
4、可以设置菜单显示的宽度
5、在添加到activity中,可以设置是否共享标题栏
6、可以控制菜单滚动时,先出现菜单哪部分
7、菜单滚动时,可以改变菜单的透明度变化
SlidingMenu——方式2
1.
将继承的
Activity
替换为框架中的
SlidingFragmentActivity
2.
调用
setBehindContentView
设置菜单的显示
3.
初始化
SlidingMenu
对象
4.
设置
SlidingMenu
的菜单模式。 只有左侧菜单,只有右侧菜单,或者双侧菜单
5.
设置菜单的显示方式
6.
设置滑动菜单的隐藏方式
7.
设置菜单显示宽度
注意:
1、寻找菜单中控件,一定要在
sliding=getSlidingMenu()后
,才要findViewById,否则会出现空指针异常
与1的不同:
1.因为没有attachToActivit方法,所以就不存在是否共享标题栏,其实activity本身没有标题栏
SlidingMenu——方式3
- 1. 在方式一或者方式二的基础上先显示一个基础SlidingMenu
- 2.在设置SlidingMenu菜单显示的布局中,将内容变为一个FrameLayout
- 3.通过FragmentManager的add或replace方法将要作为菜单显示内容的Fragment替换第二步中FrameLayout中的内容即可
注意:
这样写对菜单内的控件的点击事件,只能写在fragMent中,并且更新UI还得通过回调接口
贴上部分代码
SlidingMenu1:
package
com.wang.main;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.support.v7.app.AppCompatActivity;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.DragEvent;
import
android.view.View;
import
android.widget.Button;
import
android.widget.ImageView;
import
android.widget.LinearLayout;
import
android.widget.ScrollView;
import
android.widget.TextView;
import
android.widget.Toast;
import
com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
public
class
MainActivity
extends
AppCompatActivity
implements
View.OnClickListener {
private
SlidingMenu sliding;
private
ImageView iv;
private
LinearLayout layout1,layout2,layout3;
private
TextView bottom_tv1,bottom_tv2;
private
ScrollView scrollview;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sliding=
new
SlidingMenu(
this
);
sliding.setMode(SlidingMenu.LEFT);
sliding.setMenu(R.layout.slidingleft);
initView();
sliding.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
sliding.setTouchModeBehind(SlidingMenu.TOUCHMODE_MARGIN);
int
i=getResources().getDisplayMetrics().widthPixels*
2
/
3
;
sliding.setBehindWidth(i);
sliding.setShadowDrawable(getResources().getDrawable(R.drawable.shape_sdshadowdraw));
sliding.setShadowWidth(
30
);
sliding.setFadeDegree(
1
.0f);
sliding.setBehindScrollScale(
0
.5f);
sliding.attachToActivity(
this
,SlidingMenu.SLIDING_WINDOW);
GetRoundBitmap gp=
new
GetRoundBitmap();
Bitmap bp=gp.getRoundBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.naruto));
Log.i(
"mtag"
,
"start"
+bp);
Log.i(
"mtag"
,
"start"
+iv);
iv.setImageBitmap(bp);
}
/**
* 寻找控件
*
*@author WangShujie
*created at 2016/4/23 18:59
*/
private
void
initView() {
iv= (ImageView)sliding.getMenu().findViewById(R.id.slidingleft_iv);
layout1= (LinearLayout) sliding.getMenu().findViewById(R.id.slidingleft_layout1);
layout2=(LinearLayout) sliding.getMenu().findViewById(R.id.slidingleft_layout2);
layout3=(LinearLayout) sliding.getMenu().findViewById(R.id.slidingleft_layout3);
bottom_tv1= (TextView) sliding.getMenu().findViewById(R.id.sliding_botomtv1);
bottom_tv2= (TextView) sliding.getMenu().findViewById(R.id.sliding_botomtv2);
scrollview= (ScrollView) sliding.getMenu().findViewById(R.id.slidingleft_lv);
layout1.setOnClickListener(
this
);
layout2.setOnClickListener(
this
);
layout3.setOnClickListener(
this
);
bottom_tv1.setOnClickListener(
this
);
bottom_tv2.setOnClickListener(
this
);
}
/**
*对控件进行监听
*
*@author WangShujie
*created at 2016/4/23 19:31
*/
@Override
public
void
onClick(View v) {
switch
(v.getId()){
case
R.id.slidingleft_layout1:
Toast.makeText(MainActivity.
this
,
"你点击了我的好友"
, Toast.LENGTH_SHORT).show();
break
;
case
R.id.slidingleft_layout2:
Toast.makeText(MainActivity.
this
,
"你点击了我的收藏"
, Toast.LENGTH_SHORT).show();
break
;
case
R.id.slidingleft_layout3:
Toast.makeText(MainActivity.
this
,
"你点击了退出"
, Toast.LENGTH_SHORT).show();
break
;
case
R.id.sliding_botomtv1:
Toast.makeText(MainActivity.
this
,
"你点击了设置1"
, Toast.LENGTH_SHORT).show();
break
;
case
R.id.sliding_botomtv2:
Toast.makeText(MainActivity.
this
,
"你点击了设置2"
, Toast.LENGTH_SHORT).show();
break
;
}
}
}
SlidingMenu2:
package
com.wang.main;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.View;
import
android.widget.ImageView;
import
android.widget.LinearLayout;
import
android.widget.ScrollView;
import
android.widget.TextView;
import
android.widget.Toast;
import
com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import
com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import
com.wang.main.GetRoundBitmap;
public
class
MainActivity
extends
SlidingFragmentActivity
implements
View.OnClickListener {
private
SlidingMenu sliding;
private
ImageView iv;
private
LinearLayout layout1,layout2,layout3;
private
TextView bottom_tv1,bottom_tv2;
private
ScrollView scrollview;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.slidingleft);
sliding=getSlidingMenu();
initView();
sliding.setMode(SlidingMenu.LEFT);
sliding.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
sliding.setTouchModeBehind(SlidingMenu.TOUCHMODE_MARGIN);
int
i=getResources().getDisplayMetrics().widthPixels*
2
/
3
;
sliding.setBehindWidth(i);
sliding.setShadowDrawable(getResources().getDrawable(R.drawable.shape_sdshadowdraw));
sliding.setShadowWidth(
30
);
sliding.setFadeDegree(
1
.0f);
sliding.setBehindScrollScale(
0
.5f);
GetRoundBitmap gp=
new
GetRoundBitmap();
Bitmap bp=gp.getRoundBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.naruto));
Log.i(
"mtag"
,
"start"
+bp);
Log.i(
"mtag"
,
"start"
+iv);
iv.setImageBitmap(bp);
}
/**
* 寻找控件
*
*@author WangShujie
*created at 2016/4/23 18:59
*/
private
void
initView() {
iv= (ImageView)sliding.getMenu().findViewById(R.id.slidingleft_iv);
layout1= (LinearLayout) sliding.getMenu().findViewById(R.id.slidingleft_layout1);
layout2=(LinearLayout) sliding.getMenu().findViewById(R.id.slidingleft_layout2);
layout3=(LinearLayout) sliding.getMenu().findViewById(R.id.slidingleft_layout3);
bottom_tv1= (TextView) sliding.getMenu().findViewById(R.id.sliding_botomtv1);
bottom_tv2= (TextView) sliding.getMenu().findViewById(R.id.sliding_botomtv2);
scrollview= (ScrollView) sliding.getMenu().findViewById(R.id.slidingleft_lv);
Log.i(
"mtag"
,
"tianfuxiaoyan++iv===="
+iv);
Log.i(
"mtag"
,
"tianfuxiaoyan++layout1===="
+layout1);
Log.i(
"mtag"
,
"tianfuxiaoyan++bottom_tv1===="
+bottom_tv1);
layout1.setOnClickListener(
this
);
layout2.setOnClickListener(
this
);
layout3.setOnClickListener(
this
);
bottom_tv1.setOnClickListener(
this
);
bottom_tv2.setOnClickListener(
this
);
}
/**
*对控件进行监听
*
*@author WangShujie
*created at 2016/4/23 19:31
*/
@Override
public
void
onClick(View v) {
switch
(v.getId()){
case
R.id.slidingleft_layout1:
Toast.makeText(MainActivity.
this
,
"你点击了我的好友"
, Toast.LENGTH_SHORT).show();
break
;
case
R.id.slidingleft_layout2:
Toast.makeText(MainActivity.
this
,
"你点击了我的收藏"
, Toast.LENGTH_SHORT).show();
break
;
case
R.id.slidingleft_layout3:
Toast.makeText(MainActivity.
this
,
"你点击了退出"
, Toast.LENGTH_SHORT).show();
break
;
case
R.id.sliding_botomtv1:
Toast.makeText(MainActivity.
this
,
"你点击了设置1"
, Toast.LENGTH_SHORT).show();
break
;
case
R.id.sliding_botomtv2:
Toast.makeText(MainActivity.
this
,
"你点击了设置2"
, Toast.LENGTH_SHORT).show();
break
;
}
}
}
原码下载地址:
http://download.csdn.net/detail/tianfuxiaoyan/9501522