PopupWindow写法
//定义上下文
private Context context;
//创建一个集合
private List<Bean.DataBean.ComicsBean> list;
//有参构造
public MyAdapter(Context context, List<Bean.DataBean.ComicsBean> list) {
this.context = context;
this.list = list;
}
public void loadMore(List<Bean.DataBean.ComicsBean> list1, boolean falg) {
for (Bean.DataBean.ComicsBean bean : list1) {
if (falg) {
list.add(bean);
} else {
list.add(0, bean);
}
}
notifyDataSetChanged();
}
public void deleteData(int position) {
this.list.remove(position);
notifyDataSetChanged();
}
//集合的长度
@Override
public int getCount() {
return list.size();
}
//集合的下标
@Override
public Object getItem(int i) {
return list.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
//定义ViewHolder为空
ViewHolder holder = null;
//判断view是否为空
if (view == null) {
//实例化ViewHolder
holder = new ViewHolder();
//加载布局
view = View.inflate(context, R.layout.item, null);
//查找控件
holder.title = view.findViewById(R.id.title);
holder.url = view.findViewById(R.id.url);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
//展示值
holder.title.setText(list.get(i).getTitle());
ImageLoader.getInstance().displayImage(list.get(i).getCover_image_url(), holder.url, MyApplication.options());
final ImageView dianji = view.findViewById(R.id.dianji);
final View popupView = LayoutInflater.from(context).inflate(R.layout.popupwindow, null);
final PopupWindow window = new PopupWindow(popupView, 300, 50);
window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));
window.setFocusable(true);
window.setOutsideTouchable(true);
window.update();
dianji.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
window.showAsDropDown(dianji);
}
});
TextView read = popupView.findViewById(R.id.read);
TextView collect = popupView.findViewById(R.id.collect);
TextView delete = popupView.findViewById(R.id.delete);
ImageView close = popupView.findViewById(R.id.close);
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "已阅读", Toast.LENGTH_SHORT).show();
}
});
collect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "已收藏", Toast.LENGTH_SHORT).show();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deleteData(i);
window.dismiss();
}
});
close.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
window.dismiss();
}
});
//返回view
return view;
}
//定义ViewHolder
class ViewHolder {
TextView title;
ImageView url;
}