主页面 public class MainActivity extends AppCompatActivity { private ListView listview; private List<ItemBean> list; private Myadapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView) findViewById(R.id.listview); initData(); } private void initData() { list = new ArrayList<>(); for (int i = 0; i < 20; i++) { ItemBean itemBean = new ItemBean("title " + i, "content " + i); list.add(itemBean); } if (myAdapter == null) { myAdapter = new Myadapter(this, list); listview.setAdapter(myAdapter); } } } 适配器public class Myadapter extends BaseAdapter { private Context context; private List<ItemBean> list; private PopupWindow popupWindow; private TextView deleteView; private ImageView closeView; public Myadapter(Context context, List<ItemBean> list) { this.context = context; this.list = list; PopView(); } @Override public int getCount() { return list == null ? 0 : list.size(); } @Override public Object getItem(int position) { return list == null ? null : list.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 = convertView.inflate(context, 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(list.get(position).getTitle()); holder.contentView.setText(list.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[] arry=new int[2]; v.getLocationOnScreen(arry); int x=arry[0]; int y=arry[1]; showpop(v,position,x,y); } } private void PopView(){ View inflate = View.inflate(context, R.layout.popwindow_layout, null); popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000"))); //出现退出效果 popupWindow.setAnimationStyle(R.style.popwindowanimation); //知道popwindow中间的控件 ,去做点击 deleteView = (TextView) inflate.findViewById(R.id.delete_tv); closeView = (ImageView) inflate.findViewById(R.id.close_iv); } private void showpop(final View parent, final int position, int x, int y){ //根据view的位置显示popupwindow的位置 popupWindow.showAtLocation(parent,0,x,y); //设置popupwindow可以获取焦点,不获取焦点的话 popupwiondow点击无效 popupWindow.setFocusable(true); //点击popupwindow的外部,popupwindow消失 popupWindow.setOutsideTouchable(true); deleteView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { list.remove(position); notifyDataSetChanged(); if (popupWindow.isShowing()){ popupWindow.dismiss(); } } }); closeView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (popupWindow.isShowing()){ popupWindow.dismiss(); } } }); } }Bean类public class ItemBean { private String title; private String content; public ItemBean(String title, String content) { this.title = title; this.content = content; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } } XML:<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.popwindow.MainActivity"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:cacheColorHint="@color/colorPrimary" ></ListView> </FrameLayout> item_layout:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="150dp" android:orientation="vertical"> <TextView android:id="@+id/title_tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/content_tv" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:padding="5dp" android:layout_gravity="right" android:src="@drawable/more" android:id="@+id/more_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>popwindouw_layout:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="350dp" android:layout_height="80dp" android:minWidth="220dp" android:minHeight="30dp" 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>anim下:
pop_in:<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromXDelta="100%p" android:toXDelta="0" /> </set> pop_out:<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="300" android:fromXDelta="0" android:toXDelta="-100%p" /> </set> values下: style:<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style>重点 <style name="popwindowanimation" parent="@android:style/Animation"> <item name="android:windowEnterAnimation">@anim/pop_in</item> <item name="android:windowExitAnimation">@anim/pop_out</item> </style> </resources>
popwindow实现listview对应删除
最新推荐文章于 2018-11-28 14:41:57 发布