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

来源:互联网 发布:macpro软件卸不掉 编辑:程序博客网 时间:2024/06/06 09:53
适配器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>

阅读全文
1 0
原创粉丝点击