【原创】给实现dialog LifeCycleOwner fra

2021-12-04  本文已影响0人  吉凶以情迁

activity和fragment都有了,dialog不加一个怎么行呢?
什么是 LifeCycleOwner 建议自己百度,它可以用在livedata,AutoDispose 来实现自动取消rxjava任务等等,用途多多。
但是传参数却需要LifeCycleOwner ,dialog是没有的,于是我徒手写了一个。

用法

            Dialog dialog=new Dialog(this);
            DialogLifeCycleOwner dialogLifeCycle=new DialogLifeCycleOwner(dialog);
            //由于接管了onDismiss,因此需要监听则需要通过此方法来实现监听。
            dialogLifeCycle.setProxyOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    //dialog close
                }
            });
            LifecycleOwner owner=dialogLifeCycle;
            //AutoDispose的用法
            observe.to(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(owner))).subscribe(observer);
            

源码


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.LifecycleRegistry;

import java.lang.ref.SoftReference;

/**
 * Author:Lozn
 * Email:qssq521@gmail.com
 * 2021/12/4
 * 10:33
 */
public class DialogLifeCycleOwner implements LifecycleOwner {

    private  static class MyView extends View {

        public void setLifecycleRegistrySoftReference(SoftReference<LifecycleRegistry> lifecycleRegistrySoftReference) {
            this.lifecycleRegistrySoftReference = lifecycleRegistrySoftReference;
        }

        SoftReference<LifecycleRegistry> lifecycleRegistrySoftReference=null;
        public MyView(Context context) {
            super(context);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return false;
        }

        public MyView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
        }

        public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @Override
        public void onVisibilityAggregated(boolean isVisible) {
            super.onVisibilityAggregated(isVisible);
        }

        @Override
        protected void onWindowVisibilityChanged(int visibility) {
            super.onWindowVisibilityChanged(visibility);
            if(lifecycleRegistrySoftReference.get()!=null){
                lifecycleRegistrySoftReference.get().setCurrentState(visibility==VISIBLE?Lifecycle.State.STARTED:Lifecycle.State.INITIALIZED);
                //没有pause,那么可以移动到oncreate,或者onPause之前
            }
        }

        @Override
        public void onWindowFocusChanged(boolean hasWindowFocus) {
            super.onWindowFocusChanged(hasWindowFocus);
            if(lifecycleRegistrySoftReference.get()!=null){
                lifecycleRegistrySoftReference.get().setCurrentState(hasWindowFocus?Lifecycle.State.RESUMED:Lifecycle.State.INITIALIZED);

                //没有pause,那么可以移动到oncreate,或者onPause之前
            }
        }

    }
    public DialogLifeCycleOwner(Dialog dialog) {
        lifecycleRegistry = new LifecycleRegistry(this);
        lifecycleRegistry.setCurrentState(Lifecycle.State.CREATED);
   /*     dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                lifecycleRegistry.setCurrentState(Lifecycle.State.STARTED);
                if (proxyOnShowListener != null) {
                    proxyOnShowListener.onShow(dialog);

                }
            }
        });*/
        MyView view = new MyView(dialog.getContext());
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(1, 1);
        params.gravity= Gravity.RIGHT|Gravity.CENTER;

        dialog.addContentView(view, params);


        dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                lifecycleRegistry.setCurrentState(Lifecycle.State.DESTROYED);
                if (proxyOnDismissListener != null) {
                    proxyOnDismissListener.onDismiss(dialog);
                }
            }
        });
    }

    public void setProxyOnDismissListener(DialogInterface.OnDismissListener proxyOnDismissListener) {
        this.proxyOnDismissListener = proxyOnDismissListener;
    }

   /* public void setProxyOnShowListener(DialogInterface.OnShowListener proxyOnShowListener) {
        this.proxyOnShowListener = proxyOnShowListener;
    }*/
    DialogInterface.OnDismissListener proxyOnDismissListener;
//    DialogInterface.OnShowListener proxyOnShowListener;
    private LifecycleRegistry lifecycleRegistry;

    @Override
    public Lifecycle getLifecycle() {
        return lifecycleRegistry;
    }

}

上一篇下一篇

猜你喜欢

热点阅读