MainActivity.java package com.example.popupwindow;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterVie
package com.example.popupwindow; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.TextView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private EditText et; private ImageView iv; private PopupWindow popupWindow; private ListView listvew; private ArrayListactivity_main.xmldata; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et = findViewById(R.id.et); iv = findViewById(R.id.iv); et.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (popupWindow == null) { popupWindow = new PopupWindow(MainActivity.this);//创建popupwindow对象 popupWindow.setWidth(et.getWidth());//设置popupwindow的宽高 popupWindow.setHeight(200); popupWindow.setContentView(listvew);//设置popupwindow中加载的视图控件 popupWindow.setFocusable(true);//获取焦点!!!重要如果没有写点击事件将会失效! } popupWindow.showAsDropDown(et, 0, 0);//设置popupwindow位于view的位置,该popupw位于edittext下方 } }); data = new ArrayList<>(); for (int i = 0; i < 100; i++) { data.add(i + "aaaaa" + i); } listvew = new ListView(this); myAdapter = new MyAdapter(); listvew.setBackgroundColor(Color.WHITE); listvew.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { String msg = data.get(position); et.setText(msg); if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } } }); listvew.setAdapter(myAdapter); } public class MyAdapter extends BaseAdapter { @Override public int getCount() { return data.size(); } @Override public Object getItem(int position) { return position; } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view; if (convertView == null) { view = View.inflate(MainActivity.this, R.layout.item, null); } else { view = convertView; } TextView TV = view.findViewById(R.id.tv); final ImageView delete = view.findViewById(R.id.delete); final String msg = data.get(position); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { data.remove(msg); myAdapter.notifyDataSetChanged(); } }); TV.setText(data.get(position)); return view; } //使用viewholder,更加优化 /*ViewHoldor viewHoldor; if (convertView==null){ convertView=View.inflate(MainActivity.this,R.layout.item,null); viewHoldor=new ViewHoldor(); viewHoldor.tv=convertView.findViewById(R.id.tv); viewHoldor.delete=convertView.findViewById(R.id.delete); convertView.setTag(viewHoldor); }else { viewHoldor= (ViewHoldor) convertView.getTag(); } final String datas=data.get(position); viewHoldor.tv.setText(datas); viewHoldor.delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { data.remove(datas); myAdapter.notifyDataSetChanged(); } }); return convertView; } } static class ViewHoldor{ TextView tv; ImageView delete;*/ } }
item.xml