当前位置: 代码迷 >> 综合 >> PopWindow 仿淘宝消息浮窗弹出
  详细解决方案

PopWindow 仿淘宝消息浮窗弹出

热度:53   发布时间:2023-12-14 01:54:50.0

PopWindow 需要依赖于window 窗口,也就是需要Activity 对象,所以我创建实例PopWindow的时候必须传入Activity

还要每个 activity 去监听消息才行

   private void registerAppMessageBroadcast(){final Map<Integer,BroadcastReceiver> bcr = new HashMap<Integer, BroadcastReceiver>();mAppContext.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {@Overridepublic void onActivityResumed(final Activity activity) {LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(activity);BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {try {String icon = intent.getStringExtra(IMExtraName.MSG_ICON);String title = intent.getStringExtra(IMExtraName.MSG_TITLE);String msgContent = intent.getStringExtra(IMExtraName.MSG_CONTENT);String msgUrl = intent.getStringExtra(IMExtraName.MSG_URL);HeadsUpPopupWindow headsUpPopupWindow = new HeadsUpPopupWindow(activity);headsUpPopupWindow.showHeadsUp(activity,icon,title,msgContent,msgUrl);}catch (Throwable e){}}};bcr.put(activity.hashCode(),broadcastReceiver);lbm.registerReceiver(broadcastReceiver,new IntentFilter(ImConfig.MESSAGE_REMIND_BROADCAST));}@Overridepublic void onActivityPaused(Activity activity) {try {LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(activity);BroadcastReceiver broadcastReceiver = bcr.get(activity.hashCode());if(broadcastReceiver != null){lbm.unregisterReceiver(broadcastReceiver);bcr.remove(activity.hashCode());}else {//nothing}}catch (Throwable e){}}@Overridepublic void onActivityCreated(Activity activity, Bundle bundle) {}@Overridepublic void onActivityStarted(Activity activity) {}@Overridepublic void onActivityStopped(Activity activity) {}@Overridepublic void onActivitySaveInstanceState(Activity activity, Bundle bundle) {}@Overridepublic void onActivityDestroyed(Activity activity) {}});}




public class HeadsUpPopupWindow extends PopupWindow {private TextView mTitleTv;private TextView mContentTv;private ImageView mImageView;private RelativeLayout windowRL;private float downY;public HeadsUpPopupWindow(final Activity activity ) {LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);View contentView = inflater.inflate(R.layout.heads_up, null);setContentView(contentView);this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);this.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);// 设置点击窗口外边窗口消失this.setOutsideTouchable(true);// 设置此参数获得焦点,否则无法点击this.setFocusable(false);initView(contentView);}private void initView(View contentView ){mTitleTv = (TextView) contentView.findViewById(R.id.tv1);mContentTv=(TextView) contentView.findViewById(R.id.tv2);mImageView = (ImageView) contentView.findViewById(R.id.iv1);windowRL=(RelativeLayout) contentView.findViewById(R.id.windowRL);}public void showHeadsUp(final Activity activity,String icon, String title, String msgContent,final String msgUrl){windowRL.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View view, MotionEvent event) {switch (event.getAction()){case MotionEvent.ACTION_DOWN:downY = event.getRawY();break;case MotionEvent.ACTION_UP:float currentY = event.getRawY();if((downY - currentY) >= 10 || TextUtils.isEmpty(msgUrl) ||  !msgUrl.startsWith("http")){try {dismiss();}catch (Throwable e){}}else {KidImActivity.startActivity(activity,msgUrl);}break;}return true;}});mTitleTv.setText(title);mContentTv.setText(msgContent);DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_launcher)// 设置图片在下载期间显示的图片.showImageForEmptyUri(R.drawable.ic_launcher)// 设置图片Uri为空或是错误的时候显示的图片.showImageOnFail(R.drawable.ic_launcher)// 设置图片加载/解码过程中错误时候显示的图片.cacheInMemory(true)// 设置下载的图片是否缓存在内存中.cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中.build();ImageLoaderUtil.displayImage(icon,mImageView,options);showAtLocation(new View(activity), Gravity.NO_GRAVITY,0,0);new Handler().postDelayed(new Runnable() {@Overridepublic void run() {try {dismiss();}catch (Throwable e){}}},5000);}@Overridepublic void showAtLocation(View parent, int gravity, int x, int y) {super.showAtLocation(parent, gravity, x, y);}