美文网首页
PopWindow 和 Dialog 区别

PopWindow 和 Dialog 区别

作者: zhengLH | 来源:发表于2018-12-27 20:07 被阅读8次

【代码】

pWindow = new PopupWindow(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final View view = LayoutInflater.from(getContext()).inflate(R.layout.popup_window_filtrate, null);
    final ListView lv_province = (ListView) view.findViewById(R.id.lv_province);

   // 省份:转换成String[]
    provinceSize = pcaBean.getProvince().size();
    final String[] proviceNames = new String[provinceSize];
    for (int i = 0; i < provinceSize; i++) {
        proviceNames[i] = pcaBean.getProvince().get(i).getName();
    }

       final ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), R.layout.item_lv_filter, R.id.tv_content, proviceNames);
    lv_province.setAdapter(adapter);
    lv_province.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

             // todo
        }
    });

 // popWindow 以外的空白处点击
   view.findViewById(R.id.fl_root).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pWindow.dismiss();
        }
    });

   pWindow.setContentView(view);
    //点击空白区域PopupWindow消失,这里必须先设置setBackgroundDrawable,否则点击无反应
    pWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffffffff")));
    pWindow.setFocusable(true);
    pWindow.setOutsideTouchable(true);
    //设置PopupWindow动画
    // pWindow.setAnimationStyle(R.style.AnimDown);
    // PopupWindow在targetView控件下方弹出
    pWindow.showAsDropDown(mLlGoodsType);

【xml 布局】

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
    <ListView
        android:id="@+id/lv_province"
        android:divider="@null"
        android:listSelector="@drawable/vest_selector_rect_gray"
        android:scrollbars="none"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </ListView>
 </FrameLayout>

【每一个item】

 <?xml version="1.0" encoding="utf-8"?>
 <TextView
android:background="@drawable/vest_selector_rect_gray"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tv_content"
android:layout_width="match_parent"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:paddingLeft="20dp"
android:paddingRight="12dp"
android:gravity="left"
android:text="省份"
android:textColor="@color/black"
android:layout_height="wrap_content">
 </TextView>

【popWindow工具类】

 public class FilterPopupWindow {

public static class Builder{
    private WeakReference<Context> mWeakContext;
    private int mHeight = -1;

    public Builder(Context context) {
        mWeakContext = new WeakReference<Context>(context);
    }

    public Builder height(int height){
        mHeight = height;
        return this;
    }

    public PopupWindow create(ViewGroup target, String[] datas,
                              AdapterView.OnItemClickListener itemClickListener,
                              final OnAllClickLister onAllClickLister){

        PopupWindow pWindow = null;

        Context context = mWeakContext.get();

        if(context != null) {
            pWindow = new PopupWindow(context);

            View view = LayoutInflater.from(context).inflate(R.layout.popup_window_filter_single_listview, target, false);
            ListView lv = (ListView) view.findViewById(R.id.lv);
            final PopupWindow finalPWindow = pWindow;
            view.findViewById(R.id.fl_root).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finalPWindow.dismiss();
                }
            });
            view.findViewById(R.id.tv_all).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onAllClickLister.onAllClick();
                }
            });
            ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.item_lv_filter, R.id.tv_content, datas);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(itemClickListener);
            pWindow.setContentView(view);
            pWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#30000000")));
            pWindow.setFocusable(true);
            pWindow.setOutsideTouchable(true);

            pWindow.setWidth(context.getResources().getDisplayMetrics().widthPixels);

            if(mHeight != -1){
                pWindow.setHeight(mHeight);
            }
        }

     int xoff = (view.getWidth() - mYMDPWindow.getWidth()) / 2;  // 控制在某一个控件下水平居中 需要偏移量
    // 第一个参数 是以他的左下角 为参考坐标
    mYMDPWindow.showAsDropDown(tvDate, xoff, 25, Gravity.CENTER_HORIZONTAL);  // 设置偏移位置(x轴, y轴)

        return pWindow;
    }

}

public interface OnAllClickLister {

    void onAllClick();

}

【xml 布局】

 <?xml version="1.0" encoding="utf-8"?>
 <FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fl_root"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:background="@color/actionbarBackground"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="360dp">

<TextView
    android:id="@+id/tv_all"
    android:text="不限"
    android:paddingLeft="20dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:gravity="left"
    android:textSize="16sp"
    android:textColor="@color/black"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<ListView
    android:id="@+id/lv"
    android:layout_width="match_parent"
    android:layout_height="320dp"
    android:divider="@null"
    android:scrollbars="none">
</ListView>

</LinearLayout>

</FrameLayout>

相关文章

网友评论

      本文标题:PopWindow 和 Dialog 区别

      本文链接:https://www.haomeiwen.com/subject/rqhdlqtx.html