MyBase类:
public class MyBase extends BaseAdapter { Context context; List<Bean.ResultBean.BookListBean> list; private LayoutInflater mLayoutInflater; private PopupWindow mpop; private TextView delete; private ImageView close; public MyBase(Context context, List<Bean.ResultBean.BookListBean> list){ this.context = context; this.list = list; mLayoutInflater = LayoutInflater.from(context); initPopView(); } ImageOptions options = new ImageOptions.Builder() .setLoadingDrawableId(R.mipmap.ic_launcher) .setUseMemCache(true) .setSize(200, 200) .build(); public void more(List<Bean.ResultBean.BookListBean> lists, boolean flag) { for (Bean.ResultBean.BookListBean bean : lists) { 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 = null; if (convertView == null) { convertView = mLayoutInflater.inflate(R.layout.item,null); holder = new ViewHolder(); holder.name = (TextView) convertView.findViewById(R.id.name); holder.type = (TextView) convertView.findViewById(R.id.type); holder.area = (TextView) convertView.findViewById(R.id.area); holder.more = (ImageView) convertView.findViewById(R.id.more); holder.img = (ImageView) convertView.findViewById(R.id.img); 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()); x.image().bind(holder.img, list.get(position).getCoverImg(),options); holder.more.setOnClickListener(new PopAction(position)); 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); } } private void initPopView(){ View popwindowLayout = mLayoutInflater.inflate(R.layout.popwindow,null); mpop = new PopupWindow(popwindowLayout, ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); mpop.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000"))); mpop.setAnimationStyle(R.style.popWindowAnimation); delete = (TextView) popwindowLayout.findViewById(R.id.delete_tv); close = (ImageView) popwindowLayout.findViewById(R.id.close_iv); } private void showPop(View parent, final int position, int x, int y){ mpop.showAtLocation(parent,0,x,y); mpop.setFocusable(true); mpop.setOutsideTouchable(true); delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.remove(position); notifyDataSetChanged(); if(mpop.isShowing()){ mpop.dismiss(); } } }); close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(mpop.isShowing()){ mpop.dismiss(); } } }); } class ViewHolder { TextView name; TextView type; TextView area; ImageView img; ImageView more; } }然后就在需要适配器的地方进行适配
Style:
<style name="popWindowAnimation" parent="@android:style/Animation"> <item name="android:windowEnterAnimation">@anim/right</item> <item name="android:windowExitAnimation">@anim/left</item> </style>
popwindow的动画:
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromXDelta="0" android:toXDelta="-100%p" /> </set>
<set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="100%p" android:toXDelta="0.0%p" android:duration="300"/> </set>