求解:Android slidemenu 左滑菜单框架怎么监听菜单项的点击事件?
0
如题,测试了两天了没有具体结果
/**
* 菜单控件的
package com.qm.test; import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; /** * 菜单控件的生成类 * * @author Administrator * */ public class SampleListFragment 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()); // 添加一个菜单项 adapter.add(new SampleItem("MainActivity", android.R.drawable.ic_menu_search, MainActivity.class)); adapter.add(new SampleItem("NextActivity", android.R.drawable.ic_menu_search, NextActivity.class)); setListAdapter(adapter); } public class SampleItem { public String tag; public int iconRes; public Class cls; public SampleItem(String tag, int iconRes, Class<? extends SlidingFragmentActivity> cls) { this.tag = tag; this.iconRes = iconRes; this.cls = cls; } } public class SampleAdapter extends ArrayAdapter<SampleItem> { Context context; public SampleAdapter(Context context) { super(context, 0); this.context = context; } public View getView(final int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.row, 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); if (((Activity) context).getClass() == getItem(position).cls) { convertView.setBackgroundColor(Color.parseColor("#E8F2FE")); } convertView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: v.setBackgroundColor(Color.parseColor("#50BCEE")); break; case MotionEvent.ACTION_UP: v.setBackgroundColor(Color.parseColor("#E8F2FE")); break; } return false; } }); // convertView.setOnClickListener(new OnClickListener() { // // @Override // public void onClick(View v) { // // TODO Auto-generated method stub // Intent intent = new Intent(); // intent.setClass(context, getItem(position).cls); // startActivity(intent); // ((Activity) context).overridePendingTransition(R.anim.in_from_right, R.anim.out_from_left); // } // // }); return convertView; } } }
在这里添加了菜单项 我要在activity中监听菜单项的点击事件该怎么做?
activity代码:
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))); } }; } }