pop的实际应用

本文介绍如何在Android应用中创建自定义ListView,并通过PopupWindow实现动态内容展示及交互功能。示例代码展示了如何初始化ListView及其适配器,定义PopupWindow的布局与行为,以及如何响应用户操作更新ListView内容。


 主页面

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" />




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值