下面列出了android.widget.PopupWindow#update ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public OverflowHelper(Context ctx) {
mContext = ctx;
mNormalColor = mContext.getResources().getColor(R.color.white);
mDisabledColor = mContext.getResources().getColor(R.color.text_disabled);
mPopupLayout = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.comm_popup_menu, null, true);
mListView = (PopupMenuListView) mPopupLayout.findViewById(R.id.comm_popup_list);
mAdapter = new OverflowAdapter(this, ctx);
mListView.setAdapter(mAdapter);
mListView.setOnKeyListener(mOnKeyListener);
mPopupWindow = new PopupWindow(mPopupLayout, -2, -2, true);
mPopupWindow.setContentView(mPopupLayout);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setFocusable(true);
mPopupWindow.setWidth(414);
mPopupWindow.getContentView().setOnTouchListener(mOnTouchListener);
mPopupWindow.update();
}
private PopupWindow shwoPop(int witch, int hight_bottom, boolean isDown, int gravity, View... views) {
PopupWindow popWindow = new PopupWindow(views[0], witch, LinearLayout.LayoutParams.MATCH_PARENT);
popWindow.setOutsideTouchable(true);
popWindow.setFocusable(true);
popWindow.setTouchable(true);
popWindow.setBackgroundDrawable(new BitmapDrawable());// 点击空白时popupwindow关闭
int[] location = new int[2];
try {
views[1].getLocationOnScreen(location);
popWindow.showAtLocation(views[1], gravity, 10, hight_bottom);
popWindow.update();
} catch (Exception e) {
Logger.e(e);
}
return popWindow;
}
private void initList() {
View popupWindow_view = LayoutInflater.from(context).inflate(
R.layout.droppopmenu, null);
popupWindow_view.setFocusableInTouchMode(true);
// 设置popupWindow的布局
popupWindow = new PopupWindow(popupWindow_view,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
popupWindow.setBackgroundDrawable(new ColorDrawable(
android.R.color.transparent));
// 设置允许在外点击消失
popupWindow.setOutsideTouchable(true);
listView = (ListView) popupWindow_view
.findViewById(R.id.droppopmenu_listView);
popupWindow.update();
}
private void touchLong() {
LogManager.getLogger().d(Tag, "touchLong");
View gifView = LayoutInflater.from(getContext()).inflate(R.layout.view_popupimg, null);
GifView gif = (GifView) gifView.findViewById(R.id.gif);
gif.setGifResource(filePath);
gif.play();
int[] location = SystemUtil.locationOnScreen(this);
window = new PopupWindow(gifView, SystemUtil.dipToPx(100), SystemUtil.dipToPx(100));
window.setAnimationStyle(R.style.Dialog);
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_trans_while));
window.setFocusable(true);
window.setOutsideTouchable(true);
window.update();
//location[1] = location[1] - getHeight();
if ((location[0] - SystemUtil.dipToPx(25) - SystemUtil.dipToPx(10)) <= SystemUtil.dipToPx(10)) {
location[0] = SystemUtil.dipToPx(10);
} else if ((location[0] - SystemUtil.dipToPx(25) + SystemUtil.dipToPx(100) + SystemUtil.dipToPx(20)) >= SystemDataUtil.getScreenWidth()) {
location[0] = SystemDataUtil.getScreenWidth() - SystemUtil.dipToPx(10) - SystemUtil.dipToPx(100);
} else {
location[0] = location[0] - SystemUtil.dipToPx(25);
}
window.showAtLocation(this, Gravity.TOP | Gravity.START, location[0], location[1] - SystemUtil.dipToPx(100));
}
private void showKey(final int keyIndex) {
final PopupWindow previewPopup = mPreviewPopup;
final Key[] keys = mKeys;
if (keyIndex < 0 || keyIndex >= mKeys.length) return;
Key key = keys[keyIndex];
if (key.icon != null) {
mPreviewText.setCompoundDrawables(null, null, null,
key.iconPreview != null ? key.iconPreview : key.icon);
mPreviewText.setText(null);
} else {
mPreviewText.setCompoundDrawables(null, null, null, null);
mPreviewText.setText(getPreviewText(key));
if (key.label.length() > 1 && key.codes.length < 2) {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
} else {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
mPreviewText.setTypeface(Typeface.DEFAULT);
}
}
mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
+ mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
final int popupHeight = mPreviewHeight;
LayoutParams lp = mPreviewText.getLayoutParams();
if (lp != null) {
lp.width = popupWidth;
lp.height = popupHeight;
}
if (!mPreviewCentered) {
mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + mPaddingLeft;
mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
} else {
// TODO: Fix this if centering is brought back
mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
mPopupPreviewY = - mPreviewText.getMeasuredHeight();
}
mHandler.removeMessages(MSG_REMOVE_PREVIEW);
getLocationInWindow(mCoordinates);
mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero
// Set the preview background state
mPreviewText.getBackground().setState(
key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
mPopupPreviewX += mCoordinates[0];
mPopupPreviewY += mCoordinates[1];
// If the popup cannot be shown above the key, put it on the side
getLocationOnScreen(mCoordinates);
if (mPopupPreviewY + mCoordinates[1] < 0) {
// If the key you're pressing is on the left side of the keyboard, show the popup on
// the right, offset by enough to see at least one key to the left/right.
if (key.x + key.width <= getWidth() / 2) {
mPopupPreviewX += (int) (key.width * 2.5);
} else {
mPopupPreviewX -= (int) (key.width * 2.5);
}
mPopupPreviewY += popupHeight;
}
if (previewPopup.isShowing()) {
previewPopup.update(mPopupPreviewX, mPopupPreviewY,
popupWidth, popupHeight);
} else {
previewPopup.setWidth(popupWidth);
previewPopup.setHeight(popupHeight);
previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
mPopupPreviewX, mPopupPreviewY);
}
mPreviewText.setVisibility(VISIBLE);
}
private void showKey(final int keyIndex) {
final PopupWindow previewPopup = mPreviewPopup;
final Key[] keys = mKeys;
if (keyIndex < 0 || keyIndex >= mKeys.length) return;
Key key = keys[keyIndex];
if (key.icon != null) {
mPreviewText.setCompoundDrawables(null, null, null,
key.iconPreview != null ? key.iconPreview : key.icon);
mPreviewText.setText(null);
} else {
mPreviewText.setCompoundDrawables(null, null, null, null);
mPreviewText.setText(getPreviewText(key));
if (key.label.length() > 1 && key.codes.length < 2) {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
} else {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
mPreviewText.setTypeface(Typeface.DEFAULT);
}
}
mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
+ mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
final int popupHeight = mPreviewHeight;
LayoutParams lp = mPreviewText.getLayoutParams();
if (lp != null) {
lp.width = popupWidth;
lp.height = popupHeight;
}
if (!mPreviewCentered) {
mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + mPaddingLeft;
mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
} else {
// TODO: Fix this if centering is brought back
mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
mPopupPreviewY = -mPreviewText.getMeasuredHeight();
}
mHandler.removeMessages(MSG_REMOVE_PREVIEW);
getLocationInWindow(mCoordinates);
mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero
// Set the preview background state
mPreviewText.getBackground().setState(
key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
mPopupPreviewX += mCoordinates[0];
mPopupPreviewY += mCoordinates[1];
// If the popup cannot be shown above the key, put it on the side
getLocationOnScreen(mCoordinates);
if (mPopupPreviewY + mCoordinates[1] < 0) {
// If the key you're pressing is on the left side of the keyboard, show the popup on
// the right, offset by enough to see at least one key to the left/right.
if (key.x + key.width <= getWidth() / 2) {
mPopupPreviewX += (int) (key.width * 2.5);
} else {
mPopupPreviewX -= (int) (key.width * 2.5);
}
mPopupPreviewY += popupHeight;
}
if (previewPopup.isShowing()) {
previewPopup.update(mPopupPreviewX, mPopupPreviewY,
popupWidth, popupHeight);
} else {
previewPopup.setWidth(popupWidth);
previewPopup.setHeight(popupHeight);
previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
mPopupPreviewX, mPopupPreviewY);
}
mPreviewText.setVisibility(VISIBLE);
}
private void showKey(final int keyIndex) {
final PopupWindow previewPopup = mPreviewPopup;
final Key[] keys = mKeys;
if (keyIndex < 0 || keyIndex >= mKeys.length) return;
Key key = keys[keyIndex];
if (key.icon != null) {
mPreviewText.setCompoundDrawables(null, null, null,
key.iconPreview != null ? key.iconPreview : key.icon);
mPreviewText.setText(null);
} else {
mPreviewText.setCompoundDrawables(null, null, null, null);
mPreviewText.setText(getPreviewText(key));
if (key.label.length() > 1 && key.codes.length < 2) {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
} else {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
mPreviewText.setTypeface(Typeface.DEFAULT);
}
}
mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
+ mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
final int popupHeight = mPreviewHeight;
LayoutParams lp = mPreviewText.getLayoutParams();
if (lp != null) {
lp.width = popupWidth;
lp.height = popupHeight;
}
if (!mPreviewCentered) {
mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + getPaddingLeft();
mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
} else {
// TODO: Fix this if centering is brought back
mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
mPopupPreviewY = - mPreviewText.getMeasuredHeight();
}
mHandler.removeMessages(MSG_REMOVE_PREVIEW);
getLocationInWindow(mCoordinates);
mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero
// Set the preview background state
mPreviewText.getBackground().setState(
key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
mPopupPreviewX += mCoordinates[0];
mPopupPreviewY += mCoordinates[1];
// If the popup cannot be shown above the key, put it on the side
getLocationOnScreen(mCoordinates);
if (mPopupPreviewY + mCoordinates[1] < 0) {
// If the key you're pressing is on the left side of the keyboard, show the popup on
// the right, offset by enough to see at least one key to the left/right.
if (key.x + key.width <= getWidth() / 2) {
mPopupPreviewX += (int) (key.width * 2.5);
} else {
mPopupPreviewX -= (int) (key.width * 2.5);
}
mPopupPreviewY += popupHeight;
}
if (previewPopup.isShowing()) {
previewPopup.update(mPopupPreviewX, mPopupPreviewY,
popupWidth, popupHeight);
} else {
previewPopup.setWidth(popupWidth);
previewPopup.setHeight(popupHeight);
previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
mPopupPreviewX, mPopupPreviewY);
}
mPreviewText.setVisibility(VISIBLE);
}
private void changePlayMode() {
View playModeView = LayoutInflater.from(this).inflate(R.layout.play_mode, null);
ConstraintLayout orderLayout = playModeView.findViewById(R.id.orderLayout);
ConstraintLayout randomLayout = playModeView.findViewById(R.id.randomLayout);
ConstraintLayout singleLayout = playModeView.findViewById(R.id.singleLayout);
TextView orderTv = playModeView.findViewById(R.id.orderTv);
TextView randomTv = playModeView.findViewById(R.id.randomTv);
TextView singleTv = playModeView.findViewById(R.id.singleTv);
//显示弹窗
PopupWindow popupWindow = new PopupWindow(playModeView, ScreenUtil.dip2px(this, 130), ScreenUtil.dip2px(this, 150));
//设置背景色
popupWindow.setBackgroundDrawable(getDrawable(R.color.transparent));
//设置焦点
popupWindow.setFocusable(true);
//设置可以触摸框以外的地方
popupWindow.setOutsideTouchable(true);
popupWindow.update();
//设置弹出的位置
popupWindow.showAsDropDown(mPlayModeBtn, 0, -50);
//显示播放模式
int mode = mPresenter.getPlayMode();
if (mode == Constant.PLAY_ORDER) {
orderTv.setSelected(true);
randomTv.setSelected(false);
singleTv.setSelected(false);
} else if (mode == Constant.PLAY_RANDOM) {
randomTv.setSelected(true);
orderTv.setSelected(false);
singleTv.setSelected(false);
} else {
singleTv.setSelected(true);
randomTv.setSelected(false);
orderTv.setSelected(false);
}
//顺序播放
orderLayout.setOnClickListener(view -> {
mPlayStatusBinder.setPlayMode(Constant.PLAY_ORDER); //通知服务
mPresenter.setPlayMode(Constant.PLAY_ORDER);
popupWindow.dismiss();
mPlayModeBtn.setBackground(getDrawable(R.drawable.play_mode_order));
});
//随机播放
randomLayout.setOnClickListener(view -> {
mPlayStatusBinder.setPlayMode(Constant.PLAY_RANDOM);
mPresenter.setPlayMode(Constant.PLAY_RANDOM);
popupWindow.dismiss();
mPlayModeBtn.setBackground(getDrawable(R.drawable.play_mode_random));
});
//单曲循环
singleLayout.setOnClickListener(view -> {
mPlayStatusBinder.setPlayMode(Constant.PLAY_SINGLE);
mPresenter.setPlayMode(Constant.PLAY_SINGLE);
popupWindow.dismiss();
mPlayModeBtn.setBackground(getDrawable(R.drawable.play_mode_single));
});
}
private void showKey(final int keyIndex) {
final PopupWindow previewPopup = mPreviewPopup;
final Keyboard.Key[] keys = mKeys;
if (keyIndex < 0 || keyIndex >= mKeys.length) return;
Keyboard.Key key = keys[keyIndex];
if (key.icon != null) {
mPreviewText.setCompoundDrawables(null, null, null,
key.iconPreview != null ? key.iconPreview : key.icon);
mPreviewText.setText(null);
} else {
mPreviewText.setCompoundDrawables(null, null, null, null);
mPreviewText.setText(getPreviewText(key));
if (key.label.length() > 1 && key.codes.length < 2) {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
} else {
mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
mPreviewText.setTypeface(Typeface.DEFAULT);
}
}
mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
+ mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
final int popupHeight = mPreviewHeight;
ViewGroup.LayoutParams lp = mPreviewText.getLayoutParams();
if (lp != null) {
lp.width = popupWidth;
lp.height = popupHeight;
}
int popupPreviewX;
int popupPreviewY;
if (!mPreviewCentered) {
popupPreviewX = key.x - mPreviewText.getPaddingLeft() + getPaddingLeft();
popupPreviewY = key.y - popupHeight + mPreviewOffset;
} else {
// TODO: Fix this if centering is brought back
popupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
popupPreviewY = -mPreviewText.getMeasuredHeight();
}
mHandler.removeMessages(MSG_REMOVE_PREVIEW);
getLocationInWindow(mCoordinates);
mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero
// Set the preview background state
mPreviewText.getBackground().setState(
key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
popupPreviewX += mCoordinates[0];
popupPreviewY += mCoordinates[1];
// If the popup cannot be shown above the key, put it on the side
getLocationOnScreen(mCoordinates);
if (popupPreviewY + mCoordinates[1] < 0) {
// If the key you're pressing is on the left side of the keyboard, show the popup on
// the right, offset by enough to see at least one key to the left/right.
if (key.x + key.width <= getWidth() / 2) {
popupPreviewX += (int) (key.width * 2.5);
} else {
popupPreviewX -= (int) (key.width * 2.5);
}
popupPreviewY += popupHeight;
}
if (previewPopup.isShowing()) {
previewPopup.update(popupPreviewX, popupPreviewY,
popupWidth, popupHeight);
} else {
previewPopup.setWidth(popupWidth);
previewPopup.setHeight(popupHeight);
previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
popupPreviewX, popupPreviewY);
}
mPreviewText.setVisibility(VISIBLE);
}