自定义Popwin

2020-08-14  本文已影响0人  我家猫猫不睡觉

我的写法

public class TestPopWin extends PopupWindow {
    
    private Context mContext;
    private View mView;
    private ImageView mBtnClose;
    private TextView mTv1,mTv2,mTv3;
    private LinearLayout mLlChoice;
    private Button mBtn;

     public TestPopWin (Context context, View.OnClickListener itemsOnClick,
                           String value,String value2,String value3) {
        
        this.mView = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.popwindow_test, null);
        /**
         * init View
         */
        mBtnClose = mView.findViewById(R.id.btn_close);
        mTv1 = mView.findViewById(R.id.tv_1);
        mTv2 = mView.findViewById(R.id.tv_2);
        mTv3 = mView.findViewById(R.id.tv_3);
        mLlChoice = mView.findViewById(R.id.ll_choice);
        mBtn = mView.findViewById(R.id.btn);
    

         /**
         * setViewData
         */
        mTv1.setText(value);
        mTv2.setText(value2);
        mTv3.setText(value3);
        //关闭
        mBtnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

         //按钮监听
        mLlChoice.setOnClickListener(itemsOnClick);
        mBtn.setOnClickListener(itemsOnClick);

         //外部点击
        this.setOutsideTouchable(true);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                int height = view.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP && y < height) {
                    dismiss();
                }
                return true;
            }
        });

         /**
         * 设置弹出窗口特征
         */
        //设置视图
        this.setContentView(this.mView);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体可点击
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        // 设置弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // 设置弹出窗体显示时的动画,从底部向上弹出
        this.setAnimationStyle(R.style.PopWinAnim);
    }
}
  mTestPopWin = new TestPopWin(this, this, value, value2, value3);
  mTestPopWin .showAtLocation(findViewById(R.id.activity_rootview), Gravity.CENTER, 0, 0);
  //记得显示在activity的rootview

点击事件直接在activity的onClick方法中使用

 switch (view.getId()) {
    case R.id.ll_choice: 
                
             break;
    case R.id.btn:    

             break;
}

同事写法

三个底部按钮通用 popwindow 弹框, 采用构造者模式

public class ThreeBtnPopWindow extends PopupWindow {

    protected ThreeBtnPopWindow (Context context) {
        this(context, null);
    }

    protected ThreeBtnPopWindow (Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    protected ThreeBtnPopWindow (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void show(View view) {
        this.showAtLocation(view, Gravity.CENTER, 80, 0);
    }

    public static class Builder {

        private ThreeBtnPopWindow popWindow;
        private Context mContext;
        private View mView; // 视图
        private Button topConfirm; // 确认
        private Button midConfirm; // 确认
        private Button cancel; // 取消

        private PopDialogInterface mPopListener;

         public Builder(@NonNull Context context) {
            this.mContext = context.getApplicationContext();
            this.popWindow = new ThreeBtnPopWindow (mContext);
            setContentView();
        }

        public Builder(@NonNull Context context, int layoutId) {
            this.mContext = context.getApplicationContext();
            this.popWindow = new ThreeBtnPopWindow (mContext);
            setContentView(layoutId);
        }

        //默认布局
        private Builder setContentView() {
             mView = LayoutInflater.from(mContext).inflate(R.layout.popwindow_three, null);
            topConfirm = mView.findViewById(R.id.top_button);
            midConfirm = mView.findViewById(R.id.mid_button);
            cancel = mView.findViewById(R.id.cancel_button);
            cancel.setBottom(80);
             topConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mPopListener != null) {
                        mPopListener.onClick(0);
                    }
                }
            });
            midConfirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mPopListener != null) {
                        mPopListener.onClick(1);
                    }
                }
            });
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mPopListener != null) {
                        mPopListener.onClick(2);
                    }
                }
            });
            return this;
        }
        
         private Builder setContentView(int layoutId){
            mView = LayoutInflater.from(mContext).inflate(layoutId, null);
            return this;
        }

         /**
         * 设置顶部按钮文本
         * @return
         */
        public Builder setMessageTop(String msg){
            if (topConfirm != null) {
                topConfirm.setText(msg);
            }
            return this;
        }

         /**
         * 设置中间按钮文本
         * @return
         */
        public Builder setMessageMid(String msg){
            if (midConfirm != null) {
                midConfirm.setText(msg);
            }
            return this;
        }

        /**
         * 设置底部按钮文本
         * @return
         */
        public Builder setMessageBot(String msg){
            if (cancel != null) {
                cancel.setText(msg);
            }
            return this;
        }

        public Builder setClickListener(PopDialogInterface popDialogInterface) {
            this.mPopListener = popDialogInterface;
            return this;
        }

         public ThreeBtnPopWindow  build() {
            popWindow.setContentView(mView);
            // 设置弹出窗体的宽和高
            popWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            popWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
            popWindow.setFocusable(true);// 使其聚集
            popWindow.setOutsideTouchable(true);
            //实例化一个ColorDrawable颜色为半透明
            ColorDrawable dw = new ColorDrawable(0xb0000000);
            //实例化一个ColorDrawable颜色为全透明!!!为了和window底色融合,设置#00ffffff
//            ColorDrawable dw = new ColorDrawable(mContext.getResources().getColor(R.color.transparent));
            popWindow.setBackgroundDrawable(dw);
            popWindow.setAnimationStyle(R.style.PopWinAnim);
            return popWindow;
        }
    }
}


