SlidingMenu是眼下在应用程序上非常流行的一种UI技术。能够实现一种比較炫的滑动效果,SlidingMenu是Git上托管的一个项目,开源免费的。SlidingMenu作为一个Library的形式导入到须要的project。兴许可能会以不同的方式实现SlidingMenu;用户能够到下面地址下载。
SlidingMenu的Git项目托管地址:https://github.com/jfeinstein10/SlidingMenu
Demo文件夹结构:
关键代码:
MainActivity.java
package com.dzt.slidingmenudemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.slidingmenu.lib.SlidingMenu;
public class MainActivity extends FragmentActivity {
private SlidingMenu mChannelMenu;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 设置标题栏的标题
setTitle("測试SlidingMenu");
setContentView(R.layout.main_activity);
initChannelMenu();
}
private void initChannelMenu() {
// 创建SlidingMenu对象
mChannelMenu = new SlidingMenu(this);
// 设置側滑栏菜单位置,这里在左边。拉动菜单时。会从左边弹出
mChannelMenu.setMode(SlidingMenu.LEFT);
// 设置触摸的范围,这里设置全屏
mChannelMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// 设置阴影的宽度,查看上面第二张效果图,靠右边的位置,有一个阴影过渡。就是这个东西
mChannelMenu.setShadowWidthRes(R.dimen.shadow_width);
// 这里是阴影效果。能够设置图片或者一个颜色过渡
mChannelMenu.setShadowDrawable(R.drawable.shadow);
// 设置后面间距,側滑栏和原来界面间距
mChannelMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
// 边框的角度。这里指边界地方
mChannelMenu.setFadeDegree(0.35f);
// 设置触摸屏幕的模式
mChannelMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// 把側滑栏关联到当前的Activity
mChannelMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
// 側滑栏的布局文件
mChannelMenu.setMenu(R.layout.channel_slide_menu);
getSupportFragmentManager().beginTransaction()
.replace(R.id.menu_frame, new MyListFragment()).commit();
}
@Override
public void onBackPressed() {
if (mChannelMenu.isMenuShowing()) {
// 隐藏SlidingMenu,这里的Content就是我们的主Activity
mChannelMenu.showContent();
} else {
super.onBackPressed();
}
}
}
MyListFragment.java
package com.dzt.slidingmenudemo;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListFragment extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
SampleAdapter adapter = new SampleAdapter(getActivity());
for (int i = 0; i < 20; i++) {
adapter.add(new SampleItem("左边是列表",
android.R.drawable.ic_menu_search));
}
setListAdapter(adapter);
}
private class SampleItem {
public String tag;
public int iconRes;
public SampleItem(String tag, int iconRes) {
this.tag = tag;
this.iconRes = iconRes;
}
}
public class SampleAdapter extends ArrayAdapter<SampleItem> {
public SampleAdapter(Context context) {
super(context, 0);
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.item, null);
}
ImageView icon = (ImageView) convertView
.findViewById(R.id.row_icon);
icon.setImageResource(getItem(position).iconRes);
TextView title = (TextView) convertView
.findViewById(R.id.row_title);
title.setText(getItem(position).tag);
return convertView;
}
}
}
有一点须要注意。在使用的project中会默认生成:android-support-v4.jar。有可能跟SlidingMenu的有冲突。须要删除。否则会报错。
演示样例代码:http://download.csdn.net/detail/deng0zhaotai/7856011
最新的SlidingMenu能够到Git上去下载