1 在实际开放中,有类似如下的效果
的实现思路有2种
一种是 实用popupWindow 来实现,一种是实用 fragment
对于第一种方案的代码主要如下:
HotelIconPopUpWindow popUpWindow = new HotelIconPopUpWindow(mFragment.getActivity(), mItemModelList); popUpWindow.setAnchorView(mFragment.getActivity().getWindow().getDecorView());
popUpWindow.showPopWindow();
这里我们将我门浮层的anchorView 设置为decorView
/**
* @author bkhu
*/
public class HotelIconPopUpWindow extends PopupWindow implements View.OnClickListener {
private View mDialogView;
private View mDropDownAnchorView;
private Context mContext;
private LinearLayout mIconContainer;
private TextView mCloseIconTv;
private ArrayList<IconItemModel> mItemIDList;
public HotelIconPopUpWindow(Context context, ArrayList<IconItemModel> itemIDList) {
mContext = context;
mItemIDList = itemIDList;
setContentView(createContentView());
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setBackgroundDrawable(new ColorDrawable(Color.parseColor("#B3000000")));
setOutsideTouchable(true);
setFocusable(true);
}
public void setAnchorView(View anchor) {
mDropDownAnchorView = anchor;
}
private int getMarginValue() {
int screenHeight = DeviceUtil.getScreenHeight();
return Math.round(screenHeight * 0.38f);
}
private View createContentView() {
ViewGroup contentView = new FrameLayout(mContext);
contentView.setFocusable(true);
contentView.setFocusableInTouchMode(true);
mDialogView = LayoutInflater.from(mContext).inflate(R.layout.hotel_detail_icon_info_popupwindow_layout, null);
mIconContainer = (LinearLayout) mDialogView.findViewById(R.id.icon_container);
mCloseIconTv = (TextView) mDialogView.findViewById(R.id.close);
mCloseIconTv.setOnClickListener(this);
contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
lp.gravity = Gravity.BOTTOM;
contentView.addView(mDialogView, lp);
int padding = getMarginValue();
contentView.setPadding(0, padding, 0, 0);
return contentView;
}
public void showPopWindow() {
if (mDropDownAnchorView == null || mDropDownAnchorView.getWindowToken() == null) return;
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
showAtLocation(mDropDownAnchorView, Gravity.BOTTOM, 0, 0);
mDialogView.setFocusable(true);
mDialogView.setFocusableInTouchMode(true);
loadAnimation(mDialogView, R.anim.common_hotel_popwindow_in, false);
}
@Override
public void dismiss() {
loadAnimation(mDialogView, R.anim.common_hotel_popwindow_out, true);
}
private void loadAnimation(View view, int resId, boolean isSetListener) {
Animation animation = AnimationUtils.loadAnimation(mContext, resId);
animation.setFillAfter(true);
if (isSetListener) {
animation.setAnimationListener(new EndAnimationListener());
}
view.startAnimation(animation);
}
@Override
public void onClick(View v) {
int id = v.getId();
if (id == R.id.close) {
dismiss();
}
}
private class EndAnimationListener implements Animation.AnimationListener {
@Override
public void onAnimationEnd(Animation animation) {
new Handler().post(new Runnable() {
@Override
public void run() {
if (isShowing()) {
HotelIconPopUpWindow.super.dismiss();
}
}
});
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationStart(Animation animation) {
}
}
}
这里要注意的是,首先我门要给popUpWindow 设置背景,
setBackgroundDrawable(new ColorDrawable(Color.parseColor("#B3000000")));
setOutsideTouchable(true);
setFocusable(true);
这里我们在弹起的时候会有相应的背景,注意这个背景一定要设置,如果不设置,点击背景的区域的点击消失的功能,将消失, setOutsideTouchable(true); 控制window 内容区域外的部分点击自动消失。
同时注意设置背景的宽高
setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
setWindowLayoutMode(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
第二种方案,即是实用fragment
Bundle arguments = new Bundle();
arguments.putBoolean("isOversea",cacheBean.isOversea());
HotelFloatFacilitysFragment hotelFloatFacilitysFragment = new HotelFloatFacilitysFragment();
if(cacheBean.isOversea()){ hotelFloatFacilitysFragment.setOverseaContent(cacheBean.getHotelOverseaFloatFacilityTags());
}else{ hotelFloatFacilitysFragment.setInlandContent(cacheBean.getHotelInlandFloatFacilityTags());
} hotelFloatFacilitysFragment.setTargetFragment(mFragment,0);
hotelFloatFacilitysFragment.setArguments(arguments);
FragmentManager fragmentManager = mFragment.getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.setCustomAnimations(R.anim.common_hotel_popwindow_in_3,
R.anim.common_hotel_popwindow_out_3,
R.anim.common_hotel_popwindow_in_3,
R.anim.common_hotel_popwindow_out_3);
transaction.add(android.R.id.content,hotelFloatFacilitysFragment,"HotelFloatFacilitysFragment") .addToBackStack("HotelFloatFacilitysFragment").commitAllowingStateLoss();