1. 程式人生 > >一個支援ListView的底部彈出控制元件 PopupWindow 小例子

一個支援ListView的底部彈出控制元件 PopupWindow 小例子

導語:
打造一個下滑底部彈出的視窗: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>

最後來個效果:
這裡寫圖片描述
總結:很簡單噶