MainActivity.java

public class MainActivity extends Activity implements OnClickListener{

	private PopMenu popMenu;
private Context context;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
context = MainActivity.this;
button=(Button)findViewById(R.id.bt);
button.setOnClickListener(this);
popMenu = new PopMenu(context);
popMenu.setOnItemClickListener( new OnItemClickListener() {//每个条目的点击事件
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//每个条目的点击响应事件
popMenu.dismiss();
}
} ); } // 弹出菜单监听器 @Override
public void onClick(View v) {
popMenu.showAsDropDown(v); }
}

PopMenu.java

public class PopMenu {
private ArrayList<String> itemListStr ;
private ArrayList<Integer> itemListInt;
private Context context;
private PopupWindow popupWindow;
private ListView listView; public PopMenu(Context context) {
this.context = context;
itemListStr = new ArrayList<String>();
itemListInt=new ArrayList<Integer>();
String[] str=new String[]{"你好", "我好", "大家好", "哈哈"};
int[] i=new int[]{R.drawable.picture0,R.drawable.picture1,R.drawable.picture2,R.drawable.picture3};
for(String s:str){
itemListStr.add(s);
}
for(int img:i){
Integer listImgInteger=Integer.valueOf(img);//int-->Integer
itemListInt.add(listImgInteger);
}
Log.i("Tag","itemListStr______"+itemListStr);
Log.i("Tag","itemListInt________"+itemListInt);
View view = LayoutInflater.from(context).inflate(R.layout.poplist, null);//PopupWindow里面的view
listView = (ListView) view.findViewById(R.id.menu_listview);
listView.setAdapter(new PopMenuAdapter(context,itemListStr,itemListInt)); listView.setFocusableInTouchMode(true);
listView.setFocusable(true);
popupWindow = new PopupWindow(view, 300, LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable()); // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的),
} // 设置每个条目击监听事件,前面会调用这个方法
public void setOnItemClickListener(OnItemClickListener listener) {
listView.setOnItemClickListener(listener);
} // 下拉式 弹出 pop菜单 parent 右下角
public void showAsDropDown(View parent) {
popupWindow.showAsDropDown(parent,-150,0);//showAsDropDown(View anchor, int xoff, int yoff)弹出对话框,位置在紧挨着view组件,x y 代表着偏移量
popupWindow.setFocusable(true);//必须要获得焦点哦,不然里面的控件啥的点击无效的哦
popupWindow.setOutsideTouchable(true);// 设置允许在外点击消失
popupWindow.update(); // 刷新状态
} // 隐藏菜单
public void dismiss() {
popupWindow.dismiss();
}
}

PopMenuAdapter.java

public class PopMenuAdapter extends BaseAdapter {
ArrayList<String> itemListS;
ArrayList<Integer> itemListI;
Context mcon;
public PopMenuAdapter(Context mcon,ArrayList<String> itemListStr,ArrayList<Integer> itemListImg){
this.mcon=mcon;
this.itemListS=itemListStr;
this.itemListI=itemListImg;
} @Override
public int getCount() {
return itemListS.size();
} @Override
public Object getItem(int position) {
return itemListS.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mcon).inflate(R.layout.showpoplist, null);
holder = new ViewHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.img);
holder.groupItem = (TextView) convertView.findViewById(R.id.textview);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setBackgroundResource(itemListI.get(position).intValue());//Integer-->int
holder.groupItem.setText(itemListS.get(position));
Log.i("Tag","itemListS.get(position)~~~~~~"+itemListS.get(position));
Log.i("Tag","itemListI.get(position).intValue()~~~~~"+itemListI.get(position).intValue());
return convertView;
} private final class ViewHolder {
ImageView imageView;
TextView groupItem;
}
}

两个布局文件,一个是PopMenu的填充的view,里面就是ListView;另一个布局文件是ListView每个条目的布局。

poplist.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="20dip"
android:gravity="right"
android:id="@+id/hotalk_menu_view_layout" >
<!-- 显示的listview -->
<ListView android:id="@+id/menu_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:longClickable="true"
android:scrollbarSize="0sp"
android:scrollbarStyle="insideOverlay"
android:background="@drawable/menubg"
android:divider="@drawable/group_divider"
android:dividerHeight="1px"
android:cacheColorHint="#00000000">
</ListView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
> <!-- 显示内容 ,每个条目-->
<ImageView
android:id="@+id/img"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/textview"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_toRightOf="@id/img"
android:layout_alignBottom="@id/img"
android:textSize="25sp"
android:textColor="#FF33CC" /> </RelativeLayout>

