导语:
打造一个下滑底部弹出的窗口:PopupWindow,今日完成小小的购物车功能,现贴上方便大家复制粘贴。
直接上代码:
/**
* 显示popupWindow
*/
private void showPopwindow() {
// 利用layoutInflater获得View
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popwindowlayout, null);
PopupWindow window = new PopupWindow(view,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT);
window.setFocusable(true);
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
window.setBackgroundDrawable(dw);
// 设置popWindow的显示和消失动画
window.setAnimationStyle(R.style.mypopwindow_anim_style);
// 在底部显示
window.showAtLocation(StortDetailsActivity.this.findViewById(R.id.stort_details_cl),Gravity.BOTTOM, 0, 0);
//添加控件绑定并配置适配器
final List<GoodsUtils> goodsUtilsList = new ArrayList<>();
Button empty_cart =(Button)view.findViewById(R.id.empty_cart);
final ListView goodsCart = (ListView) view.findViewById(R.id.goods_cart_lv);
//类似如此添加监听事件
empty_cart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
goodsCartPopwindowAdapter = new GoodsCartPopwindowAdapter(getApplicationContext(),goodsUtilsList);
goodsCart.setAdapter(goodsCartPopwindowAdapter);
//popWindow消失监听方法
window.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
System.out.println("popWindow消失");
}
});
}
贴出布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/popwindow_lin"
android:layout_width="match_parent"
android:layout_height="360dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:layout_marginLeft="10dp"
android:textColor="@color/primaryDark"
android:text="我的购物车" />
<Button
android:id="@+id/empty_cart"
android:layout_width="wrap_content"
android:gravity="center"
android:text="清空购物车"
android:background="@color/empty"
android:drawableRight="@drawable/delete"
android:textColor="@color/white"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<ListView
android:id="@+id/goods_cart_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
添加anim对popupWindow赋予动画效果:
1.创建一个 pophidden_anim.xml 文件在anim文件夹:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
2.在创建一个 popshow_anim.xml 文件在anim文件夹,上浮动显示动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
最后来个效果:
总结:很简单噶