DialogFragment 在 android 中的 Fragment 延迟 2 秒后显示

问题描述

这是我的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:orientation="vertical"
            android:padding="15dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="2dp">

                <ImageButton
                    android:id="@+id/btnClose"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentEnd="true"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="-10dp"
                    android:layout_marginEnd="-10dp"
                    android:adjustViewBounds="true"
                    android:background="@color/colorTransparent"
                    android:padding="5dp"
                    android:src="@android:drawable/btn_dialog" />

            </RelativeLayout>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/consLay"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <FrameLayout
                    android:id="@+id/mapContainer"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    app:layout_constraintDimensionRatio="1:0.75"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>

        </LinearLayout>

这是我的 DialogFragment:

public class DialogFragment_Map extends DialogFragment implements OnMapReadyCallback {

    private String mCityName;
    private GoogleMap mMap;
    private Marker mMarker;
    private LatLng mLatLng;
    private FragmentDialogMapBinding fragmentDialogMapBinding;

    public DialogFragment_Map newInstance(String cityName,LatLng latLng) {
        DialogFragment_Map dialog = new DialogFragment_Map();
        dialog.mCityName = cityName;
        dialog.mLatLng = latLng;
        dialog.setStyle(DialogFragment.STYLE_NO_FRAME,R.style.TransparentDialog);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
        fragmentDialogMapBinding = FragmentDialogMapBinding.inflate(getLayoutInflater());
        fragmentDialogMapBinding.setProgressstatus(true);
        fragmentDialogMapBinding.setCityName(mCityName);
        fragmentDialogMapBinding.btnClose.setonClickListener(v -> dismiss());
        FragmentManager fm = getChildFragmentManager();
        SupportMapFragment smf = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.mapContainer,smf).commit();
        smf.getMapAsync(this);
        fragmentDialogMapBinding.executePendingBindings();
        return fragmentDialogMapBinding.getRoot();
    }

    @Override
    public void onMapReady(@NonNull GoogleMap googleMap) {
        mMap = googleMap;
        mMap.getUiSettings().setZoomControlsEnabled(true);
        mMap.getUiSettings().setRotateGesturesEnabled(true);
        mMap.setonMapLoadedCallback(() -> {
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLng,11));
            MarkerOptions markerOptions = new MarkerOptions();
            markerOptions.position(mLatLng);
            markerOptions.title(mCityName);
            markerOptions.icon(getContext(),R.mipmap.ic_marker);
            mMarker = mMap.addMarker(markerOptions);
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Fragment fragment = (getParentFragmentManager().findFragmentById(R.id.mapContainer));
        FragmentTransaction ft = requireActivity().getSupportFragmentManager().beginTransaction();
        if (fragment != null) {
            ft.remove(fragment);
            ft.commit();
        }
    }
}

我正在从 Fragment 调用上面的 DialogFragment:

new DialogFragment_Map().newInstance(city.getTitle(),city.getLatLon()).
                show(requireActivity().getSupportFragmentManager(),null);

当上面的行从 Fragment 执行时,DialogFragment 会在几秒钟后显示(晚于 Activity 打开的速度)。是否有任何原因导致此延迟。可以做什么来像打开新活动一样立即打开 DialogFragment。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)