本文主要是介绍漂亮的侧滑主页(仿QQ、腾讯QQ首页侧滑效果),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目原地址: https://github.com/SpecialCyCi/AndroidResideMenu
GitHub文档说明:
DEMO
This copy is the demo.
Version Migration
Upgrading tov1.4
fromv1.3
, v1.2
, v1.1
, v1.0
Duplicate the followed code in dispatchTouchEvent() of Activity, replace the olddispatchTouchEvent()
code.
@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {return resideMenu.dispatchTouchEvent(ev);}
Requirements
Run in Android 2.3 +
Installation
- import ResideMenu project to your workspace.
- make it as a dependency library project to your main project.
( see example )
or
If you want to merge ResideMenu with your project, you should follow these steps.
- Copy all files from src/com/special/ResideMenu to your project.
- Copy libs/nineoldandroids-library-2.4.0.jar to your project’s corresponding path: libs/
- Copy res/drawable-hdpi/shadow.9.png to your project’s corresponding path: res/drawable-hdpi/
- Copy res/layout/residemenu.xml and residemenu_item.xml to your project’s corresponding path: res/layout
Usage
init ResideMenu: write these code in Activity onCreate()
// attach to current activity;resideMenu = new ResideMenu(this);resideMenu.setBackground(R.drawable.menu_background);resideMenu.attachToActivity(this);// create menu items;String titles[] = { "Home", "Profile", "Calendar", "Settings" };int icon[] = { R.drawable.icon_home, R.drawable.icon_profile, R.drawable.icon_calendar, R.drawable.icon_settings };for (int i = 0; i < titles.length; i++){ResideMenuItem item = new ResideMenuItem(this, icon[i], titles[i]);item.setOnClickListener(this);resideMenu.addMenuItem(item, ResideMenu.DIRECTION_LEFT); // or ResideMenu.DIRECTION_RIGHT}
If you want to use slipping gesture to operate(lock/unlock) the menu, override this code in Acitivity dispatchTouchEvent() (please duplicate the followed code in dispatchTouchEvent() of Activity.
@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {return resideMenu.dispatchTouchEvent(ev);}
On some occasions, the slipping gesture function for locking/unlocking menu, may have conflicts with your widgets, such as viewpager. By then you can add the viewpager to ignored view, please refer to next chapter – Ignored Views.
open/close menu
resideMenu.openMenu(ResideMenu.DIRECTION_LEFT); // or ResideMenu.DIRECTION_RIGHT
resideMenu.closeMenu();
listen in the menu state
resideMenu.setMenuListener(menuListener);private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {@Overridepublic void openMenu() {Toast.makeText(mContext, "Menu is opened!", Toast.LENGTH_SHORT).show();}@Overridepublic void closeMenu() {Toast.makeText(mContext, "Menu is closed!", Toast.LENGTH_SHORT).show();}};
disable a swipe direction
resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);
Ignored Views
On some occasions, the slipping gesture function for locking/unlocking menu, may have conflicts with your widgets such as viewpager.By then you can add the viewpager to ignored view.
// add gesture operation's ignored viewsFrameLayout ignored_view = (FrameLayout) findViewById(R.id.ignored_view);resideMenu.addIgnoredView(ignored_view);
So that in ignored view’s workplace, the slipping gesture will not be allowed to operate menu.
引用简述:
1. 引用ResideMenu依赖
2. 在Activity中初始化ResideMenu(左侧菜单选择条目、右侧菜单选择条目)
3. 设置ResideMenu的打开和关闭监听
代码示例:
public class MenuActivity extends FragmentActivity implements View.OnClickListener{private ResideMenu resideMenu; //初始化resideMenuprivate MenuActivity mContext;private ResideMenuItem itemHome; //初始化菜单选择选项private ResideMenuItem itemProfile;private ResideMenuItem itemCalendar;private ResideMenuItem itemSettings;/*** Called when the activity is first created.* 当这个activity第一次被创建的时候调用*/@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mContext = this;setUpMenu();changeFragment(new HomeFragment());}/*** 初始化Menu*/private void setUpMenu() {// attach to current activity;resideMenu = new ResideMenu(this);resideMenu.setBackground(R.drawable.menu_background);resideMenu.attachToActivity(this);resideMenu.setMenuListener(menuListener);//valid scale factor is between 0.0f and 1.0f. leftmenu'width is 150dip. resideMenu.setScaleValue(0.6f); //这个API是设置两侧菜单宽度 0.1f~0.4f HomeFragment划出不显示,手势可以滑回//0.5~1.0 值越大两侧菜单显示越小// create menu items;itemHome = new ResideMenuItem(this, R.drawable.icon_home, "Home");itemProfile = new ResideMenuItem(this, R.drawable.icon_profile, "Profile");itemCalendar = new ResideMenuItem(this, R.drawable.icon_calendar, "Calendar");itemSettings = new ResideMenuItem(this, R.drawable.icon_settings, "Settings");itemHome.setOnClickListener(this);itemProfile.setOnClickListener(this);itemCalendar.setOnClickListener(this);itemSettings.setOnClickListener(this);/*设置左侧和右侧选择菜单*/resideMenu.addMenuItem(itemHome, ResideMenu.DIRECTION_LEFT);resideMenu.addMenuItem(itemProfile, ResideMenu.DIRECTION_LEFT);resideMenu.addMenuItem(itemCalendar, ResideMenu.DIRECTION_RIGHT);resideMenu.addMenuItem(itemSettings, ResideMenu.DIRECTION_RIGHT);// You can disable a direction by setting ->// resideMenu.setSwipeDirectionDisable(ResideMenu.DIRECTION_RIGHT);//顶部左上部按钮,点击打开左侧菜单findViewById(R.id.title_bar_left_menu).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {resideMenu.openMenu(ResideMenu.DIRECTION_LEFT);}});//顶部右上部按钮,点击打开右侧隐藏菜单findViewById(R.id.title_bar_right_menu).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {resideMenu.openMenu(ResideMenu.DIRECTION_RIGHT);}});}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {return resideMenu.dispatchTouchEvent(ev);}@Overridepublic void onClick(View view) { //跳转到指定的Fragmentif (view == itemHome){changeFragment(new HomeFragment());}else if (view == itemProfile){changeFragment(new ProfileFragment());}else if (view == itemCalendar){changeFragment(new CalendarFragment());}else if (view == itemSettings){changeFragment(new SettingsFragment());}resideMenu.closeMenu();}/** ResideMenu打开和关闭事件的监听*/private ResideMenu.OnMenuListener menuListener = new ResideMenu.OnMenuListener() {@Overridepublic void openMenu() {Toast.makeText(mContext, "Menu is opened!", Toast.LENGTH_SHORT).show();}@Overridepublic void closeMenu() {Toast.makeText(mContext, "Menu is closed!", Toast.LENGTH_SHORT).show();}};/*** Fragment跳转的方法* @param targetFragment 需要跳转的Fragment */private void changeFragment(Fragment targetFragment){resideMenu.clearIgnoredViewList();getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment, targetFragment, "fragment").setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();}// What good method is to access resideMenu?public ResideMenu getResideMenu(){return resideMenu;}
}
源码下载地址: http://download.csdn.net/detail/u010333409/7851261
这篇关于漂亮的侧滑主页(仿QQ、腾讯QQ首页侧滑效果)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!