PopupWindow里面动态添加内容的应用
程序员文章站
2022-05-31 10:56:08
...
效果图如下:
核心代码:
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推荐阅读
-
VUE在for循环里面根据内容值动态的加入class值的方法
-
JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存
-
js里面怎么动态的为对象添加属性
-
Python 爬取网页中JavaScript动态添加的内容(一)
-
Python 爬取网页中JavaScript动态添加的内容(二)
-
VUE在for循环里面根据内容值动态的加入class值的方法
-
PopupWindow里面动态添加内容的应用
-
详解jQuery向动态生成的内容添加事件响应jQuery live()方法_jquery
-
IFrame里面的子页面html内容变化时,怎么动态改变IFrame的高度
-
JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存