昨天和同事一起测试了公司的产品并且将bug记录下来建立了一张bug更新表,下午的时候自己就在研究popwindow,http://blog.csdn.net/familyhappylife/article/details/8562177;
以前做这样的功能,我都是自定义一个LinerLayout,然后控制它的隐藏和显示,还需要添加弹出和隐藏的动画效果。另外还需要处理点击空白处和返回该键下拉菜单自动关闭。今天在eoe上无意看到popwindow也能实现这样的功能,而且根本不需要处理动画效果和空白处、返回键点击自动关闭的功能。哎,看来我真的out了,直接上代码: 这里是自定义的PopMenu 类,里面对popwindow进行了封装,实现下拉菜单的效果 package com.su.testpopwindow; import java.util.ArrayList; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import com.su.testpopwindow.R; public class PopMenu { private ArrayList<String> itemList; private Context context; private PopupWindow popupWindow; private ListView listView; public PopMenu(Context context) { this.context = context; itemList = new ArrayList<String>(); View view = LayoutInflater.from(context) .inflate(R.layout.popmenu, null); // 设置 listview listView = (ListView) view.findViewById(R.id.listView); listView.setAdapter(new PopAdapter()); listView.setFocusableInTouchMode(true); listView.setFocusable(true); popupWindow = new PopupWindow(view, 100, LayoutParams.WRAP_CONTENT); popupWindow = new PopupWindow(view, context.getResources() .getDimensionPixelSize(R.dimen.popmenu_width), LayoutParams.WRAP_CONTENT); // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景(很神奇的) popupWindow.setBackgroundDrawable(new BitmapDrawable()); } // 设置菜单项点击监听器 public void setOnItemClickListener(OnItemClickListener listener) { listView.setOnItemClickListener(listener); } // 批量添加菜单项 public void addItems(String[] items) { for (String s : items) { itemList.add(s); } } // 单个添加菜单项 public void addItem(String item) { itemList.add(item); } // 下拉式 弹出 pop菜单 parent 右下角 public void showAsDropDown(View parent) { popupWindow.showAsDropDown(parent, 10, // 保证尺寸是根据屏幕像素密度来的 context.getResources().getDimensionPixelSize( R.dimen.popmenu_yoff)); // 使其聚集 popupWindow.setFocusable(true); // 设置允许在外点击消失 popupWindow.setOutsideTouchable(true); // 刷新状态 popupWindow.update(); } // 隐藏菜单 public void dismiss() { popupWindow.dismiss(); } // 适配器 private final class PopAdapter extends BaseAdapter { @Override public int getCount() { return itemList.size(); } @Override public Object getItem(int position) { return itemList.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(context).inflate( R.layout.pomenu_item, null); holder = new ViewHolder(); convertView.setTag(holder); holder.groupItem = (TextView) convertView .findViewById(R.id.textView); } else { holder = (ViewHolder) convertView.getTag(); } holder.groupItem.setText(itemList.get(position)); return convertView; } private final class ViewHolder { TextView groupItem; } } } 这是主activity,点击button,弹出下拉菜单 package com.su.testpopwindow; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.AdapterView.OnItemClickListener; public class TestPullPopWindowActivity extends Activity { private PopMenu popMenu; private Context context; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); context = TestPullPopWindowActivity.this; popMenu = new PopMenu(context); popMenu.addItems(new String[] { "停发", "延期", "删除", "备注", "修改","我们" }); // 菜单项点击监听器 popMenu.setOnItemClickListener(popmenuItemClickListener); Button button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popMenu.showAsDropDown(v); } }); } // 弹出菜单监听器 OnItemClickListener popmenuItemClickListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { System.out.println("下拉菜单点击" + position); popMenu.dismiss(); } }; }
不过他的代码有点问题:点击itme停发时,没有调用点击事件!
原因:xml中itme没有获得焦点。解决:只需修改一下popmeun_itme.xml中的几条代码.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:descendantFocusability="blocksDescendants" android:orientation="vertical" > <TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/bg_pop_item" android:drawablePadding="3.0dip" android:gravity="center_vertical" android:padding="7.0dip" android:text="停发" android:textColor="#FFFFFF" android:textSize="18.0sp" /> </LinearLayout>
android:descendantFocusability该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点