| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx

求解:Android slidemenu 左滑菜单框架怎么监听菜单项的点击事件?

0
Android Java C/C++ list ico 15724 次浏览

如题,测试了两天了没有具体结果

/**

 * 菜单控件的

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)));
			}
		};
	}
	
	
	
}


16个答案

0

Hi! This is my first visit to your blog! We are a group of volunteers and starting a new project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a extraordinary job! Tips on Selling Merchant Services





===============================================================





You really make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complex and extremely broad for me. I am looking forward for your next post, I will try to get the hang of it! Merchant Services Reseller





=================================================================







Excellent beat ! I wish to apprentice while you amend your web site, how can i subscribe for a blog site? The account helped me a acceptable deal. I had been a little bit acquainted of this your broadcast provided bright clear idea Starting a Merchant Services Company

0

Write My Dissertation in an ideal manner demands massive endeavors for which students often require Dissertation Help from domain specialists who have got professional experience in Dissertation Writing Service. Great Assignment Helper offers the much needed and reliable help, free of plagiarism and grammatical errors, at the cheapest price.

 

0

ดาวน์โหลดjoker123 auto Online slots, mobile phones, new online slots games, online slots that are the hottest in 2020, with low capital, can play.

0

ทางเข้า joker123 Top camp slots games Genuine license from PGSLOT game camp, online slots, direct web slot game service.

0

 joker PG SLOT Game is a new and hot slot game that is gaining popularity right now, online slots, direct websites, not through agents.

0

เว็บตรง สล็อต PG SLOT online slots games A game that is popular with players all over the world right now. Your slots play will never be boring again.

0

I wanna know more about this because this is really informative!

audiologist fresno ca

0

I highly appreciate your work here! Thank you so much!

hearing aid repair baton rouge

0

I love your content! Great job!  hearing specialist jacksonville fl

0
0

以一种有趣的方式感谢您这样一篇非常有用的文章。阅读这篇文章非常有趣。ทางเข้าjoker

0

With the help of skilled personal statement writers, you can save time and get even better results! Only unique writing, on-time delivery & 24/7 support!

0

Xo สล็อตออนไลน์ โปรสล็อต XO เกมออนไลน์ทำเงินยอดฮิตเกมสล็อต xopg.net คือเกมทำเงิน reeffutures2018 ผ่านทางออนไลน์อย่างหนึ่ง ที่เล่นง่าย และได้เงินไว แถมยังลงทุนด้วยเงินน้อย mavoixtavoie ทำเงินได้ตลอดเวลา ซึ่งหลายคนอาจได้เคยเห็นรีวิวเรื่องของ สล็อต xo สล็อตออนไลน์ ไว้มากมาย เทคนิคสล็อต ทั้งเรื่องการเล่นแล้วได้เงิน herbalpertpresents และเล่น สล็อต แล้วไม่ได้เงิน นั่นเองค่ะ ซึ่งการที่คุณจะเล่นได้เงินหรือไม่ได้เงินนั้น essentialsforasoul ส่วนหนึ่งก็เป็นในเรื่องของดวงเข้ามาเกี่ยวด้วย northbristol เพราะสล็อตเป็นเกมออนไลน์เสี่ยงโชค ทดลองเล่น xo เกมหนึ่งซึ่งจะมีสูตร หรือเทคนิคเข้ามาช่วย gclub เพื่อโกงดวงอยู่เสมอซึ่งในเว็บของเรา สมัคร xo ก็มีมาแนะนำไว้ให้เห็นกันมากมายหลายสูตร

0

求解:Android slidemenu 左滑菜单框架怎么监听菜单项的点击事件? สล็อตวอเลท 

0

The programming code which you have mentioned is not practically understandable for the beginner level programmer like me. Finding custom dissertation writing services is not that easy nowadays. I would appreciate it if you could explain this programming code for simple individuals.

0

学学知识