效果:
注意事项:
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sg31.popwindow.MainActivity"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:background="#fff"
>
<EditText
android:id="@+id/editText_name"
android:background="#fff"
android:layout_width="150dp"
android:layout_height="40dp"
/>
<ImageView
android:id="@+id/imgView_select"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:layout_alignRight="@id/editText_name"
android:background="@drawable/downarrow"
/>
</RelativeLayout>
</RelativeLayout>
自定义cell:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp"
android:gravity="center_vertical"
android:background="#1000"
android:descendantFocusability="blocksDescendants"
>
// 非常关键的一句代码:解决焦点冲突的问题,尽量还是不要使用ImageButton Button CheckBox等强制获取焦点的控件
// android:descendantFocusability="blocksDescendants"
<TextView
android:id="@+id/textView_name"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:drawableLeft="@drawable/girl_30"
android:drawablePadding="3dp"
android:text=" s "
/>
<ImageButton
android:id="@+id/imgView_del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/del_30"
/>
</LinearLayout>
代码:
package com.sg31.popwindow;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements OnClickListener {
private EditText editText_name;
private ImageView imgView_select;
private ArrayList<String> wifeArrs = new ArrayList<String>();
private ListView listView;
private SGBaseAdapter sgBaseAdapter;
private PopupWindow popupWindow;
private int popupWindowHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initView();
}
private void initView() {
editText_name = (EditText) findViewById(R.id.editText_name);
imgView_select = (ImageView) findViewById(R.id.imgView_select);
imgView_select.setOnClickListener(this);
listView = new ListView(MainActivity.this);
// 滚动条隐藏
listView.setVerticalScrollBarEnabled(false);
// 自定义背景
// listView.setBackgroundResource(R.drawable.ic_launcher);
sgBaseAdapter = new SGBaseAdapter();
listView.setAdapter(sgBaseAdapter);
// 点击条目:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
System.out.println("sg__选中了_"+position);
popupWindow.dismiss();
editText_name.setText(wifeArrs.get(position));
}
});
}
class SGBaseAdapter extends BaseAdapter{
@Override
public int getCount() {
return wifeArrs.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final View view = View.inflate(MainActivity.this, R.layout.cell_item, null);
TextView tv_name = (TextView) view.findViewById(R.id.textView_name);
ImageButton img_del = (ImageButton) view.findViewById(R.id.imgView_del);
tv_name.setText(wifeArrs.get(position));
// 点击删除图标
img_del.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("sg__"+position);
wifeArrs.remove(position);
notifyDataSetChanged();
int listViewHeight = view.getHeight() * wifeArrs.size();
if (listViewHeight > popupWindowHeight) {
listViewHeight = popupWindowHeight;
}
popupWindow.update(editText_name.getWidth(), listViewHeight);
if (wifeArrs.size() == 0) {
popupWindow.dismiss();
imgView_select.setVisibility(View.INVISIBLE);
}
}
});
return view;
}
}
private void initData() {
String [] wifeArray = new String[] {"元春","探春","迎春","惜春","湘云","妙玉","熙凤","巧姐","可卿","黛玉","宝钗","银蝶","入画","彩屏","彩儿","瑞珠","宝珠","鸳鸯","文官","琥珀","蕊珠","秋桐","金钏","玉钏","彩霞","彩云","彩鸾","绣鸾","绣凤","小霞","袭人","蕊珠","晴雯","麝月","秋纹","碧痕","春燕","芳官","龄官","雪茜","佳蕙","坠儿","檀云","绮霰","良儿","媚人","墨雨","紫绡","平儿","小红","丰儿","彩明","抱琴","青芸","琴韵","司棋","侍书","雪雁","紫鹃","素云","碧月","翠缕","莺儿","娇杏","宝琴","岫烟","李绮","金桂","香菱","佳慧","诗涵","诗语","紫涵","紫缘","香橼","雨晴","时怡"};
for (String name : wifeArray) {
wifeArrs.add(name);
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imgView_select:
{
showPopupWindow();
}
break;
default:
break;
}
}
private void showPopupWindow() {
System.out.println("sg__显示popupWindow");
popupWindowHeight = 300;
// 非常关键的代码
if (popupWindow == null) {
popupWindow = new PopupWindow(listView, editText_name.getWidth(), popupWindowHeight);
// 下面三个是同时设置的
popupWindow.setFocusable(true);
// 必须要设置图片?!!因为内部源码会判断是否为null
popupWindow.setBackgroundDrawable(new BitmapDrawable());
// 点击外部,消失
popupWindow.setOutsideTouchable(true);
}
popupWindow.showAsDropDown(editText_name, 0, 3);
}
}