PopuWindow用法
来源:互联网 发布:2014上半年经济数据 编辑:程序博客网 时间:2024/05/21 18:33
public class MainActivity extends AppCompatActivity {
private ListView listView;
private List<ItemBean> data;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
//创建数据
private void initData() {
data = new ArrayList<>();
for (int i = 0; i < 20; i++) {
ItemBean itemBean = new ItemBean("title " + i, "content " + i);
data.add(itemBean);
}
if (myAdapter == null) {
myAdapter = new MyAdapter(this, data);
listView.setAdapter(myAdapter);
}
}
//list展示
private void initView() {
listView = (ListView) findViewById(R.id.list_view);
}
}
适配器
public class MyAdapter extends BaseAdapter {
private Context context;
private List<ItemBean> data;
private LayoutInflater mLayoutInflater;
private PopupWindow mPopupWindow;
private TextView deleteView;
private ImageView closeView;
public MyAdapter(Context context, List<ItemBean> data) {
this.context = context;
this.data = data;
mLayoutInflater = LayoutInflater.from(context);
initPopView();
}
@Override
public int getCount() {
return data == null ? 0 : data.size();
}
@Override
public Object getItem(int position) {
return data == null ? null : data.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 = mLayoutInflater.inflate(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(data.get(position).getTitle());
holder.contentView.setText(data.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[] 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_layout, null);
mPopupWindow = new PopupWindow(popwindowLayout, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));
mPopupWindow.setAnimationStyle(R.style.popWindowAnimation);
//知道popwindow中间的控件 ,去做点击
deleteView = (TextView) popwindowLayout.findViewById(R.id.delete_tv);
closeView = (ImageView) popwindowLayout.findViewById(R.id.close_iv);
}
private void showPop(View parent, final int position, int x, int y) {
//根据view的位置显示popupwindow的位置
mPopupWindow.showAtLocation(parent, 0, x, y);
//根据view的位置popupwindow将显示到他的下面 , 可以通过x ,y 参数修正这个位置
// mPopupWindow.showAsDropDown(parent,0,-50);
//设置popupwindow可以获取焦点,不获取焦点的话 popupwiondow点击无效
mPopupWindow.setFocusable(true);
//点击popupwindow的外部,popupwindow消失
mPopupWindow.setOutsideTouchable(true);
deleteView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
data.remove(position);
notifyDataSetChanged();
if (mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
}
});
closeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mPopupWindow.isShowing()) {
mPopupWindow.dismiss();
}
}
});
}
}
- PopuWindow用法
- PopuWindow
- popuwindow
- popuwindow
- PopuWindow
- android popuwindow点击外部窗口不消失的实例,popuwindow的用法 [复制链接]
- android popuwindow
- 实现Popuwindow
- android popuwindow
- popuwindow使用
- popuwindow实例
- popuWindow初识
- 类似Popuwindow的实现
- Android弹出窗口PopuWindow
- Popuwindow简单使用
- 一个自定义popuwindow
- popuWindow相关知识
- PopuWindow的使用
- hdu6153-exkmp&&脑洞-A Secret
- LeetCode随笔之Array
- 使用freemarker导出word文档包含多张图片
- keras中文文档笔记13——解释CNN的滤波器
- codevs1298 凸包周长 解题报告
- PopuWindow用法
- WCF绑定netTcpBinding寄宿到控制台应用程序
- 袋鼠过河
- oracle增删改查语法
- 【POJ 1094】Sorting it all out (拓扑排序)
- 转载技巧~
- 【微信小程序】Page not found 错误
- 516_fragment不要用构造函数传参数
- 访问修饰符