一、popupwindow工具類
/**
* @創建者 Sun
* @創建時間 2016/8/19
* @描述 popupwindow工廠
*/
public class PopupWindowUtils {
/**
* 得到在屏幕中間的popupwindow并顯示(寬高都是包裹視圖)
*
* @param contentView popupwindow要顯示的視圖
* @param parentView? 參考視圖
* @return
*/
public static PopupWindow getPopupWindowInCenter(View contentView, View parentView) {
//? ? ? ? int width = ViewGroup.LayoutParams.MATCH_PARENT;
int width = ViewGroup.LayoutParams.WRAP_CONTENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
return getPopupWindowInCenter(contentView, width, height, parentView);
}
/**
* 得到在屏幕中間的popupwindow并顯示
*
* @param contentView popupwindow要顯示的視圖
* @param width? ? ? popupwindow的寬度
* @param height? ? ? popupwindow的高度
* @param parentView? 參考視圖
* @return
*/
public static PopupWindow getPopupWindowInCenter(View contentView, int width, int height, View parentView) {
//Gravity.CENTER:在屏幕居中,無偏移
return getPopupWindowAtLocation(contentView, width, height, parentView, Gravity.CENTER, 0, 0);
}
/**
* 得到指定在某個視圖內位置的popupwindow并顯示
*
* @param contentView popupwindow要顯示的視圖
* @param width? ? ? popupwindow的寬度
* @param height? ? ? popupwindow的高度
* @param parentView? 參考視圖
* @param gravityType 在參考視圖中的相對位置
* @param xoff? ? ? ? x軸偏移量
* @param yoff? ? ? ? y軸偏移量
* @return
*/
public static PopupWindow getPopupWindowAtLocation(View contentView, int width, int height, View parentView, int gravityType, int xoff, int yoff) {
PopupWindow popupWindow = getPopupWindow(contentView, width, height);
//在parentView中偏移xoff和yoff
popupWindow.showAtLocation(parentView,
gravityType, xoff, yoff);
return popupWindow;
}
public static PopupWindow getPopupWindowAtLocation(View contentView, View parentView, int gravityType, int xoff, int yoff) {
return getPopupWindowAtLocation(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, parentView, gravityType, xoff, yoff);
}
/**
* 得到一個自動識別在目標控件上方或下方的pupupwindow并顯示
*
* @param contentView popupwindow要顯示的視圖
* @param width? ? ? popupwindow的寬度
* @param activity? ? 能得到getWindowManager()的上下文
* @return
*/
public static PopupWindow getPopupWindowAsDropDownParentAuto(View contentView, int width, int height, View anchorView, Activity activity) {
//? ? ? ? View itemView = (View) contentView.getParent();// 得到contentView的父控件
PopupWindow popupWindow = getPopupWindow(contentView, width, height);
// 控制它放置的位置
if (isShowBottom(activity, anchorView)) {// 顯示popupwindow在itemView的下方,偏移量都為0
popupWindow.showAsDropDown(anchorView, 0, 0);
} else {// 顯示popupwindow在itemView的上方,偏移量y都為-2*itemView.getHeight()
popupWindow.showAsDropDown(anchorView, 0,
-2 * anchorView.getHeight());
}
return popupWindow;
}
/**
* 得到在指定某個視圖外的popupwindow并顯示
*
* @param contentView popupwindow要顯示的視圖
* @param width? ? ? popupwindow的寬度
* @param height? ? ? popupwindow的高度
* @param anchorView? 參考視圖
* @param xoff? ? ? ? x軸偏移量
* @param yoff? ? ? ? y軸偏移量
* @return
*/
public static PopupWindow getPopupWindowAsDropDown(View contentView, int width, int height, View anchorView, int xoff, int yoff) {
PopupWindow popupWindow = getPopupWindow(contentView, width, height);
popupWindow.showAsDropDown(anchorView, xoff, yoff);
return popupWindow;
}
/**
* 得到在指定某個視圖外的popupwindow并顯示(該方法只支持4.4以上)
*
* @param contentView popupwindow要顯示的視圖
* @param width? ? ? popupwindow的寬度
* @param height? ? ? popupwindow的高度
* @param anchorView? 參考視圖
* @param gravityType 在參考視圖外的相對位置
* @param xoff? ? ? ? x軸偏移量
* @param yoff? ? ? ? y軸偏移量
* @return
*/
@TargetApi(Build.VERSION_CODES.KITKAT)
public static PopupWindow getPopupWindowAsDropDown(View contentView, int width, int height, View anchorView, int gravityType, int xoff, int yoff) {
PopupWindow popupWindow = getPopupWindow(contentView, width, height);
popupWindow.showAsDropDown(anchorView, xoff, yoff, gravityType);
return popupWindow;
}
/**
* 判斷popupWindow是否顯示在條目的下方
*
* @param itemView
* @return
*/
private static boolean isShowBottom(Activity context, View itemView) {
// 得到屏幕的高度
// int heightPixels =
// getResources().getDisplayMetrics().heightPixels;//方式1
int screenHeight = context.getWindowManager().getDefaultDisplay().getHeight();// 方式2
int[] location = new int[2];
// location[0]-->x
// location[1]-->y
itemView.getLocationInWindow(location);
// 得到itemView在屏幕中Y軸的值
int itemViewY = location[1];
// 得到itemView距離屏幕底部的距離
int distance = screenHeight - itemViewY - itemView.getHeight();
if (distance < itemView.getHeight()) {// 條目下方放不下popupWindow
return false;
} else {// 條目下方放得下popupWindow
return true;
}
}
/**
* 最核心的pupupwindow創建方法
*
* @param contentView popupwindow要顯示的視圖
* @param width? ? ? popupwindow的寬度
* @param height? ? ? popupwindow的高度
* @return
*/
@NonNull
private static PopupWindow getPopupWindow(View contentView, int width, int height) {
PopupWindow popupWindow = new PopupWindow(contentView, width, height, true);
popupWindow.setOutsideTouchable(false);
openOutsideTouchable(popupWindow);
return popupWindow;
}
/**
* 點擊popupwindow范圍以外的地方時隱藏
*
* @param popupWindow
*/
public static void openOutsideTouchable(PopupWindow popupWindow) {
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
}
/**
* 使window變暗
*/
public static void makeWindowDark(Activity activity) {
makeWindowDark(activity, 0.7f);
}
public static void makeWindowDark(Activity activity, float alpha) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = alpha;
activity.getWindow().setAttributes(lp);
}
/**
* 使window變亮
*/
public static void makeWindowLight(Activity activity) {
WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
lp.alpha = 1f;
activity.getWindow().setAttributes(lp);
}
}
二、在代碼中的使用,以下代碼寫在你得OnLongClick點擊事件下面
View menuView = View.inflate(mContext, R.layout.layout_longclick_popwindow, null);
final PopupWindow popupWindow = PopupWindowUtils.getPopupWindowAsDropDownParentAuto(menuView, RecyclerView.LayoutParams.WRAP_CONTENT, RecyclerView.LayoutParams.WRAP_CONTENT, view, mContext);
menuView.findViewById(R.id.tvcope).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
cm.setText(sText);
popupWindow.dismiss();
}
});
menuView.findViewById(R.id.tvforward).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
menuView.findViewById(R.id.tvcollection).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.dismiss();
}
});
menuView.findViewById(R.id.tvdelete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
三、布局代碼
就是在CardView里面創建一個LinearLayout,下面放你需要的Textview
四、使用起來還是有一定問題
getPopupWindowAsDropDownParentAuto()這個自動識別在目標控件上方或下方的pupupwindow還是有點問題,希望各位大佬多多指教