前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PopupWindow的使用

PopupWindow的使用

作者头像
六月的雨
发布2022-01-12 15:43:32
2610
发布2022-01-12 15:43:32
举报

想要弹出内容就可以考虑使用悬浮窗

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".LoginActivity" >
    <com.handmark.pulltorefresh.library.PullToRefreshListView
        android:id="@+id/lv_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#fff" />
    <View
        android:id="@+id/viewHolder"
        android:layout_width="match_parent"
        android:layout_height="53dp"
        android:visibility="gone" />
</LinearLayout>

 代码

	lv_list.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
				Toast.makeText(getApplicationContext(), "长按", 0).show();
				//开启编辑模式
				startEditModel();
				if (position > 0) {
					position -= 1;
				}
				adapter.toggleSelect(view, position);
				return true;
			}
		});
	}
	private boolean isEditModel;
	private int seletedCount;

	/**
	 * 开启编辑模式
	 */
	private void startEditModel() {
		//listview需要刷新
		isEditModel = true;
		adapter.notifyDataSetChanged();
		//修改actionbar
		uploadMenuItem.setVisible(false);
		downloadMenuItem.setVisible(false);
		moreMenuItem.setVisible(false);
		selectMenuItem.setVisible(true);
		actionBar.setDisplayHomeAsUpEnabled(true);
		actionBar.setTitle(String.format("已选定%d个", seletedCount));
		//显示底部的popupwindows
//当在最 底部时会覆盖条目,可以在下面弄个view,让他显示
		showBottomPopupWindow();
		//listview上移
		viewHolder.setVisibility(0);
	}
	/**
	 * 结束编辑模式
	 */
	private void stopEditModel() {
		//listview需要刷新
		isEditModel = false;
		adapter.notifyDataSetChanged();
		//修改actionbar
		uploadMenuItem.setVisible(true);
		downloadMenuItem.setVisible(true);
		moreMenuItem.setVisible(true);
		selectMenuItem.setVisible(false);
		actionBar.setTitle("黑马网盘");
		//返回按钮的处理
		if ("/".equals(curPath)) {
			actionBar.setDisplayHomeAsUpEnabled(false);
		}
		//隐藏popupwindows
		bottomPopupWindow.dismiss();
		//listview还原
		viewHolder.setVisibility(8);
		//还原entryWrapper的选中状态
		for (EntryWrapper entryWrapper : contents) {
			entryWrapper.isCheck = false;
		}
		seletedCount = 0;
	}
	private void showBottomPopupWindow() {
		if (bottomPopupWindow == null) {
			View contentView = View.inflate(MainActivity.this, R.layout.bottom_edit_pop, null);
			int width = ViewGroup.LayoutParams.FILL_PARENT;
			int height = ViewGroup.LayoutParams.WRAP_CONTENT;
			bottomPopupWindow = new PopupWindow(contentView, width, height);
			contentView.findViewById(R.id.DeleteBtn).setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					List<EntryWrapper> selectedEntryWrappers = new ArrayList<EntryWrapper>();
					for (EntryWrapper info : contents) {
						if (info.isCheck) {
							selectedEntryWrappers.add(info);
						}
					}
					StringBuffer sb = new StringBuffer();
					//遍历输出
					for (EntryWrapper entryWrapper : selectedEntryWrappers) {
						sb.append(entryWrapper.entry.fileName()).append(" ");
					}
					System.out.println(sb.toString());
				}
			});
		}
		bottomPopupWindow.showAtLocation(rl_root, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
	}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档