Android 自定義PopupWindow 實現 ListView與PopupWindow結合下拉框

轉載請標明出處:http://blog.csdn.net/u010886975/article/details/52711611

1.說明

1.1 項目中要求用下拉框彈出一個帶有listView的選擇框,開始想到spinner來實現,但是那個效果着實不好看,後來用PopupWindow,加listView的佈局文件來實現,但是使用的地方多了,就想着把他們弄到一個控件裏面,免得麻煩。最後效果如下,用法和PopupWindow一樣。Android菜鳥一枚,請大神們指教。

2.效果圖

這裏寫圖片描述

3.自定義

3.1 定義效果:顯示由上向下,消失由下向上。在anim中定義兩個動畫。
popupwindow_show:

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale  android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:pivotX="0%"
        android:pivotY="0%"
        android:fillAfter="false"
        android:duration="500" >
    </scale>
</set>  

popupwindow_gone:

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale  android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotX="0%"
        android:pivotY="0%"
        android:fillAfter="false"
        android:duration="500" >
    </scale>
</set>

在style.xml中引用定義的動畫:

 <style name="popwin_anim_style">
        <item name="android:windowEnterAnimation">@anim/popupwindow_show</item>
        <item name="android:windowExitAnimation">@anim/popupwindow_gone</item>
    </style>

3.2 定義控件:
第一步:定義ListViewPopuWindow繼承自PopupWindow;
接着定義一個ListView

 listView = new ListView(mContext);
 listView.setSelector(R.drawable.listview_item_selector);
 listView.setCacheColorHint(Color.TRANSPARENT);
 listView.setVerticalScrollBarEnabled(false);
 listView.setDivider(new ColorDrawable(Color.parseColor("#caced1")));
 listView.setDividerHeight(2);

有listView,那麼adapter肯定不能少的。這裏adapter沒什麼好說的。要說的adapter中的getView方法,這個返回view,沒有用佈局文件,當然你也可以用佈局文件。我們直接用代碼生成:

private LinearLayout createSingleView(String name) {
            LinearLayout ll = new LinearLayout(mContext);
            ll.setBackgroundColor(Color.TRANSPARENT);
            ll.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            ll.setOrientation(LinearLayout.HORIZONTAL);
            TextView tv = new TextView(mContext);
            tv.setText(name);
            tv.setFocusable(false);
            tv.setTextSize(textSize);
            tv.setPadding(0, 20, 0, 20);
            tv.setTextColor(Color.parseColor(textColor));
            ll.addView(tv, rlp);
            return ll;
        }

接下來在構造方法中設置PopupWindow的一些必要屬性和動畫:

    // 設置SelectPicPopupWindow彈出窗體的寬  
        this.setWidth(w);  
        // 設置SelectPicPopupWindow彈出窗體的高  
        this.setHeight(h); 

        initView();

        this.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.bg_listview_popuwindow_white));
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.setContentView(parentLinear);
        this.update();
        this.setAnimationStyle(R.style.popwin_anim_style);

這裏需要注意的是:需要有一個viewGroup來裝我們的listView,然後把viewGroup設置到PopupWindow中。這裏的viewGroup就是parentLinear,也可以,把listView直接設置到PopupWindow中,我這樣做是方便設置背景圖片。例如彈框的外形什麼的。
其實代碼真的沒什麼難度,很簡單的,只是把listview,和adapter放到空間裏邊去了。現在要做的就是,要實現一個接口。以便於我們在點擊item的時候。返回我們想要的信息:

    public interface MyClickListener {
        void ItemClick(int index, String str);
    }

    public void setOnMyItemClickListener(MyClickListener listener) {
        mYitemClickListener = listener;
    }

然後在listView的item點擊事件中把信息傳出去:

 listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int index,
                    long arg3) {
                mYitemClickListener.ItemClick(index,mStrings[index]);
            }
        });

這裏的mStrings是數據源。

3.使用

int[] location = new int[2];
        button.getLocationOnScreen(location);

        final ListViewPopuWindow popupWindow = new ListViewPopuWindow(
                this, tv_state_value, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,0);
        popupWindow.setOnMyItemClickListener(new ListViewPopuWindow.MyClickListener() {
            @Override
            public void ItemClick(int index, String str) {
                button.setText(str);
                popupWindow.dismiss();
            }
        });
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {

                @Override
                public void onDismiss() {
                //處理popupWindow消失時處理的事情
                }
            });
    popupWindow.showAtLocation(button, Gravity.NO_GRAVITY,
            location[0], location[1] + button.getHeight());

使用的是我們只需要傳一個,context,數據源,寬,高,背景圖片id。這裏寬高可以是ViewGroup.LayoutParams.WRAP_CONTENT。button.getLocationOnScreen(location)是爲了拿到顯示位置。
到這裏就寫完了。希望能對人有幫助。不明白的地方可以私信我。
demo下載地址:http://download.csdn.net/detail/u010886975/9648787

發佈了29 篇原創文章 · 獲贊 40 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章