点击每一个条目的图片都可以弹出PopupWindow

来源:互联网 发布:windows会员计划 编辑:程序博客网 时间:2024/05/22 14:52

1.布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="300dp"

    android:layout_height="50dp"

    android:orientation="horizontal"

    android:gravity="center"

    android:background="#c2c2c2">

    <ImageView

        android:layout_width="20dp"

        android:layout_height="20dp"

        android:background="@mipmap/listpage_more_speak_pressed_night"/>

<TextView

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="读文章"

    android:textColor="#fff"

    android:layout_marginLeft="10dp"/>

    <ImageView

        android:layout_width="20dp"

        android:layout_height="20dp"

        android:layout_marginLeft="10dp"

        android:background="@mipmap/listpage_more_like_pressed_night"/>

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="收藏"

        android:textColor="#fff"

        android:layout_marginLeft="10dp"/>

    <ImageView

        android:layout_width="20dp"

        android:layout_height="20dp"

        android:layout_marginLeft="10dp"

        android:background="@mipmap/listpage_more_dislike_pressed_night"/>

    <TextView

        android:id="@+id/bgx"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="不感兴趣"

        android:textColor="#fff"

        android:layout_marginLeft="10dp"/>

    <ImageView

        android:id="@+id/dis"

        android:layout_width="20dp"

        android:layout_height="20dp"

        android:layout_marginLeft="10dp"

        android:background="@mipmap/abc_ic_clear_search_api_holo_light"/>

</LinearLayout>


2.在每个适配器的viewHolder找到点击弹出PopupWindow的按钮,进行监听

 private  PopupWindow pop

//找到PopupWindow弹框的布局

 View view = View.inflate(context,R.layout.pwo_layout,null);

      //获取弹框的控件

        ImageView dis = (ImageView) view.findViewById(R.id.dis);
        TextView bgx = (TextView) view.findViewById(R.id.bgx);
       //PopupWindow弹框消失
        dis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               Toast.makeText(context, "您点击了消失", Toast.LENGTH_SHORT).show();

                pop.dismiss();
            }
        });

        pop = new PopupWindow();
        // 设置可以获得焦点
        pop.setFocusable(true);
        // 设置弹窗内可点击
        pop.setTouchable(true);
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 设置弹窗外可点击
        pop.setOutsideTouchable(true);
        pop.setWidth(300);
        pop.setHeight(50);
        pop.setContentView(view);

        //进行PopupWindow弹框
        viewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                index =position;

                pop.showAsDropDown(viewHolder.image);
            }
        });
        //点击不敢兴趣进行删除此条目

        bgx.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(context, "您点击了不感兴趣", Toast.LENGTH_SHORT).show();

                list.remove(list.get(index));

                pop.dismiss();

                notifyDataSetChanged();
            }
        });

3.如图所示



阅读全文
0 0