加快PreferenceFragmentCompat设置屏幕的加载速度导航UI

问题描述

请参阅此设置屏幕:(https://developer.android.com/guide/topics/ui/settings

当更改到设置屏幕时,我注意到了一个小延迟,当更改为其他片段时(使用导航UI控制的底部导航栏时,不会发生)。我相信是由于在运行时将首选项xml文件加载到一个片段中引起的,因为更改片段后滞后消失了。同样,在切换到其他片段时也不会发生这种延迟。

这是我的主要活动

public class MainActivity extends AppCompatActivity{

    NavController navController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navController = Navigation.findNavController(this,R.id.fragment_main);

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_nav);  
        NavigationUI.setupWithNavController(bottomNavigationView,navController);

    }
}

设置片段

public class SettingsFragment extends PreferenceFragmentCompat {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState,String rootKey) {
        setPreferencesFromResource(R.xml.root_preferences,rootKey);
    }
}

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:itemBackground="@color/bottomNavColor"
        app:itemIconTint="@color/bottom_nav_menu"
        app:itemTextColor="@color/bottom_nav_menu"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/fragment_main"
        app:navGraph="@navigation/nav_view"
        app:defaultNavHost="true"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout="@layout/fragment_main_screen" />

</androidx.constraintlayout.widget.ConstraintLayout>

底部导航手册

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_fragment_main"
        android:icon="@drawable/ic_home_dark"
        android:title="Home"/>

    <item
        android:id="@+id/profileFragment"
        android:icon="@drawable/ic_profile"
        android:title="My Profile" />

    <item
        android:id="@+id/settingsFragment"
        android:icon="@drawable/ic_settings"
        android:title="Settings" />
</menu>

nav_view.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_view"
    app:startDestination="@id/navigation_fragment_main">

    <fragment
        android:id="@+id/navigation_fragment_main"
        android:name="com.example.drinkinggamesapp.Fragment_main"
        android:label="fragment_main_screen"
        tools:layout="@layout/fragment_main_screen" >
    </fragment>
    
    <fragment
        android:id="@+id/settingsFragment"
        android:name="com.example.drinkinggamesapp.SettingsFragment"
        android:label="SettingsFragment" />
    <fragment
        android:id="@+id/profileFragment"
        android:name="com.example.drinkinggamesapp.ProfileFragment"
        android:label="fragment_profile"
        tools:layout="@layout/fragment_profile" />
</navigation>

更改为“设置片段”时,如何减少延迟?我可以以某种方式预加载它并保留NavigationUI吗?任何人都有经验吗?

解决方法

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

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

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