Android UI实现SlidingMenu侧滑菜单效果

作者:IT_xiao小巫 时间:2021-12-08 16:03:34 

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

 Android UI实现SlidingMenu侧滑菜单效果

Android UI实现SlidingMenu侧滑菜单效果

主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) {
   switch (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;

@Override
 public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
 }

@Override
 public void onAttach(Activity activity) {
   super.onAttach(activity);
 }

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
     Bundle savedInstanceState) {
   View view = inflater.inflate(R.layout.layout_menu, null);
   findViews(view);

return view;
 }

public void findViews(View view) {
   todayView = view.findViewById(R.id.tvToday);
   lastListView = view.findViewById(R.id.tvLastlist);
   discussView = view.findViewById(R.id.tvDiscussMeeting);
   favoritesView = view.findViewById(R.id.tvMyFavorites);
   commentsView = view.findViewById(R.id.tvMyComments);
   settingsView = view.findViewById(R.id.tvMySettings);

todayView.setOnClickListener(this);
   lastListView.setOnClickListener(this);
   discussView.setOnClickListener(this);
   favoritesView.setOnClickListener(this);
   commentsView.setOnClickListener(this);
   settingsView.setOnClickListener(this);
 }

@Override
 public void onDestroyView() {
   super.onDestroyView();
 }

@Override
 public void onDestroy() {
   super.onDestroy();
 }

@Override
 public void onClick(View v) {
   Fragment newContent = null;
   String title = null;
   switch (v.getId()) {
   case R.id.tvToday: // 今日
     newContent = new TodayFragment();
     title = getString(R.string.today);
     break;
   case R.id.tvLastlist:// 往期列表
     newContent = new LastListFragment();
     title = getString(R.string.lastList);
     break;
   case R.id.tvDiscussMeeting: // 讨论集会
     newContent = new DiscussFragment();
     title = getString(R.string.discussMeetting);
     break;
   case R.id.tvMyFavorites: // 我的收藏
     newContent = new MyFavoritesFragment();
     title = getString(R.string.myFavorities);
     break;
   case R.id.tvMyComments: // 我的评论
     newContent = new MyCommentsFragment();
     title = getString(R.string.myComments);
     break;
   case R.id.tvMySettings: // 设置
     newContent = new MySettingsFragment();
     title = getString(R.string.settings);
     break;
   default:
     break;
   }
   if (newContent != null) {
     switchFragment(newContent, title);
   }
 }

/**
  * 切换fragment
  * @param fragment
  */
 private void switchFragment(Fragment fragment, String title) {
   if (getActivity() == null) {
     return;
   }
   if (getActivity() instanceof MainActivity) {
     MainActivity fca = (MainActivity) getActivity();
     fca.switchConent(fragment, title);
   }
 }

}

上面是核心代码,引入SlidingMenu开源库。

来源:http://blog.csdn.net/wwj_748/article/details/41355459

标签:Android,SlidingMenu,侧滑菜单
0
投稿

猜你喜欢

  • java 指定某个jdk版本方法

    2023-10-20 08:07:28
  • SpringCloud用Zookeeper搭建配置中心的方法

    2022-12-17 18:16:18
  • Java 中 hashCode() 与 equals() 的关系(面试)

    2023-08-29 18:03:57
  • Java中实现文件预览的功能(实例代码)

    2023-09-09 16:13:06
  • C#异步编程由浅入深(一)

    2023-12-16 07:32:29
  • Jmeter post上传文件实现过程详解

    2022-01-09 14:02:28
  • 使用java生成字母验证码

    2021-10-29 23:50:25
  • 解决WPF附加属性的Set函数不调用的问题

    2022-09-27 09:52:38
  • Unity3D实现甜品消消乐游戏

    2022-11-06 16:26:24
  • Android实现未读消息小红点显示实例

    2022-05-18 07:57:27
  • SpringBoot如何通过devtools实现热部署

    2022-07-23 10:04:10
  • C#调用Windows的API实现窗体动画

    2022-09-09 13:05:22
  • Java线程让步_动力节点Java学院整理

    2021-07-21 14:44:52
  • 简单的观察者模式示例分享

    2023-02-11 12:52:12
  • C#四舍五入(函数)用法实例

    2022-02-03 21:51:16
  • C++ boost scoped_ptr智能指针详解

    2023-10-04 19:11:45
  • Android编程实现为ListView创建上下文菜单(ContextMenu)的方法

    2021-12-09 06:26:28
  • Java并发编程之LockSupport类详解

    2022-08-21 10:38:36
  • 关于mybatis if else if 条件判断SQL片段表达式取值和拼接问题

    2023-02-04 18:51:21
  • C#对Word文档的创建、插入表格、设置样式等操作实例

    2021-10-24 23:13:46
  • asp之家 软件编程 m.aspxhome.com