使用PopupWindow布置复合菜单

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

MainActivity:

package com.home.testpopwindow;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class MainActivity extends Activity {
	private List<String> titles; // 标题栏
	private List<List<String>> item_names; // 选项名称
	private List<List<Integer>> item_images; // 选项图标
	private MyPopupWindow myPop; // 弹出菜单

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 菜单标题栏
		titles = addItems(new String[] { "菜单一", "菜单二", "菜单三" });
		// 选项图标
		item_images = new ArrayList<List<Integer>>();
		item_images.add(addItems(new Integer[] { R.drawable.bag,
				R.drawable.bluetooth, R.drawable.earth, R.drawable.email }));
		item_images.add(addItems(new Integer[] { R.drawable.map,
				R.drawable.news, R.drawable.reader, R.drawable.sound,
				R.drawable.tape }));
		item_images.add(addItems(new Integer[] { R.drawable.telephone,
				R.drawable.bluetooth, R.drawable.earth, R.drawable.email }));
		// 选项名称
		item_names = new ArrayList<List<String>>();
		item_names.add(addItems(new String[] { "购物", "蓝牙", "游览器", "邮件" }));
		item_names
				.add(addItems(new String[] { "地图", "新闻", "阅读器", "音箱", "录音" }));
		item_names.add(addItems(new String[] { "电话", "蓝牙", "阅读器", "邮箱" }));
		// 创建弹出菜单对象
		myPop = new MyPopupWindow(this, titles, item_names, item_images,
				new ItemClickEvent());

	}

	/**
	 * 将字符串数组转换为字符串的集合
	 * 
	 * @param strs
	 *            字符串数组
	 * @return list 字符串的集合
	 */
	private List<String> addItems(String[] strs) {
		List<String> list = new ArrayList<String>();
		for (String str : strs) {
			list.add(str);
		}
		return list;
	}

	/**
	 * 将Integer的数组转换为Integer的集合
	 * 
	 * @param values
	 *            整型数组
	 * @return list 整型集合
	 */
	private List<Integer> addItems(Integer[] values) {
		List<Integer> list = new ArrayList<Integer>();
		for (Integer var : values) {
			list.add(var);
		}
		return list;
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		if (0 == myPop.currentState && myPop.isShowing()) {
			myPop.dismiss(); // 对话框消失
			myPop.currentState = 1; // 标记状态,已消失
		} else {
			myPop.showAtLocation(findViewById(R.id.layout), Gravity.BOTTOM, 0,
					0);
			myPop.currentState = 0; // 标记状态,显示中
		}
		return false;// true显示系统自带菜单;false屏蔽系统自带菜单。
	}

	/**
	 * 菜单选项点击事件
	 * 
	 * @author Administrator
	 * 
	 */
	class ItemClickEvent implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// 显示点击的是哪个菜单哪个选项。
			Toast.makeText(
					MainActivity.this,
					"Menu: " + titles.get(myPop.getTitleIndex()) + " Item: "
							+ item_names.get(myPop.getTitleIndex()).get(arg2),
					Toast.LENGTH_SHORT).show();
			myPop.dismiss(); // 菜单消失
			myPop.currentState = 1; // 标记状态,已消失
		}

	}

}

MyPopupWindow:

package com.home.testpopwindow;

import java.util.List;
import com.home.testpopwindow.MainActivity.ItemClickEvent;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

public class MyPopupWindow extends PopupWindow {
	private LinearLayout layout; // 总的布局
	private GridView gv_title; // 菜单栏
	private GridView gv_body; // 选项视图
	private BodyAdatper[] bodyAdapter; // 选项适配器
	private TitleAdatper titleAdapter; // 标题适配器
	private Context context; // 上下文
	private int titleIndex; // 菜单序号
	public int currentState; // 对话框状态:0--显示中、1--已消失、2--失去焦点

