PopupWindow的使用

2022-01-12 15:43:32 浏览数 (1)

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

布局

代码语言:javascript复制
<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>

 代码

代码语言:javascript复制
	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);
	}

0 人点赞