弹出式菜单PopMenu的更多相关文章

  1. Swift实现封装PopMenu菜单,可在屏幕任意位置弹出

    效果图: 说明: 代码现已支持 Swift3 语法 使用介绍: 1.初始化位置 //frame 为整个popview相对整个屏幕的位置 箭头距离右边位置,默认15 //popMenu = SwiftP ...

  2. C++ Button右键弹出式菜单

    Button右键弹出式菜单 关键点 用类来实现 的 实现过程 新建1个类  类名CButtonPopMenu 基类CButton 新建1个菜单资源 IDR_MENU1 // ButtonPopMenu ...

  3. pb popmenu弹出式选单位置的问题

    在主界面上使用m_main.m_title.PopMenu(PointX(),PointY()),可以正确定位弹出式选单的位置: 在主界面的控件智商为使用m_main.m_title.PopMenu( ...

  4. Android学习总结——Popup menu:弹出式菜单

    PopupMenu,弹出菜单,一个模态形式展示的弹出风格的菜单,绑在在某个View上,一般出现在被绑定的View的下方(如果下方有空间). 注意:弹出菜单是在API 11和更高版本上才有效的. 核心步 ...

  5. 手动添加PopMenu出现的问题

    最近在github上找了个比较酷的弹出菜单PopMenu用来做分享页面,demo下载运行OK,于是拖进项目中,一编译就呵呵了.... 一看demo目录,原来这还还引用了其他库,于是把pod下面的pop ...

  6. 弹出式菜单(下拉菜单)实现——PopupMenu

    PopupMenu代表弹出式菜单,它会在指定组件上弹出PopupMenu,默认情况下,PopupMenu会显示在该组件的下方或上方.PopupMenu可增加多个菜单项,并可为菜单项增加子菜单. 使用P ...

  7. 使用PopupMenu创建弹出式菜单

    PopupMenu代表弹出式菜单,它会在指定组件上弹出PopupMenu,默认情况下,PopupMenu会显示在该组件的下方或上方.PopupMenu可增加多个菜单项,并可为菜单项增加子菜单. 使用P ...

  8. css3动画:弹出式菜单

    css3动画:弹出式菜单 今天主要来讲讲transition和transform结合做的动画,会举一些现在(2017年)常见的动画例子. 注:本人也接触css3不久,如果写的有纰漏请指出,不喜勿喷. ...

  9. 高级组件——弹出式菜单JPopupMenu

    弹出式菜单JPopupMenu,需要用到鼠标事件.MouseListener必须要实现所有接口,MouseAdapter是类,只写你关心的方法,即MouseAdapter实现了MouseListene ...

随机推荐

  1. loj1341(数学)

    传送门:Aladdin and the Flying Carpet 题意: 给出两个正整数1<=m<=n<=1e12.问N可以拆成多少对p*q,使得p和q中最小的不小于a,且p!=q ...

  2. Linux 系统文件夹结构

    登录后,在当前的命令窗口中输入命令: <span style="font-size:14px;">ls /</span> 你的终端上会出现例如以下的代码: ...

  3. hdu3811(状态压缩dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3811 题目大意:给定1~N个数,求出至少满足一个条件的排列总数.M个条件如下:Ai位置的数为Bi 分析 ...

  4. 【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】

    传送门:pid=4888">[HDU]4888 Redraw Beautiful Drawings 题目分析: 比赛的时候看出是个网络流,可是没有敲出来.各种反面样例推倒自己(究其原因 ...

  5. Win7+ubuntu kylin+CentOS 6.5三系统安装图文教程

    Win7+ubuntu kylin+CentOS 6.5三系统安装图文教程 引言:原本机子上已经装好了win7+Ubuntu Kylin 由win7引导,而不是Ubuntu的grub引导的双系统(安装 ...

  6. html+css实现登录界面

    <!DOCTYPE html> <style type="text/css"> body{ background-color: #555555; } #ti ...

  7. Cocos2d-x中父节点scale对子节点的影响

    背景:在前几天,刚接触cocos2d-x,随便找了一张图,作为一个CCSprite,而且设置了scale属性,然后在这个sprite上创建了一个CCLabelTTF,并用sprite->addC ...

  8. IOS开发笔记 - 基于wsdl2objc调用webservice

    为了方便在ios下调用webserivce,找来了wsdl2objc这样一个开源的框架来解析webservice方便在ios下引用. 下面做个小例子. 1.首先是用Asp.net搭建一个测试的webs ...

  9. struts(二)——struts框架实现的基本原理

    上一篇文章,我们介绍了MVC框架的基本原理,并指出了这个基本框架中存在大量if…else的问题.今天我们来介绍一下struts框架,让struts解决这个问题. 首先,看一下粗略的时序图: Actio ...

  10. oracle之spool详细使用总结(转)

    今天实际项目中用到了spool,发现网上好多内容不是很全,自己摸索了好半天,现在总结一下. 一.通过spool 命令,可以将select 数据库的内容写到文件中,通过在sqlplus设置一些参数,使得 ...