Android slidemenu 左滑菜单框架怎么监听菜单项的点击事件
1个回答
展开全部
package com.qm.test;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.CanvasTransformer;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Interpolator;
import android.widget.TextView;
import android.widget.Toast;
/*
* 主页面包装菜单、返回动画、退出事件的包装类
*/
public class ListActivityMode extends SlidingFragmentActivity {
private CanvasTransformer mTransformer;
private SlidingMenu sm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initSlidingMenu(this);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
/*
* 当主页面不可见时关闭菜单
* @see android.support.v4.app.FragmentActivity#onStop()
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
if(sm.isMenuShowing()){
sm.toggle();
}
super.onStop();
}
private long exitTime = 0;
/*
* 双击返回按钮退出
* @see android.support.v4.app.FragmentActivity#onKeyDown(int, android.view.KeyEvent)
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == keyCode) {
// 判断是否在两秒之内连续点击返回键,是则退出,否则不退出
if (System.currentTimeMillis() - exitTime > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
// 将系统当前的时间赋值给exitTime
exitTime = System.currentTimeMillis();
} else {
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
/*
* 菜单初始化程序
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 初始化滑动菜单
*/
public void initSlidingMenu(SlidingFragmentActivity activity) {
//初始化动画效果
initAnimation(activity);
// 设置主界面视图
// activity.setContentView(R.layout.activity_main);
// 设置滑动菜单视图
activity.setBehindContentView(R.layout.menu_frame);
activity.getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, new SampleListFragment()).commit();
// 设置滑动菜单的属性值
sm = activity.getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setBehindScrollScale(0.0f);
sm.setBehindCanvasTransformer(mTransformer);
activity.setSlidingActionBarEnabled(true);
}
private Interpolator interp = new Interpolator() {
@Override
public float getInterpolation(float t) {
t -= 1.0f;
return t * t * t + 1.0f;
}
};
/**
* 初始化动画效果
*/
private void initAnimation(SlidingFragmentActivity activity) {
mTransformer = new CanvasTransformer() {
@Override
public void transformCanvas(Canvas canvas, float percentOpen) {
canvas.translate(0, canvas.getHeight() * (1 - interp.getInterpolation(percentOpen)));
}
};
}
}
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu.CanvasTransformer;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Interpolator;
import android.widget.TextView;
import android.widget.Toast;
/*
* 主页面包装菜单、返回动画、退出事件的包装类
*/
public class ListActivityMode extends SlidingFragmentActivity {
private CanvasTransformer mTransformer;
private SlidingMenu sm;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initSlidingMenu(this);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
/*
* 当主页面不可见时关闭菜单
* @see android.support.v4.app.FragmentActivity#onStop()
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
if(sm.isMenuShowing()){
sm.toggle();
}
super.onStop();
}
private long exitTime = 0;
/*
* 双击返回按钮退出
* @see android.support.v4.app.FragmentActivity#onKeyDown(int, android.view.KeyEvent)
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (KeyEvent.KEYCODE_BACK == keyCode) {
// 判断是否在两秒之内连续点击返回键,是则退出,否则不退出
if (System.currentTimeMillis() - exitTime > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
// 将系统当前的时间赋值给exitTime
exitTime = System.currentTimeMillis();
} else {
finish();
System.exit(0);
}
return true;
}
return super.onKeyDown(keyCode, event);
}
/*
* 菜单初始化程序
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* 初始化滑动菜单
*/
public void initSlidingMenu(SlidingFragmentActivity activity) {
//初始化动画效果
initAnimation(activity);
// 设置主界面视图
// activity.setContentView(R.layout.activity_main);
// 设置滑动菜单视图
activity.setBehindContentView(R.layout.menu_frame);
activity.getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame, new SampleListFragment()).commit();
// 设置滑动菜单的属性值
sm = activity.getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setBehindScrollScale(0.0f);
sm.setBehindCanvasTransformer(mTransformer);
activity.setSlidingActionBarEnabled(true);
}
private Interpolator interp = new Interpolator() {
@Override
public float getInterpolation(float t) {
t -= 1.0f;
return t * t * t + 1.0f;
}
};
/**
* 初始化动画效果
*/
private void initAnimation(SlidingFragmentActivity activity) {
mTransformer = new CanvasTransformer() {
@Override
public void transformCanvas(Canvas canvas, float percentOpen) {
canvas.translate(0, canvas.getHeight() * (1 - interp.getInterpolation(percentOpen)));
}
};
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询