自己動手擼一個Android中PopupWindow

效果圖如下:
GIF.gif
實現流程:
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,構造方法中將數據源以及選擇某一項數據的回調方法傳進來

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

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

public interface PopupWindowCodeItemSelectInterface {
    //這里按照自己的數據源做更改,我這里的數據源是通過數據字典接口獲取,同事需要用到文字對應的編碼
    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        需要在對應的那個控件中顯示
     */
    private void showPupupWindow(JobPopupWindowAdapter amAdapter,View view) {
        mView = view;
        //PupupWindow的寬
        int windowWidth = 450;//這里可以修改成動態修改成控件的長度
        //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);
            // 設置點擊屏幕其它地方彈出框消失
            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、調用步驟

首先在對應的Activity和Fragment中實現選擇的PopupWindowCodeItemSelectInterface 接口,實現對應的方法

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的地方,調用其方法,我這里是點擊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);方法對之前的統一封裝的PopupWindow展示的方法showPupupWindow()再做一次封裝,為了調用方便

    /**
     * 組裝顯示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);
    }

好了,這樣子就應該可以實現上面動圖的效果!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容