本文主要是介绍popwindow创建以及事件拦截与内部包含checkbox选中,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1:需求一个这样的弹窗覆盖
2:界面搭建以及popuwidow弹出动画效果
private void cate_screen_poppu() {View view = LayoutInflater.from(this).inflate(R.layout.cate_screen_popup, null);final CheckBox checkbox_all = (CheckBox) view.findViewById(R.id.checkbox_all);final CheckBox checkbox_serial = (CheckBox) view.findViewById(R.id.checkbox_serial);final CheckBox checkbox_free = (CheckBox) view.findViewById(R.id.checkbox_free);final CheckBox checkbox_vip = (CheckBox) view.findViewById(R.id.checkbox_vip);final RelativeLayout rl_checkbox_serial = (RelativeLayout) view.findViewById(R.id.rl_checkbox_serial);final RelativeLayout rl_checkbox_all = (RelativeLayout) view.findViewById(R.id.rl_checkbox_all);final RelativeLayout rl_checkbox_free = (RelativeLayout) view.findViewById(R.id.rl_checkbox_free);final RelativeLayout rl_checkbox_vip = (RelativeLayout) view.findViewById(R.id.rl_checkbox_vip);boolean all = PrefUtils.getBoolean(UiUtils.getContext(), "checkbox_all", true);if(all){checkbox_all.setChecked(true);}else{checkbox_all.setChecked(false);}boolean serial = PrefUtils.getBoolean(UiUtils.getContext(), "checkbox_serial", true);if(serial){checkbox_serial.setChecked(true);}else{checkbox_serial.setChecked(false);}boolean free = PrefUtils.getBoolean(UiUtils.getContext(), "checkbox_free", true);if(free){checkbox_free.setChecked(true);}else{checkbox_free.setChecked(false);}boolean vip = PrefUtils.getBoolean(UiUtils.getContext(), "checkbox_vip", true);if(vip){checkbox_vip.setChecked(true);}else{checkbox_vip.setChecked(false);}Button ok_button = (Button) view.findViewById(R.id.ok_button);Button resetting_button = (Button) view.findViewById(R.id.resetting_button);WindowManager manger = (WindowManager) getSystemService(CatagaryActivity.WINDOW_SERVICE);@SuppressWarnings("deprecation")int width = manger.getDefaultDisplay().getWidth()/4;int heigh = manger.getDefaultDisplay().getHeight();mWindow = new PopupWindow(view,3*width, heigh-getStatusBarHeight(this));mWindow.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));mWindow.setOutsideTouchable(true);mWindow.setTouchable(true);mWindow.setFocusable(true);mWindow.update();View parent = LayoutInflater.from(this).inflate(R.layout.catagary_activity, null);//为popWindow添加动画效果mWindow.setAnimationStyle(R.style.popWindow_animation);// 点击弹出泡泡窗口mWindow.showAtLocation(parent, Gravity.RIGHT, 0,getStatusBarHeight(this));//popupWindow在弹窗的时候背景半透明final WindowManager.LayoutParams params = getWindow().getAttributes();params.alpha = 0.5f;getWindow().setAttributes(params);mWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {@Overridepublic void onDismiss() {params.alpha = 1.0f;getWindow().setAttributes(params);}});//判断是否选中切换书籍筛选状态resetting_button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {checkbox_all.setChecked(true);checkbox_serial.setChecked(true);checkbox_free.setChecked(true);checkbox_vip.setChecked(true);}});ok_button.setOnClickListener(new View.OnClickListener() {public void onClick(View view) {boolean checkbox_allSelected = checkbox_all.isChecked();Log.e("glk",checkbox_allSelected+"");boolean checkbox_serialSelected = checkbox_serial.isChecked();boolean checkbox_freeSelected = checkbox_free.isChecked();boolean checkbox_vipSelected = checkbox_vip.isChecked();if(checkbox_allSelected&&checkbox_serialSelected&&checkbox_freeSelected&&checkbox_vipSelected){radio_cate_screen_book.setTextColor(Color.BLACK);}else{radio_cate_screen_book.setTextColor(Color.RED);}if((checkbox_allSelected&&checkbox_serialSelected&&checkbox_freeSelected&&checkbox_vipSelected)||(!checkbox_allSelected&&!checkbox_serialSelected&&!checkbox_freeSelected&&!checkbox_vipSelected)){mProcess="";mIsVip="";presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");mAdapter.notifyDataSetChanged();}else if(checkbox_allSelected&&!checkbox_serialSelected&&checkbox_freeSelected&&checkbox_vipSelected){mProcess="2";mIsVip="";presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");mAdapter.notifyDataSetChanged();}else if(checkbox_allSelected&&!checkbox_serialSelected&&!checkbox_freeSelected&&checkbox_vipSelected){mProcess="2";mIsVip="0";presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");mAdapter.notifyDataSetChanged();}else if(checkbox_allSelected&&!checkbox_serialSelected&&checkbox_freeSelected&&!checkbox_vipSelected){mProcess="2";mIsVip="1";presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");mAdapter.notifyDataSetChanged();}else if(!checkbox_allSelected&&checkbox_serialSelected&&checkbox_freeSelected&&checkbox_vipSelected){mProcess="1";mIsVip="";presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");mAdapter.notifyDataSetChanged();}else if(!checkbox_allSelected&&checkbox_serialSelected&&!checkbox_freeSelected&&checkbox_vipSelected){mProcess="1";mIsVip="0";presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");mAdapter.notifyDataSetChanged();}else if(!checkbox_allSelected&&checkbox_serialSelected&&checkbox_freeSelected&&!checkbox_vipSelected){mProcess="1";mIsVip="1";presenter.getBookCategaryList(mCid, mOrder, mProcess, mIsVip, mPage,"10");mAdapter.notifyDataSetChanged();}mWindow.dismiss();}});checkbox_all.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton button, boolean b) {checkbox_all.setChecked(b? true:false);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_all",b);}});checkbox_serial.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton button, boolean b) {checkbox_serial.setChecked(b? true:false);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_serial",b);}});checkbox_free.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton button, boolean b) {checkbox_free.setChecked(b? true:false);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_free",b);}});checkbox_vip.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton button, boolean b) {checkbox_vip.setChecked(b? true:false);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_vip",b);}});rl_checkbox_serial.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {boolean checked = checkbox_serial.isChecked();if(checked){checkbox_serial.setChecked(false);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_serial",false);}else{checkbox_serial.setChecked(true);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_serial",true);}Log.e("glk", "ipone7pluss");}});rl_checkbox_all.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {boolean checked = checkbox_all.isChecked();if(checked){checkbox_all.setChecked(false);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_all",false);}else{checkbox_all.setChecked(true);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_all",true);}Log.e("glk", "ipone7plusa");}});rl_checkbox_free.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {boolean checked = checkbox_free.isChecked();if(checked){checkbox_free.setChecked(false);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_free",false);}else{checkbox_free.setChecked(true);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_free",true);}Log.e("glk", "ipone7plusf");}});rl_checkbox_vip.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {boolean checked = checkbox_vip.isChecked();if(checked){checkbox_vip.setChecked(false);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_vip",false);}else{checkbox_vip.setChecked(true);PrefUtils.putBoolean(UiUtils.getContext(),"checkbox_vip",true);}Log.e("glk", "ipone7plusv");}});}
3:动画效果
//为popWindow添加动画效果mWindow.setAnimationStyle(R.style.popWindow_animation);
进出的动画style
<style name="popWindow_animation"><item name="android:windowEnterAnimation">@anim/popupwindow_enter</item><item name="android:windowExitAnimation">@anim/popupwindow_exit</item></style>
进出的xml文件
4:我的pop xml文件
<?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="match_parent"android:background="#fff"android:layout_marginLeft="@dimen/dm200"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:gravity="left"android:text="都市情感"android:padding="@dimen/dm030"android:textColor="@color/editTextColor"android:background="@drawable/selector_green_textview"android:textSize="@dimen/font_size_large_high"android:layout_height="wrap_content"/><Viewandroid:layout_width="match_parent"android:layout_height="@dimen/dm001"android:background="#d2d2d2"/><TextViewandroid:layout_width="match_parent"android:gravity="left"android:text="书籍状态"android:padding="@dimen/dm040"android:textColor="@color/accountColor"android:background="@drawable/selector_green_textview"android:textSize="@dimen/font_size_large"android:layout_height="wrap_content"/>
<RelativeLayoutandroid:id="@+id/rl_checkbox_serial"android:layout_width="match_parent"android:padding="@dimen/dm020"android:background="@drawable/selector_green_textview"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:text="连载"android:layout_marginLeft="@dimen/dm030"android:layout_centerInParent="true"android:textColor="@color/editTextColor"android:textSize="@dimen/font_size_middle"android:layout_alignParentLeft="true"android:layout_height="wrap_content"/><CheckBoxandroid:id="@+id/checkbox_serial"android:layout_width="wrap_content"android:checked="true"android:layout_alignParentRight="true"android:layout_centerInParent="true"android:button="@drawable/check_box_selector"android:layout_height="wrap_content"/>
</RelativeLayout><RelativeLayoutandroid:id="@+id/rl_checkbox_all"android:layout_width="match_parent"android:padding="@dimen/dm020"android:background="@drawable/selector_green_textview"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:text="完本"android:layout_marginLeft="@dimen/dm030"android:layout_centerInParent="true"android:textColor="@color/editTextColor"android:textSize="@dimen/font_size_middle"android:layout_alignParentLeft="true"android:layout_height="wrap_content"/><CheckBoxandroid:id="@+id/checkbox_all"android:checked="true"android:layout_width="wrap_content"android:layout_alignParentRight="true"android:layout_centerInParent="true"android:button="@drawable/check_box_selector"android:layout_height="wrap_content"/></RelativeLayout><Viewandroid:layout_width="match_parent"android:layout_height="@dimen/dm001"android:background="#d2d2d2"/><TextViewandroid:layout_width="match_parent"android:gravity="left"android:text="收费状态"android:padding="@dimen/dm040"android:textColor="@color/accountColor"android:background="@drawable/selector_green_textview"android:textSize="@dimen/font_size_large"android:layout_height="wrap_content"/><RelativeLayoutandroid:id="@+id/rl_checkbox_free"android:layout_width="match_parent"android:padding="@dimen/dm020"android:background="@drawable/selector_green_textview"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:text="免费"android:layout_marginLeft="@dimen/dm030"android:layout_centerInParent="true"android:textColor="@color/editTextColor"android:textSize="@dimen/font_size_middle"android:layout_alignParentLeft="true"android:layout_height="wrap_content"/><CheckBoxandroid:id="@+id/checkbox_free"android:checked="true"android:layout_width="wrap_content"android:layout_alignParentRight="true"android:layout_centerInParent="true"android:button="@drawable/check_box_selector"android:layout_height="wrap_content"/></RelativeLayout><RelativeLayoutandroid:id="@+id/rl_checkbox_vip"android:layout_width="match_parent"android:padding="@dimen/dm020"android:background="@drawable/selector_green_textview"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:text="VIP"android:layout_marginLeft="@dimen/dm030"android:layout_centerInParent="true"android:textColor="@color/editTextColor"android:textSize="@dimen/font_size_middle"android:layout_alignParentLeft="true"android:layout_height="wrap_content"/><CheckBoxandroid:id="@+id/checkbox_vip"android:checked="true"android:layout_width="wrap_content"android:layout_alignParentRight="true"android:layout_centerInParent="true"android:button="@drawable/check_box_selector"android:layout_height="wrap_content"/></RelativeLayout>
<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_alignParentBottom="true"android:layout_width="match_parent"android:orientation="horizontal"android:layout_marginBottom="@dimen/dm010"android:layout_height="wrap_content"><Buttonandroid:id="@+id/resetting_button"android:layout_width="0dp"android:layout_weight="1"android:text="重置"android:background="@color/backgroundDark"android:textColor="@color/colorPrimary"android:layout_height="match_parent"/><Buttonandroid:id="@+id/ok_button"android:layout_width="0dp"android:layout_weight="1"android:text="确定"android:background="@color/colorPrimary"android:layout_height="match_parent"/></LinearLayout>
</RelativeLayout>
</LinearLayout>
5:要想解决点击阴影部分弹窗消失,但是又不能触发弹窗下面的recycleview条目的点击事件加上这样几行
不要设置那个外部触摸的监听,因为无论你返回true 还是false,都会出现状况,要么下方按钮点击没响应要么弹窗消失不了要么。。。。很多种都试试了,真恶心
mWindow.setOutsideTouchable(true);mWindow.setTouchable(true);mWindow.setFocusable(true);
6:有疑问联系18792586740@163.com
福利地址点进来吧
这篇关于popwindow创建以及事件拦截与内部包含checkbox选中的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!