项目中点击imageview显示popupWindow,删除,退出等





适配器


public class MyBase extends BaseAdapter {
    Context context;
    List<InfoUtils.ResultBean.BookListBean> list;
    private final LayoutInflater inflater;
    private PopupWindow popwindow;
    private View viewpop;
    private PopupWindow popupWindow;
    private TextView delete;
    private ImageView colse;


    public MyBase(Context context, List<InfoUtils.ResultBean.BookListBean> list) {
        this.context = context;
        this.list = list;
        inflater = LayoutInflater.from(context);
        initpop();
    }
    public void add(Context context, List<InfoUtils.ResultBean.BookListBean> list1,boolean flag){
        for (InfoUtils.ResultBean.BookListBean bean: list1) {
            if (flag){
                list.add(0,bean);
            }else {
                list.add(bean);
            }
        }

    }

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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.fragment_layout,null);
            holder.name = (TextView) convertView.findViewById(R.id.text_view1);
            holder.type = (TextView) convertView.findViewById(R.id.text_view2);
            holder.area = (TextView) convertView.findViewById(R.id.text_view3);
            holder.image = (ImageView) convertView.findViewById(R.id.image_view);
            holder.more = (ImageView) convertView.findViewById(pop);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.name.setText(list.get(position).getName());
        holder.type.setText(list.get(position).getType());
        holder.area.setText(list.get(position).getArea());
        holder.more.setOnClickListener(new Popaction(position));
        x.image().bind(holder.image,list.get(position).getCoverImg());

        return convertView;
    }
    class Popaction implements View.OnClickListener{
        private int position;

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

        @Override
        public void onClick(View v) {
            int[] array = new int[2];
            v.getLocationOnScreen(array);
            int x = array[0];
            int y = array[1];
            showpop(v,position,x,y);
        }
    }
    public void initpop(){

        viewpop = inflater.inflate(R.layout.item,null);
        popupWindow = new PopupWindow(viewpop, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));
        delete = (TextView) viewpop.findViewById(R.id.delete_tv);
        colse = (ImageView) viewpop.findViewById(R.id.close_iv);
    }
    public void showpop(View view, final int position, int x, int y){

        popupWindow.showAtLocation(view,0,x,y);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);

        colse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (popupWindow.isShowing()){

                    popupWindow.dismiss();
                }
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list.remove(position);
                notifyDataSetChanged();
                if (popupWindow.isShowing()){

                    popupWindow.dismiss();
                }
            }
        });

    }
    class ViewHolder{
        private TextView name;
        private TextView type;
        private TextView area;
        private ImageView image;
        private ImageView more;
    }
}

Activity 

if (base==null){
    base = new MyBase(getActivity(),list);
    xListView.setAdapter(base);
}else {
    base.add(getActivity(),list,flag);
    base.notifyDataSetChanged();
}


XML

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="220dp"
    android:minHeight="40dp"
    android:gravity="center"
    >

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值