主页面
ListView lv = (ListView) findViewById(R.id.main_lv);
list = new ArrayList<>();
for (int i = 0; i < 20; i++) {
list.add("这是第" + i + "条");
}
lv.setAdapter(new MyAdapter(MainActivity.this,list));
适配器
public class MyAdapter extends BaseAdapter { private LayoutInflater Inflater; private ArrayList<String> list; private TextView tv; private Context context; private ImageView img; private PopupWindow window; private TextView tv1; private TextView tv2; public MyAdapter(Context context, ArrayList<String> list) { Inflater = LayoutInflater.from(context); this.context = context; this.list = list; initPopView(); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(final int i, View view, ViewGroup viewGroup) { view = View.inflate(context, R.layout.item, null); tv = view.findViewById(R.id.item_tv); img = view.findViewById(R.id.item_img); tv.setText(list.get(i)); img.setOnClickListener(new PopWindow(i)); return view; } class PopWindow implements View.OnClickListener { private int position; public PopWindow(int position) { this.position = position; } //显示在当前item上 @Override public void onClick(View view) { int[] ints = new int[2]; view.getLocationInWindow(ints); int x = ints[0]; int y = ints[1]; showPop(view, position, x, y); } } private void initPopView() { View view = Inflater.inflate(R.layout.poupwindow_layout, null); window = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //必须写,不写的话,点击popwindow外面没有效果 window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000"))); // window.setAnimationStyle(R.style.popWindowAnimation); //知道popwindow中间的控件 ,去做点击 tv1 = view.findViewById(R.id.t1); tv2 = view.findViewById(R.id.t2); } private void showPop(View parent, final int position, int x, int y) { //根据view的位置显示popupwindow的位置 window.showAtLocation(parent, 0, x, y); //根据view的位置popupwindow将显示到他的下面 , 可以通过x ,y 参数修正这个位置 // mPopupWindow.showAsDropDown(parent,0,-50); //设置popupwindow可以获取焦点,不获取焦点的话 popupwiondow点击无效 window.setFocusable(true); //点击popupwindow的外部,popupwindow消失 window.setOutsideTouchable(true); tv2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.remove(position); notifyDataSetChanged(); if (window.isShowing()) { window.dismiss(); } } }); tv1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (window.isShowing()) { window.dismiss(); } } });}}
item的控件
<TextView android:textSize="25sp" android:layout_marginTop="15dp" android:id="@+id/item_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> <ImageView android:id="@+id/item_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" />
popupwindow的控件
<TextView android:id="@+id/t1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="读文章" android:textSize="30sp" /> <TextView android:id="@+id/t2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="不感兴趣" android:textSize="30sp" />