效果图如下:
核心代码:
private void showDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
View bordView = layoutInflater.inflate(R.layout.goods_select_pop_up, null);
goodsSelectLayout = (LinearLayout) bordView.findViewById(R.id.goods_container);
goodsSelectTitleTv = (TextView) bordView.findViewById(R.id.goods_select_title);
int width = getResources().getDisplayMetrics().widthPixels;
goodSelectBoard = new PopupWindow(bordView, width, ViewGroup.LayoutParams.WRAP_CONTENT, true);
goodSelectBoard.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
goodSelectBoard.setAnimationStyle(R.style.popup_anim_style);
goodSelectBoard.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
setMaskDisplay(false);
}
});
goodsSelectListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
goodSelectBoard.dismiss();
}
};
// 2. 更新內容
goodsSelectLayout.removeAllViews();
int num = dataList.size();
for (int i = 0; i < num; i++) {
String barcode = dataList.get(i);
View itemView = layoutInflater.inflate(R.layout.goods_select_item, null);
itemView.setOnClickListener(goodsSelectListener);
TextView nameTv = (TextView) itemView.findViewById(R.id.goods_item_name);
nameTv.setText(barcode);
goodsSelectLayout.addView(itemView);
}
// 3. 显示popupWindow
goodSelectBoard.showAtLocation(rootView, Gravity.CENTER, 0, 0);
setMaskDisplay(true);
}
PopupWindow的弹出动画:
<style name="popup_anim_style">
<item name="android:windowEnterAnimation">@anim/pop_enter_anim</item>
<item name="android:windowExitAnimation">@anim/pop_exit_anim</item>
<item name="android:background">#00ffffff</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
pop_enter_anim的内容:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="200"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
控制PopupWindow的遮罩的出现和消失:
private void setMaskDisplay(boolean display) {
if (null != maskAnimator1 && maskAnimator1.isRunning()) {
maskAnimator1.cancel();
}
float alphaTo = display ? 1.0f : 0.0f;
float alphaFrom = 1.0f - alphaTo;
maskAnimator1 = ObjectAnimator.ofFloat(maskView, "alpha", alphaFrom, alphaTo);
maskAnimator1.setDuration(200);
maskAnimator1.start();
}
代码下载地址:https://download.csdn.net/download/jingerlovexiaojie/10375298