自己動手擼一個Android中PopupWindow

效果圖如下:
GIF.gif
實現(xiàn)流程:
1斤寂、編寫列表Item布局:area_code_item_view.

很簡單填帽,就是一個TeztView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
    <TextView
        android:id="@+id/area_code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp"
        android:text="123"
        android:textColor="@color/c_black"
        android:textSize="@dimen/text_size_20" />
    
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:background="@color/c_bg_gray_light" />

</LinearLayout>
圖片.png
2评也、編寫列表適配器Adapter:JobPopupWindowAdapter

JobPopupWindowAdapter 繼承于BaseAdapter瞳脓,構(gòu)造方法中將數(shù)據(jù)源以及選擇某一項數(shù)據(jù)的回調(diào)方法傳進來

  public JobPopupWindowAdapter(Context context,ArrayList<CodeDataResponse> data, PopupWindowCodeItemSelectInterface selectInterface) {
        mContext = context;
        mData = data;
        mSelectInterface = selectInterface;
    }

PopupWindowCodeItemSelectInterface 是用于選擇PopupWindow中列表的某一項

public interface PopupWindowCodeItemSelectInterface {
    //這里按照自己的數(shù)據(jù)源做更改悔政,我這里的數(shù)據(jù)源是通過數(shù)據(jù)字典接口獲取,同事需要用到文字對應(yīng)的編碼
    public void onItemSelected(CodeDataResponse item);
}

完整代碼如下:

public class JobPopupWindowAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<CodeDataResponse> mData;
    private PopupWindowCodeItemSelectInterface mSelectInterface;

    public JobPopupWindowAdapter(Context context,ArrayList<CodeDataResponse> data, PopupWindowCodeItemSelectInterface selectInterface) {
        mContext = context;
        mData = data;
        mSelectInterface = selectInterface;
    }


    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.area_code_item_view, null);
        }

//        String areaCodeStr = mData.get(position);
        CodeDataResponse codeDataResponse = mData.get(position);

        TextView areaCode = (TextView) convertView.findViewById(R.id.area_code);
        areaCode.setText(codeDataResponse.aaa103);
        convertView.setTag(codeDataResponse);
        convertView.setOnClickListener(mOnClickListener);

        return convertView;
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (mSelectInterface != null) {
                CodeDataResponse  mCodeDataResponse = (CodeDataResponse)v.getTag();
//                String code = String.valueOf(v.getTag());
                mSelectInterface.onItemSelected(mCodeDataResponse);
            }
        }
    };

}
3桥言、封裝通用的顯示PopupWindow的方法:showPupupWindow(JobPopupWindowAdapter amAdapter,View view)
/**
     * 選擇的popupwindow
     *  amAdapter   適配器
     *  view        需要在對應(yīng)的那個控件中顯示
     */
    private void showPupupWindow(JobPopupWindowAdapter amAdapter,View view) {
        mView = view;
        //PupupWindow的寬
        int windowWidth = 450;//這里可以修改成動態(tài)修改成控件的長度
        //PupupWindow的高
        int windowHeight = 300;
        if (mPopupWindowWindow == null) {
            View contentView = LayoutInflater.from(mContext).inflate(R.layout.popup_window_listview_layout, null);
            mPopupWindowWindow = new PopupWindow(contentView, windowWidth, windowHeight);
            // 設(shè)置點擊屏幕其它地方彈出框消失
            mPopupWindowWindow.setFocusable(true);
            mPopupWindowWindow.setOutsideTouchable(true);
            mPopupWindowWindow.setBackgroundDrawable(new BitmapDrawable());

            ListView listView = (ListView) contentView.findViewById(R.id.listview);
            listView.setAdapter(amAdapter);
        }
        if (!mPopupWindowWindow.isShowing()) {
            int[] location = new int[2];
            view.getLocationOnScreen(location); // 獲取組件在屏幕中的位置

            mPopupWindowWindow.showAsDropDown(view,  0, 0);
        }
    }
4萌踱、點擊PopupWindow列表中具體某一項,PopupWindow消失的方法 hidePopupWindow()
    /**
     * 點擊item清除PopupWindow
     */
    private void hidePopupWindow() {
        if (mPopupWindowWindow == null) {
            LogUtils.d("mPopupWindowWindow == null");
        }
        if (mPopupWindowWindow != null && mPopupWindowWindow.isShowing()) {
            mPopupWindowWindow.dismiss();
            LogUtils.d("hidePopupWindow=====dismiss");
        }
    }
