listView中设置PopUpWindow

//drawable文件夹下放置两张图片


//自定义数据  创建一个JavaBean 在MainActivity中写一个添加数据的方法然后添加到集合中


//需要创建和添加的文件及属性

//popupwindow的动画

//res下创建一个anim文件夹

//创建第一个文件sliding_right_in.xml



       

//创建第二个文件sliding_right_out.xml




            android:duration="300"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />

//在style文件下添加属性设置



   
   
  
   

//popupwindow的布局

    android:layout_width="350dp"
    android:layout_height="80dp"
    android:minWidth="220dp"
    android:minHeight="30dp"
    android:gravity="center"
    >

            android:text="阅读"
        android:textColor="#ffffff"
        android:id="@+id/read_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />
            android:text="收藏"
        android:textColor="#ffffff"
        android:id="@+id/collect_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />
            android:text="删除"
        android:textColor="#ffffff"
        android:id="@+id/delete_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

            android:src="@drawable/close"
        android:id="@+id/close_iv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center" />

//适配器
/**
 * Created by hasee on 2017/8/14.
 */

public class MyAdapter extends BaseAdapter {
    private Context context;
    private List data;
    private LayoutInflater mLayoutInflater;
    private PopupWindow mPopupWindow;
    private TextView deleteView;
    private ImageView closeView;

    public MyAdapter(Context context, List data) {
        this.context = context;
        this.data = data;
        mLayoutInflater = LayoutInflater.from(context);
        initPopView();
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.item_layout, null);
            holder.titleView = (TextView) convertView.findViewById(R.id.title_tv);
            holder.contentView = (TextView) convertView.findViewById(R.id.content_tv);
            holder.moreView = (ImageView) convertView.findViewById(R.id.more_iv);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.titleView.setText(data.get(position).getTitle());
        holder.contentView.setText(data.get(position).getContent());
        holder.moreView.setOnClickListener(new PopAction(position));
        return convertView;
    }

    class ViewHolder {
        private TextView titleView;
        private TextView contentView;
        private ImageView moreView;
    }

    class PopAction implements View.OnClickListener {

        private int position;

        public PopAction(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            //操作对应positiion的数据
            int[] array = new int[2];
            v.getLocationOnScreen(array);
            int x = array[0];
            int y = array[1];
            showPop(v, position, x, y);
        }
    }

    private void initPopView() {
        View popwindowLayout = mLayoutInflater.inflate(R.layout.popwindow_layout, null);
        mPopupWindow = new PopupWindow(popwindowLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));
        mPopupWindow.setAnimationStyle(R.style.popWindowAnimation);

        //知道popwindow中间的控件 ,去做点击
        deleteView = (TextView) popwindowLayout.findViewById(R.id.delete_tv);
        closeView = (ImageView) popwindowLayout.findViewById(R.id.close_iv);
    }

    private void showPop(View parent, final int position, int x, int y) {

        //根据view的位置显示popupwindow的位置
        mPopupWindow.showAtLocation(parent, 0, x, y);

        //根据view的位置popupwindow将显示到他的下面 , 可以通过x ,y 参数修正这个位置
        // mPopupWindow.showAsDropDown(parent,0,-50);

        //设置popupwindow可以获取焦点,不获取焦点的话 popupwiondow点击无效
        mPopupWindow.setFocusable(true);

        //点击popupwindow的外部,popupwindow消失
        mPopupWindow.setOutsideTouchable(true);

        deleteView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                data.remove(position);
                notifyDataSetChanged();
                if (mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
            }
        });

        closeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopupWindow.isShowing()) {
                    mPopupWindow.dismiss();
                }
            }
        });


    }
}


你可能感兴趣的:(listView中设置PopUpWindow)