//接口
public interface PopDialogInterface {
    /**
     * 0 为顶部按钮点击, 1 为中间按钮点击  2 为 底部按钮点击
     * @param index
     */
    void onClick(int index);
}
 if (threePopWindow == null) {
    threePopWindow = new ThreeBtnPopWindow  .Builder(mContext)
      .setClickListener(new PopDialogInterface() {
                                @Override
                                public void onClick(int index) {
                                    switch (index) {
                                        case 0 : 
                                            break;
                                        case 1 : 
                                    }
                                    if (threePopWindow != null) {
                                        threePopWindow .dismiss();
                                    }
                                }
                            })
                            .build();
}
threePopWindow .show(container); //activity rootview
 <style name="PopWinAnim" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/pop_enter_anim</item>
        <item name="android:windowExitAnimation">@anim/pop_exit_anim</item>
  </style>


//pop_enter_anim

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate
    android:duration="200"
    android:fromYDelta="100%p"
    android:toYDelta="0" />
<alpha
    android:duration="200"
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />
</set>

//pop_exit_anim

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="200"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
    <alpha
        android:duration="200"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

RecyclerViewPopwin

指定空间旁边弹出列表Popwindow

public class ScreenPopWin extends PopupWindow  {

    private View mView;
    private final RecyclerView mRecycleView;
    private ScreenInfo mScreenInfo;

    public ScreenPopWin(Context context, ScreenInfo screenInfo, final List<ScreenBean> items, int selectedPos) {

        this.mContext = context;
        this.mScreenInfo = screenInfo;
        this.mView = LayoutInflater.from(context).inflate(R.layout.popwin_screen, null);

        /**
         * init View
         */
        mRecycleView = mView.findViewById(R.id.recycle_view);
        GridLayoutManager layoutManager = new GridLayoutManager(context,3);
        mRecycleView.setLayoutManager(layoutManager);
        ScreenAdapter adapter = new ScreenAdapter(context, items);
        mRecycleView.setAdapter(adapter);
        adapter.setDefSelect(selectedPos);
        adapter.setOnItemListener(new ScreenAdapter.OnItemListener(){
            @Override
            public void onClick(View view, int position) {
                ScreenBean bean = items.get(position);
                if (mScreenInfo!=null){
                    mScreenInfo.screenFinish(bean,position);
                    dismiss();
                }
            }
        });

        //外部点击
        this.setOutsideTouchable(true);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.mView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                int height = view.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP && y < height) {
                    dismiss();
                }
                return true;
            }
        });

        /**
         * 设置弹出窗口特征
         */
        //设置视图
        this.setContentView(this.mView);
        // 设置弹出窗体的宽和高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        this.setWidth(RelativeLayout.LayoutParams.WRAP_CONTENT);
        // 设置弹出窗体可点击
        this.setFocusable(true);
        // 实例化一个ColorDrawable颜色为半透明
        //ColorDrawable dw = new ColorDrawable(0xb0eeeeee);
        // 设置弹出窗体的背景
        //this.setBackgroundDrawable(dw);
        // 设置弹出窗体显示时的动画,从底部向上弹出
        //this.setAnimationStyle(R.style.PayDetailPopWinAnim);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pop_layout"
    android:background="@drawable/background_radius_5_e5e5"
    android:layout_width="@dimen/dp_360"
    android:layout_height="wrap_content">
    <androidx.recyclerview.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recycle_view"
        android:layout_margin="@dimen/dp_5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>
public class ScreenAdapter extends RecyclerView.Adapter<ScreenAdapter.MyViewHolder> {

    private Context context;
    private List<ScreenBean> items;
    private int defItem = -1;   //默认值
    private OnItemListener onItemListener;
    public ScreenAdapter(Context context, List list) {
        this.context = context;
        this.items = list;
    }

    public void setOnItemListener(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    public interface OnItemListener {
        void onClick(View view, int position);
    }

    public void setDefSelect(int position) {
        this.defItem = position;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       final MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.item_screen, parent, false));
        //添加选中的打勾显示
        holder.tvValue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //将点击的位置传出去
                defItem = holder.getAdapterPosition();
                if (onItemListener != null) {
                    onItemListener.onClick(v, defItem);
                }
                holder.tvValue.setTextColor(context.getResources().getColor(R.color.color_0099FF));
                //刷新界面 notify 通知Data 数据set设置Changed变化
                //在这里运行notifyDataSetChanged 会导致下面的onBindViewHolder 重新加载一遍
                notifyDataSetChanged();
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ScreenBean bean = items.get(position);
        holder.tvValue.setText(bean.billType);
        if (defItem != -1) {
            if (defItem == position) {
                holder.tvValue.setTextColor(context.getResources().getColor(R.color.color_0099FF));
            } else {
                holder.tvValue.setTextColor(context.getResources().getColor(R.color.color_333333));
            }
        }
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    /**
     * ViewHolder的类,用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tvValue;
        public MyViewHolder(View view) {
            super(view);
            tvValue = view.findViewById(R.id.tv_value);
        }
    }
}

1:接口

//activity 继承ScreenInfo 接口
public interface ScreenInfo {
    void screenFinish(ScreenBean bean, int position);
}

2:调用


private int selected = 0;           //默认选中
//显示弹窗
private void showScreenPopwin() {
        mPopWin = new ScreenPopWin(this, this, this, mList, selected);
        mPopWin.showAsDropDown(mIvArrow); //mIvArrow需要显示在哪个控件旁边
    }

//选中回调
 @Override
    public void screenFinish(ScreenBean bean, int position) {
        //筛选条件选择结束刷新数据
        selected = position;
       //逻辑处理
    }
上一篇 下一篇

猜你喜欢

热点阅读