android 在PopupWindow中使用ListView做下拉选择功能

效果图如下所示

android 在PopupWindow中使用ListView做下拉选择功能_第1张图片
image.png
android 在PopupWindow中使用ListView做下拉选择功能_第2张图片
image.png

PopupWindow布局:layout_insurance_list_pop_up.xml文件




    
    


ListView列表Item布局:layout_item_insurance_list.xml文件




    


适配器Adapter:InsurancePopupWindowAdapter.java文件

public class InsurancePopupWindowAdapter extends BaseAdapter {

    private Context mContext;
    private List mData;
    private final LayoutInflater inflater;

    public InsurancePopupWindowAdapter(Context context,List mData){
        this.mContext = context;
        this.mData = mData;
        inflater = LayoutInflater.from(mContext);
    }

    @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) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.layout_item_insurance_list, null);
            holder.tvName =  (TextView) convertView.findViewById(R.id.tv_insurance);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }

        SysBean bean = mData.get(position);

        holder.tvName.setText(bean.name);

        return convertView;
    }


    final class ViewHolder{
        public  TextView tvName;
    }

}

调用方式

private void showPopup() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_insurance_list_pop_up,null);//PopupWindow对象
        listView = (ListView)view.findViewById(R.id.lv);
        View rootView  = (LinearLayout)view.findViewById(R.id.llt_root);
        listView.setAdapter(adapter);
        if (popupWindow != null){
            popupWindow = null;
        }

        //初始化PopupWindow对象
        popupWindow=new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        // 设置滑入滑出的动画效果
        popupWindow.setAnimationStyle(R.style.style_pop_up_window2);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView adapterView, View view, int i, long l) {
                SysBean bean = sysBeanList.get(i);
                if (bean != null){
                    tvInsurane.setText(!TextUtils.isEmpty(bean.name)?bean.name:"");
                }
                if (popupWindow != null && popupWindow.isShowing()){
                    popupWindow.dismiss();
                }
            }
        });


        //点击外面让popupwindow消失
        rootView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (popupWindow != null && popupWindow.isShowing()){
                    popupWindow.dismiss();
                }
                return false;
            }
        });

        //让popupwindow在底部显示
        popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
    }

你可能感兴趣的:(android 在PopupWindow中使用ListView做下拉选择功能)