别的不多说先上图吧
以前做这样的功能,我都是自定义一个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();
}
};
}
最后附上源码下载地址:
http://download.csdn.net/detail/duanyu218/5045677