	public MyPopupWindow(Context context, List<String> titles,
			List<List<String>> item_names, List<List<Integer>> item_images,
			ItemClickEvent itemClickEvent) {
		super(context);
		this.context = context;
		currentState = 1;

		// 布局框架
		layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));

		// 菜单标题栏
		titleIndex = 0;
		gv_title = new GridView(context);
		titleAdapter = new TitleAdatper(context, titles);
		gv_title.setAdapter(titleAdapter);
		gv_title.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		gv_title.setNumColumns(titles.size()); // 菜单个数
		gv_title.setBackgroundColor(Color.WHITE);

		// 选项视图
		bodyAdapter = new BodyAdatper[item_names.size()]; // 各个视图适配器
		for (int i = 0; i < item_names.size(); i++) {
			bodyAdapter[i] = new BodyAdatper(context, item_names.get(i),
					item_images.get(i));
		}
		gv_body = new GridView(context);
		gv_body.setNumColumns(4); // 每行显示4个选项
		gv_body.setBackgroundColor(Color.TRANSPARENT);
		gv_body.setAdapter(bodyAdapter[0]); // 设置适配器

		// 菜单项切换
		gv_title.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				titleIndex = arg2; // 记录当前选中菜单项序号
				titleAdapter.setFocus(arg2);
				gv_body.setAdapter(bodyAdapter[arg2]); // 改变选项视图

			}
		});
		// 设置选项点击事件
		gv_body.setOnItemClickListener(itemClickEvent);

		// 添加标题栏和选项
		layout.addView(gv_title);
		layout.addView(gv_body);

		// 添加菜单视图
		this.setContentView(layout);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应

	}

	/**
	 * 获取当前选中菜单项
	 * 
	 * @return 菜单项序号
	 */
	public int getTitleIndex() {
		return titleIndex;
	}

}

TitleAdatper(标题栏适配器):

package com.home.testpopwindow;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

public class TitleAdatper extends BaseAdapter {
	private List<String> titles;
	private Context context;
	private final TextView[] tv_titels;

	public TitleAdatper(Context context, List<String> titles) {
		this.context = context;
		this.titles = titles;
		tv_titels = new TextView[titles.size()];
	}

	@Override
	public int getCount() {
		return titles.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	/**
	 * 选中后,改变菜单颜色。
	 * 
	 * @param position
	 */
	public void setFocus(int position) {
		for (int i = 0; i < titles.size(); i++) {
			tv_titels[i].setBackgroundColor(Color.WHITE);
		}
		tv_titels[position].setBackgroundColor(Color.BLUE);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 菜单栏文字项
		tv_titels[position] = new TextView(context);
		tv_titels[position].setGravity(Gravity.CENTER);
		tv_titels[position].setText(titles.get(position));
		tv_titels[position].setTextSize(18);
		tv_titels[position].setLayoutParams(new GridView.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		return tv_titels[position];
	}

}

BodyAdatper(选项适配器):

package com.home.testpopwindow;

import java.util.List;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BodyAdatper extends BaseAdapter {
	
	private List<String> item_names;
	private List<Integer> item_images;
	private Context context;

	public BodyAdatper(Context context, List<String> item_names,
			List<Integer> item_images) {
		this.context = context;
		this.item_names = item_names;
		this.item_images = item_images;
	}

	@Override
	public int getCount() {
		return item_images.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		//总布局
		LinearLayout layout = new LinearLayout(context);
		layout.setOrientation(LinearLayout.VERTICAL);
		layout.setGravity(Gravity.CENTER);
		//选项名称
		TextView tv_item = new TextView(context);
		tv_item.setGravity(Gravity.CENTER);
		tv_item.setLayoutParams(new GridView.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		tv_item.setText(item_names.get(position));
		//选项图表
		ImageView img_item = new ImageView(context);
		img_item.setLayoutParams(new LayoutParams(50, 50));
		img_item.setImageResource(item_images.get(position));
		//添加选项图标和名字
		layout.addView(img_item);
		layout.addView(tv_item);
		
		return layout;
	}

}

配图:




 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值