Android UI-SlidingMenu侧滑菜单效果,android系统开发工具

这篇博客展示了如何在Android应用中实现一个滑动菜单(SlidingMenu),并结合Fragment进行内容切换。通过SlidingFragmentActivity和SlidingMenu库,创建了可以左右滑动的菜单,并设置了阴影效果、触摸模式等。同时,详细介绍了主Activity和侧边栏菜单Fragment的代码实现。
摘要由CSDN通过智能技术生成

本篇博客给大家分享一个效果比较好的侧滑菜单的Demo,实现点击左边菜单切换Fragment。

效果如下:

主Activity代码:

package com.infzm.slidingmenu.demo;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.widget.ImageView;

import android.widget.TextView;

import com.infzm.slidingmenu.demo.fragment.LeftFragment;

import com.infzm.slidingmenu.demo.fragment.TodayFragment;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

/**

  • @date 2014/11/14

  • @author wuwenjie

  • @description 主界面

*/

public class MainActivity extends SlidingFragmentActivity implements

OnClickListener {

private ImageView topButton;

private Fragment mContent;

private TextView topTextView;

@Override

public void onCreate(Bundle savedInstanceState) {

requestWindowFeature(Window.FEATURE_NO_TITLE); // 无标题

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initSlidingMenu(savedInstanceState);

topButton = (ImageView) findViewById(R.id.topButton);

topButton.setOnClickListener(this);

topTextView = (TextView) findViewById(R.id.topTv);

}

/**

  • 初始化侧边栏

*/

private void initSlidingMenu(Bundle savedInstanceState) {

// 如果保存的状态不为空则得到之前保存的Fragment,否则实例化MyFragment

if (savedInstanceState != null) {

mContent = getSupportFragmentManager().getFragment(

savedInstanceState, “mContent”);

}

if (mContent == null) {

mContent = new TodayFragment();

}

// 设置左侧滑动菜单

setBehindContentView(R.layout.menu_frame_left);

getSupportFragmentManager().beginTransaction()

.replace(R.id.menu_frame, new LeftFragment()).commit();

// 实例化滑动菜单对象

SlidingMenu sm = getSlidingMenu();

// 设置可以左右滑动的菜单

sm.setMode(SlidingMenu.LEFT);

// 设置滑动阴影的宽度

sm.setShadowWidthRes(R.dimen.shadow_width);

// 设置滑动菜单阴影的图像资源

sm.setShadowDrawable(null);

// 设置滑动菜单视图的宽度

sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);

// 设置渐入渐出效果的值

sm.setFadeDegree(0.35f);

// 设置触摸屏幕的模式,这里设置为全屏

sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

// 设置下方视图的在滚动时的缩放比例

sm.setBehindScrollScale(0.0f);

}

@Override

protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

getSupportFragmentManager().putFragment(outState, “mContent”, mContent);

}

/**

  • 切换Fragment

  • @param fragment

*/

public void switchConent(Fragment fragment, String title) {

mContent = fragment;

getSupportFragmentManager().beginTransaction()

.replace(R.id.content_frame, fragment).commit();

getSlidingMenu().showContent();

topTextView.setText(title);

}

@Override

public void onClick(View v) {

s
witch (v.getId()) {

case R.id.topButton:

toggle();

break;

default:

break;

}

}

}

侧边栏菜单Fragment

package com.infzm.slidingmenu.demo.fragment;

import android.app.Activity;

import android.os.Bundle;

import android.support.v4.app.Fragment;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup;

import com.infzm.slidingmenu.demo.MainActivity;

import com.infzm.slidingmenu.demo.R;

/**

  • @date 2014/11/14

  • @author wuwenjie

  • @description 侧边栏菜单

*/

public class LeftFragment extends Fragment implements OnClickListener{

private View todayView;

private View lastListView;

private View discussView;

private View favoritesView;

private View commentsView;

private View settingsView;

1/14

  • @author wuwenjie

  • @description 侧边栏菜单

*/

public class LeftFragment extends Fragment implements OnClickListener{

private View todayView;

private View lastListView;

private View discussView;

private View favoritesView;

private View commentsView;

private View settingsView;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值