您好,登錄后才能下訂單哦!
創建slidingMenu自定義菜單。
1、繼承activity,通過創建 SlidingMenu slidingMenu=new SlidingMenu(this)獲取滑動
菜單。
實例:
1)、左菜單
package com.zhy.zhy_slidemenu_demo;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// configure the SlidingMenu
SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
//設置觸摸屏幕的模式
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
// 設置滑動菜單視圖的寬度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// 設置漸入漸出效果的值
menu.setFadeDegree(0.35f);
/**
* SLIDING_WINDOW will include the Title/ActionBar in the content
* section of the SlidingMenu, while SLIDING_CONTENT does not.
*/
//把滑動菜單添加進所有的Activity中,可選值SLIDING_CONTENT , SLIDING_WINDOW
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
//為側滑菜單設置布局
menu.setMenu(R.layout.leftmenu);
}
}
2)、左、右菜單
package com.example.aaaaaaaaa;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
public class MainActivity extends Activity {
private ViewPager viewPager;
private SlidingMenu slidingMenu;
private View []str=new View[5];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < str.length; i++) {
TextView textView=new TextView(this);
LayoutParams params= textView.getLayoutParams();
if(params!=null){
params.height=LayoutParams.WRAP_CONTENT;
params.width=LayoutParams.WRAP_CONTENT;
}else{
params=new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
textView.setLayoutParams(params);
textView.setText(i+"-----------");
str[i]=textView;
}
initFirstSliding();
intiPage();
}
private void intiPage(){
viewPager=(ViewPager) findViewById(R.id.vp);
viewPager.setAdapter(new PagerAdapter() {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0==arg1;
}
@Override
public int getCount() {
return str.length;
}
@Override
public void destroyItem(ViewGroup container, int position,
Object object) {
((ViewPager)container).removeView(str[position]);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
((ViewPager)container).addView(str[position]);
return str[position];
}
});
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
System.out.println(arg0+",,,,,,"+str.length);
if(arg0==0){
System.out.println(arg0+",,,,,,"+str.length);
initFirstSliding();
}else if(arg0==str.length-1){
System.out.println("1111=="+arg0+",,,,,,"+str.length);
initSecondSliding();
}else{
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
}
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
private void initFirstSliding(){
slidingMenu=new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.LEFT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
slidingMenu.setShadowWidth(3);
slidingMenu.setShadowDrawable(R.drawable.shadow);
slidingMenu.setBehindWidth(200);
// 設置滑動菜單視圖的寬度
slidingMenu.setBehindOffset(100);
// 設置漸入漸出效果的值
slidingMenu.setFadeDegree(0.35f);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
//為側滑菜單設置布局
slidingMenu.setMenu(R.layout.leftmenu);
}
private void initSecondSliding(){
slidingMenu=new SlidingMenu(this);
slidingMenu.setMode(SlidingMenu.RIGHT);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
slidingMenu.setShadowWidth(3);
slidingMenu.setShadowDrawable(R.drawable.shadow);
slidingMenu.setBehindWidth(200);
// 設置滑動菜單視圖的寬度
slidingMenu.setBehindOffset(100);
// 設置漸入漸出效果的值
slidingMenu.setFadeDegree(0.35f);
slidingMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
//為側滑菜單設置布局
//slidingMenu.setMenu(R.layout.leftmenu);
slidingMenu.setSecondaryMenu(R.layout.rightmenu);
slidingMenu.setSecondaryShadowDrawable(R.drawable.shadow);
}
private void getLeftMenu(){
slidingMenu.showMenu();
}
private void getRightMenu(){
slidingMenu.showSecondaryMenu();
}
}
2、繼承SlidingActivity,通過SlidingMenu menu = getSlidingMenu()獲取菜單
實例:
package com.zhy.zhy_slidemenu_demo02;
public class MainActivity extends SlidingActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.leftmenu);
// configure the SlidingMenu
SlidingMenu menu = getSlidingMenu();
menu.setMode(SlidingMenu.LEFT);
// 設置觸摸屏幕的模式
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
// 設置滑動菜單視圖的寬度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// 設置漸入漸出效果的值
menu.setFadeDegree(0.35f);
/**
* SLIDING_WINDOW will include the Title/ActionBar in the content
* section of the SlidingMenu, while SLIDING_CONTENT does not.
*/
// menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
// menu.setMenu(R.layout.leftmenu);
}
}
3、把 SlidingMenu當自定義控件
實例:
1)、布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/id_main_ly"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp" >
<com.jeremyfeinstein.slidingmenu.lib.SlidingMenu
xmlns:sliding="http://schemas.android.com/apk/res-auto"
android:id="@+id/slidingmenulayout"
android:layout_width="120dp"
android:layout_height="170dp"
android:background="#ffffffff"
sliding:behindOffset="0dp"
sliding:behindScrollScale="1"
sliding:fadeDegree="0.3"
sliding:fadeEnabled="true"
sliding:touchModeAbove="fullscreen"
sliding:viewAbove="@layout/pic" />
</LinearLayout>
</RelativeLayout>
2)、實例代碼
package com.zhy.zhy_slidemenu_demo03;
public class MainActivity extends Activity
{
private SlidingMenu mLeftMenu;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLeftMenu = (SlidingMenu) findViewById(R.id.slidingmenulayout);
// configure the SlidingMenu
// SlidingMenu menu = new SlidingMenu(this);
mLeftMenu.setMode(SlidingMenu.LEFT);
// 設置觸摸屏幕的模式
mLeftMenu.setShadowWidthRes(R.dimen.shadow_width);
mLeftMenu.setShadowDrawable(R.drawable.shadow);
mLeftMenu.setMenu(R.layout.leftmenu);
mLeftMenu.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (mLeftMenu.isMenuShowing())
mLeftMenu.toggle();
}
});
// 設置滑動菜單視圖的寬度
// 設置漸入漸出效果的值
/**
* SLIDING_WINDOW will include the Title/ActionBar in the content
* section of the SlidingMenu, while SLIDING_CONTENT does not.
*/
}
}
4、對fragment的支持,繼承SlidingFragmentActivity類
實例:
package com.zhy.zhy_slidemenu_demo04;
public class MainActivity extends SlidingFragmentActivity
{
private ViewPager mViewPager;
private FragmentPagerAdapter mAdapter;
private List<Fragment> mFragments = new ArrayList<Fragment>();
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 初始化SlideMenu
initRightMenu();
// 初始化ViewPager
initViewPager();
}
private void initViewPager()
{
mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
MainTab01 tab01 = new MainTab01();
MainTab02 tab02 = new MainTab02();
MainTab03 tab03 = new MainTab03();
mFragments.add(tab01);
mFragments.add(tab02);
mFragments.add(tab03);
/**
* 初始化Adapter
*/
mAdapter = new FragmentPagerAdapter(getSupportFragmentManager())
{
@Override
public int getCount()
{
return mFragments.size();
}
@Override
public Fragment getItem(int arg0)
{
return mFragments.get(arg0);
}
};
mViewPager.setAdapter(mAdapter);
}
private void initRightMenu()
{
Fragment leftMenuFragment = new MenuLeftFragment();
setBehindContentView(R.layout.left_menu_frame);
getSupportFragmentManager().beginTransaction()
.replace(R.id.id_left_menu_frame, leftMenuFragment).commit();
SlidingMenu menu = getSlidingMenu();
menu.setMode(SlidingMenu.LEFT_RIGHT);
// 設置觸摸屏幕的模式
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
// 設置滑動菜單視圖的寬度
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// menu.setBehindWidth()
// 設置漸入漸出效果的值
menu.setFadeDegree(0.35f);
// menu.setBehindScrollScale(1.0f);
menu.setSecondaryShadowDrawable(R.drawable.shadow);
//設置右邊(二級)側滑菜單
menu.setSecondaryMenu(R.layout.right_menu_frame);
Fragment rightMenuFragment = new MenuRightFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.id_right_menu_frame, rightMenuFragment).commit();
}
public void showLeftMenu(View view)
{
getSlidingMenu().showMenu();
}
public void showRightMenu(View view)
{
getSlidingMenu().showSecondaryMenu();
}
}
5、更多案例可以參考項目提供的example。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。