效果图:
PopupWindow的知识点
//方法一:
public PopupWindow (Context context)
//方法二:
public PopupWindow(View contentView)
//方法三:
public PopupWindow(View contentView, int width, int height)
//方法四:
public PopupWindow(View contentView, int width, int height, boolean focusable)
showAsDropDown(view);//相对某个控件的位置(正左下方),无偏移
showAtLocation(contentView,Gravity.BOTTOM,0,0); //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
setOutsideTouchable(true);//点击popupwindow外部消失
setFocusable(true);//获取焦点
setOnDismissListener(this);//加上上面两句让popupwindow自动消失
setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_pupwindow));// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
mPopuoWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//pop消失的时候的监听
mSupplierListTvProduct.setTextColor(Color.parseColor("#5a5959"));
mSupplierListTvSort.setTextColor(Color.parseColor("#5a5959"));
mSupplierListTvActivity.setTextColor(Color.parseColor("#5a5959"));
}
});
popupWindow消失时的监听,消失时取消按钮的选中的状态
public void onClick(View view) {
switch (view.getId()){
case R.id.around_supplier_list_product:
mSupplierListTvProduct.setTextColor(Color.parseColor("#39ac69"));
mPopListView.setAdapter(mMenuAdapter1);
mPopuoWindow.showAsDropDown(mSupploerListProduct, 0, 2);
supplierMenuIndex = 0;
break;
case R.id.around_supplier_list_sort:
mSupplierListTvSort.setTextColor(Color.parseColor("#39ac69"));
mPopListView.setAdapter(mMenuAdapter2);
mPopuoWindow.showAsDropDown(mSupplierListSort, 0, 2);
supplierMenuIndex = 1;
break;
case R.id.around_supplier_list_activity:
mSupplierListTvActivity.setTextColor(Color.parseColor("#39ac69"));
mPopListView.setAdapter(mMenuAdapter3);
mPopuoWindow.showAsDropDown(mSupplierListActivity, 0, 2);
supplierMenuIndex = 2;
break;
default:
break;
}
}
使用supplierMenuIndex来标记点击的种类,在ListView.setOnItemClickListener的时候可以分出点击哪一个里面的内容
mPopListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
switch (supplierMenuIndex){
case 0:
mSupplierListTvProduct.setText(mMenuData1.get(i).get("name"));
mPopuoWindow.dismiss();
break;
case 1:
mSupplierListTvSort.setText(mMenuData2.get(i).get("name"));
mPopuoWindow.dismiss();
break;
case 2:
mSupplierListTvActivity.setText(mMenuData3.get(i).get("name"));
mPopuoWindow.dismiss();
break;
default:
break;
}
}
});
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1px"
android:orientation="horizontal">
<View
android:id="@+id/pop_location_view_product"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<View
android:id="@+id/pop_location_view_sort"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<View
android:id="@+id/pop_location_view_activity"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
</LinearLayout>
这样可以使3个popupwindow分别在全部,综合排序,优惠活动的正下方
mPopuoWindow.showAsDropDown(popLocationViewProduct);
mPopuoWindow.showAsDropDown(popLocationViewSort);
mPopuoWindow.showAsDropDown(popLocationViewActivity);
代码下载地址:https://download.csdn.net/download/jingerlovexiaojie/10194684