5号阿、調(diào)用步驟

首先在對應(yīng)的Activity和Fragment中實現(xiàn)選擇的PopupWindowCodeItemSelectInterface 接口并鸵,實現(xiàn)對應(yīng)的方法

ublic class WriteJobIntentionInfoActivity extends BaseActivity implements PopupWindowCodeItemSelectInterface{
        @Override
    public void onItemSelected(CodeDataResponse item) {
        if (mView.getId() == rltJobs.getId()){
            etJobs.setText(item.aaa103);
            mJobNameNum = item.aaa102;
        }else  if (mView.getId() == rltSalary.getId()){
            etSalary.setText(item.aaa103);
            mSalaryNum = item.aaa102;
        }else  if (mView.getId() == rltWorkTime.getId()){
            etWorkTime.setText(item.aaa103);
        }
        hidePopupWindow();
    }
}

接著在需要展示PopupWindow的地方,調(diào)用其方法扔涧,我這里是點擊EdText的時候园担,onClick中的代碼如下:

if (v.equals(etJobs)){  //求職崗位
            setDataToPopupWindow(mRPCodeLists,this,rltJobs);
        }else if (v.equals(etSalary)){  //薪資要求
            setDataToPopupWindow(mRSCodeLists,this,rltSalary);
        }else if (v.equals(etWorkTime)){  //到崗時間
            setDataToPopupWindow(mWDCodeLists,this,rltWorkTime);
        }

setDataToPopupWindow(mWDCodeLists,this,rltWorkTime);方法對之前的統(tǒng)一封裝的PopupWindow展示的方法showPupupWindow()再做一次封裝,為了調(diào)用方便

    /**
     * 組裝顯示poupwindow的方法
     * @param mInterface
     */
    private void setDataToPopupWindow(List<CodeDataResponse> list, PopupWindowCodeItemSelectInterface mInterface, View editTextView){
        mCodes.clear();
        mCodes.addAll(list);
        mJobPopupWindowAdapter = new JobPopupWindowAdapter(mContext,mCodes, mInterface);
        showPupupWindow(mJobPopupWindowAdapter,editTextView);
    }

好了枯夜,這樣子就應(yīng)該可以實現(xiàn)上面動圖的效果弯汰!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市湖雹,隨后出現(xiàn)的幾起案子咏闪,更是在濱河造成了極大的恐慌,老刑警劉巖摔吏,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件鸽嫂,死亡現(xiàn)場離奇詭異纵装,居然都是意外死亡,警方通過查閱死者的電腦和手機溪胶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進店門搂擦,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人哗脖,你說我怎么就攤上這事瀑踢。” “怎么了才避?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵橱夭,是天一觀的道長。 經(jīng)常有香客問我桑逝,道長棘劣,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任楞遏,我火速辦了婚禮茬暇,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘寡喝。我一直安慰自己糙俗,他們只是感情好,可當我...
    茶點故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布预鬓。 她就那樣靜靜地躺著巧骚,像睡著了一般。 火紅的嫁衣襯著肌膚如雪格二。 梳的紋絲不亂的頭發(fā)上劈彪,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天,我揣著相機與錄音顶猜,去河邊找鬼沧奴。 笑死,一個胖子當著我的面吹牛驶兜,可吹牛的內(nèi)容都是我干的扼仲。 我是一名探鬼主播,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼抄淑,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了驰后?” 一聲冷哼從身側(cè)響起肆资,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎灶芝,沒想到半個月后郑原,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體唉韭,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年犯犁,在試婚紗的時候發(fā)現(xiàn)自己被綠了属愤。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡酸役,死狀恐怖住诸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情涣澡,我是刑警寧澤贱呐,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站入桂,受9級特大地震影響奄薇,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜抗愁,卻給世界環(huán)境...
    茶點故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一馁蒂、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蜘腌,春花似錦沫屡、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至劫瞳,卻和暖如春倘潜,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背志于。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工涮因, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人伺绽。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓养泡,卻偏偏與公主長得像,于是被迫代替她去往敵國和親奈应。 傳聞我的和親對象是個殘疾皇子澜掩,可洞房花燭夜當晚...
    茶點故事閱讀 45,066評論 2 355

推薦閱讀更多精彩內(nèi)容