在较小的设备和大型设备上使用对话框关闭Android DialogFragment的全屏幕处理

时间:2017-08-04 16:20:18

标签: android layout android-recyclerview dismiss dialogfragment

我已经实现了自定义DialogFragment以从用户那里获得一些输入。

 if(mIsLargeLayout) {
            // The device is using a large layout, so show the fragment as a dialog
            userDeviceDialogFragment.show(fragmentManager, USER_DEVICE_DIALOG_FRAGMENT_TAG);
        } else {
            // The device is smaller, so show the fragment fullscreen
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            // Transition animation
            fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            // To make it fullscreen, use the 'content' root view as the container
            // for the fragment, which is always the root view for the activity
            fragmentTransaction.add(android.R.id.content, userDeviceDialogFragment)
                               .addToBackStack(null)
                               .commit();
        }

我现在有这样的问题。

  1. 当有大屏幕时,DialogFragment使用Dialog,我可以覆盖 onDismiss 方法,并通知Dialog正在关闭。

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    
        Log.d(TAG, "onDismiss method called.");
    
        if(mOnDialogEventListener != null) {
            mOnDialogEventListener.onDialogClose();
        }
    }
    

    然后我可以通知Activity关于Dialog(DialogFragment)关闭。

  2. 如果屏幕较小,我使用工具栏以全屏模式显示 DialogFragment Fragmen 。此 DialogFragment 可以通过 X按钮,添加按钮和后退按钮来解除。但是不再调用 onDismiss()重写方法。如何关闭DialogFragment,无论是工具栏上的后退按钮还是操作按钮,如何通知我的父活动。 我考虑直接调用Callback(Delegate方法)mOnDialogEventListener.onDialogClose();

  3. 但我该如何处理后退按钮?好的我可以在Activity里覆盖它,但现在如何在显示DialogFragment时将其限制为情况。

    原因: 我想在我的Activity中需要这个Dialog关闭通知来重绘RecyclerView列表。似乎当DialogFragment位于前台时,然后旋转设备RecyclerView重绘不正确(图标和文本混乱)。因此,我认为在显示DialogFragment时我应该停止重绘RecyclerView,然后在关闭DialogFragment后重绘它。

    enter image description here] enter image description here

    我不知道为什么这个布局搞乱了,但是在显示DialogFragment(全屏)+旋转设备后会发生这种情况。我认为CursorLoader是刷新的,并且在后台重绘无法计算出正确的大小,即RecyclerView中每个项目的水平LinearLayout的2/10和8/10权重?

    总结:

    1. 如何获得有关模型和全屏幕DialogFragment的通知,以启用RecyclerView刷新?

    2. 在显示DialogFragment时,还有其他解决方案可以解决方向更改时正确重绘RecyclerView的问题吗?

0 个答案:

没有答案