BaseDialog

这是一个抽象的BaseDialog2类,继承自Dialog并实现了LifecycleObserver和LifecycleOwner接口,用于更好地管理Dialog的生命周期。类中包含了对动画的支持,可以根据传入的Builder参数自定义布局、宽高、gravity等属性,并在创建和销毁时处理相应的动画效果。同时,它还关注Activity的生命周期事件,以便在适当的时机刷新数据。
摘要由CSDN通过智能技术生成

public abstract class BaseDialog2 extends Dialog implements LifecycleObserver, LifecycleOwner {

    protected Context context;
    protected BaseBuilder builder;
    private Unbinder unbinder;
    protected boolean isFirstLoad = true;
    private LifecycleRegistry mLifecycleRegistry = new LifecycleRegistry(this);
    private View contentView;
    private WeakHashMap<Integer, Animation> animMap = new WeakHashMap<>();

    public BaseDialog2(@NonNull Context context, BaseBuilder builder, @StyleRes int style) {
        super(context, style);
        this.context = context;
        this.builder = builder;
        BaseUtils.registerLifecycleEvent(context, this);
    }

    public BaseDialog2(@NonNull Context context, BaseBuilder builder) {
        this(context, builder, com.quanyou.baselibrary.R.style.DialogTheme_Base);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE);
        //提前设置Dialog的一些样式
        if (builder != null) {
            contentView = LayoutInflater.from(context)
                    .inflate(CheckValueUtils.checkInteger(builder.layoutResId,
                            getClass().getSimpleName() + context.getString(com.quanyou.baselibrary.R.string.s_no_layout_id)), null);
            unbinder = ButterKnife.bind(this, contentView);
            FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(builder.width, builder.height);
            layoutParams.gravity = builder.gravity;
            setContentView(contentView, layoutParams);
            playAnimation(true, null);
            Window dialogWindow = getWindow();
            if (dialogWindow != null) {
                WindowManager.LayoutParams attributes = dialogWindow.getAttributes();
                attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
                attributes.height = ViewGroup.LayoutParams.MATCH_PARENT;
                dialogWindow.setAttributes(attributes);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    dialogWindow.setStatusBarColor(Color.TRANSPARENT);
                }
                dialogWindow.setBackgroundDrawable(builder.getBackgroundDrawable());
            }
            setCancelable(builder.cancelable);
            setCanceledOnTouchOutside(builder.canceledOnTouchOutside);
            initView(contentView);
            initData();
        }
    }

    @Override
    public void cancel() {
        playAnimation(false, new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                cancelImpl();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    protected void playAnimation(boolean isEnter, Animation.AnimationListener listener) {
        Resources.Theme theme;
        if (builder.animationStyle == null
                || contentView == null
                || context == null
                || (theme = context.getTheme()) == null) {
            if (listener != null) {
                listener.onAnimationEnd(null);
            }
            return;
        }
        try {
            int index = isEnter ? 0 : 1;
            Animation anim = animMap.get(index);
            if (anim != null) {
                playAnim(anim, listener);
                return;
            }
            int[] systemAttrs = {android.R.attr.windowEnterAnimation, android.R.attr.windowExitAnimation};
            TypedArray array = theme.obtainStyledAttributes(builder.animationStyle, systemAttrs);
            int resourceId = array.getResourceId(index, 0);
            if (resourceId > 0) {
                Animation animation = AnimationUtils.loadAnimation(context, resourceId);
                if (animation != null) {
                    animMap.put(index, animation);
                    playAnim(animation, listener);
                    return;
                }
            }
            if (listener != null) {
                listener.onAnimationEnd(null);
            }
            array.recycle();
        } catch (Resources.NotFoundException e) {
            if (listener != null) {
                listener.onAnimationEnd(null);
            }
        }
    }

    protected void playAnim(Animation anim, Animation.AnimationListener listener) {
        if (listener != null) {
            anim.setAnimationListener(listener);
        }
        contentView.startAnimation(anim);
    }

    private void cancelImpl() {
        if (contentView != null) {
            contentView.clearAnimation();
        }
        animMap.clear();
        super.cancel();
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (isShowing() && shouldCloseOnTouch(event)) {
            onBackPressed();
            return true;
        }
        return super.onTouchEvent(event);
    }

    private boolean shouldCloseOnTouch(MotionEvent event) {
        return builder.canceledOnTouchOutside
                && contentView != null
                && (event.getAction() == MotionEvent.ACTION_UP
                && ViewUtils.isOutsideTheView(contentView, event)
                || event.getAction() == MotionEvent.ACTION_OUTSIDE);
    }

    public View getContentView() {
        return contentView;
    }

    public abstract void initView(View inflate);

    public abstract void initData();

    public void refreshData() {
    }

    @NonNull
    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)//和activity生命周期绑定后可用
    public void destroy() {
        cancel();
        if (mLifecycleRegistry != null) {
            mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY);
        }
        BaseUtils.unregisterLifecycleEvent(context, this);
        if (unbinder != null && unbinder != Unbinder.EMPTY) {
            unbinder.unbind();
            unbinder = null;
        }
        if (builder != null) {
            builder.destroy();
            builder = null;
        }
        context = null;
        contentView = null;
        mLifecycleRegistry = null;
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!isFirstLoad) {
            refreshData();
        }
        isFirstLoad = false;
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START);
    }

    @Override
    protected void onStop() {
        mLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
        super.onStop();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
修改这段代码实现选择表格一行信息删除package frame; import javax.swing.; import java.awt.; import java.awt.event.ActionEvent; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; //添加页面 public class EditDialog extends BaseDialog{ DataChangelisenter dataChangelisenter; public void setDataChangelisenter(DataChangelisenter dataChangelisenter) { this.dataChangelisenter = dataChangelisenter; } @Override public void init() { setLayout(new FlowLayout()); //布局 setTitle("修改图书信息"); JPanel jp0=new JPanel(); JLabel l0 = new JLabel("id"); JTextField t0 = new JTextField(); t0.setPreferredSize(new Dimension(300,30)); jp0.add(l0); jp0.add(t0); JPanel jp1=new JPanel(); JLabel l1 = new JLabel("书名"); JTextField t1 = new JTextField(); t1.setPreferredSize(new Dimension(300,30)); jp1.add(l1); jp1.add(t1); JPanel jp2=new JPanel(); JLabel l2 = new JLabel("作者"); JTextField t2 = new JTextField(); t2.setPreferredSize(new Dimension(300,30)); jp2.add(l2); jp2.add(t2); JPanel jp3=new JPanel(); JLabel l3 = new JLabel("分类"); JTextField t3 = new JTextField(); t3.setPreferredSize(new Dimension(300,30)); jp3.add(l3); jp3.add(t3); JPanel jp4=new JPanel(); JLabel l4 = new JLabel("出版社"); JTextField t4 = new JTextField(); t4.setPreferredSize(new Dimension(300,30)); jp4.add(l4); jp4.add(t4); JPanel jp5=new JPanel(); JLabel l5 = new JLabel("数量"); JTextField t5 = new JTextField(); t5.setPreferredSize(new Dimension(300,30)); jp5.add(l5); jp5.add(t5); add(jp0); add(jp1); add(jp2); add(jp3); add(jp4); add(jp5); JButton b1 = new JButton("保存"); b1.setPreferredSize(new Dimension(150,30)); b1.addActionListener(new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { String id = t0.getText(); String sname = t1.getText(); String author = t2.getText(); String classify = t3.getText(); String press = t4.getText(); String quantity = t5.getText(); Connection con = DBUtil.getConnect(); String sql = "UPDATE tushu SET sname=?,author=?,classify=?,press=?,quantity=? WHERE id=?"; try { PreparedStatement ps = con.prepareStatement(sql); ps.setString(1,sname); ps.setString(2,author); ps.setString(3,classify); ps.setString(4,press); ps.setString(5,quantity); ps.setString(6,id); int result = ps.executeUpdate(); if (result>0){ JOptionPane.showMessageDialog(EditDialog.this,"修改成功"); // new StudentFrame(); dataChangelisenter.dataChange(); dispose(); }else { JOptionPane.showMessageDialog(EditDialog.this,"修改失败"); } } catch (SQLException throwables) { throwables.printStackTrace(); } } }); add(b1); } }
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值