Android应用切换系统主题才显示Fragment内容问题求助
Android导航Fragment内容不显示,仅切换设备主题后可见
已完成NavController与BottomNavigationView集成,底部导航切换功能正常,但Fragment内容始终为空,仅切换设备明暗主题后才会显示。
涉及HomeActivity及HomeFragment、SearchFragment、ProfileFragment三个Fragment,布局预览中Fragment内容正常,已排除颜色和主题配置问题,推测是代码逻辑导致内容未加载。
HomeActivity.java
public class Home extends AppCompatActivity { TextView tvSong, tvArtist; ImageView ivCover, ivPlayButton; ActivityHomeBinding binding; MaterialCardView cardView; BottomNavigationView bottomNavigationView; NavController navController; private static final int ANIMATION_DURATION = 150; // Duration in milliseconds @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); binding = ActivityHomeBinding.inflate(getLayoutInflater()); setContentView(R.layout.activity_home); ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.homePage), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); tvSong = findViewById(R.id.tvSong); tvArtist = findViewById(R.id.tvArtist); ivPlayButton = findViewById(R.id.ivPlay); cardView = findViewById(R.id.mcvCurrentSong); bottomNavigationView = findViewById(R.id.bnBotNav); final MutableLiveData<Boolean>[] musicPlaying = new MutableLiveData[]{new MutableLiveData<Boolean>()}; musicPlaying[0].setValue(true); musicPlaying[0].observe(this, aBoolean -> { if (Boolean.TRUE.equals(aBoolean)) { cardView.setVisibility(View.VISIBLE); cardView.setAlpha(0f); cardView.animate() .alpha(1f) .setDuration(ANIMATION_DURATION) .setListener(null); ivPlayButton.setImageResource(R.drawable.ic_pause); } else { cardView.animate() .alpha(0f) .setDuration(ANIMATION_DURATION) .setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { cardView.setVisibility(View.GONE); } }); ivPlayButton.setImageResource(R.drawable.ic_play); } }); ivPlayButton.setOnClickListener(v -> { if (Boolean.TRUE.equals(musicPlaying[0].getValue())) { ivPlayButton.setImageResource(R.drawable.ic_pause); musicPlaying[0].setValue(false); } else { ivPlayButton.setImageResource(R.drawable.ic_play); musicPlaying[0].setValue(true); } }); final NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.navHostFragment); assert navHostFragment != null; navController = navHostFragment.getNavController(); NavigationUI.setupWithNavController(bottomNavigationView, navController); } }
ActivityHome.xml
<?xml version="1.0" encoding="utf-8"?> <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:id="@+id/homePage" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Home"> <androidx.fragment.app.FragmentContainerView android:id="@+id/navHostFragment" android:name="androidx.navigation.fragment.NavHostFragment" android:layout_width="0dp" android:layout_height="0dp" app:defaultNavHost="true" app:layout_constraintBottom_toTopOf="@+id/bnBotNav" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:navGraph="@navigation/navgraph" /> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bnBotNav" android:layout_width="0dp" android:layout_height="wrap_content" style="@style/SCREEN" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:menu="@menu/bottom_navigation" /> </androidx.constraintlayout.widget.ConstraintLayout>
NAV GRAPH.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/navgraph" app:startDestination="@id/homeFragment"> <fragment android:id="@+id/homeFragment" android:name="com.example.synthshine_music.HomeFragment" android:label="fragment_home" tools:layout="@layout/fragment_home" > <action android:id="@+id/action_homeFragment_to_searchFragment" app:popUpToInclusive="true" app:destination="@id/searchFragment" /> <action android:id="@+id/action_homeFragment_to_profileFragment" app:popUpToInclusive="true" app:destination="@id/profileFragment" /> </fragment> <fragment android:id="@+id/searchFragment" android:name="com.example.synthshine_music.SearchFragment" android:label="fragment_search" tools:layout="@layout/fragment_search" > <action android:id="@+id/action_searchFragment_to_profileFragment" app:popUpToInclusive="true" app:destination="@id/profileFragment" /> <action android:id="@+id/action_searchFragment_to_homeFragment2" app:popUpToInclusive="true" app:destination="@id/homeFragment" /> </fragment> <fragment android:id="@+id/profileFragment" android:name="com.example.synthshine_music.ProfileFragment" android:label="fragment_profile" tools:layout="@layout/fragment_profile" > <action android:id="@+id/action_profileFragment_to_homeFragment" app:popUpToInclusive="true" app:destination="@id/homeFragment" /> <action android:id="@+id/action_profileFragment_to_searchFragment" app:popUpToInclusive="true" app:destination="@id/searchFragment" /> </fragment> </navigation>
HomeFragment.java
package com.example.synthshine_music; import android.os.Bundle; import androidx.fragment.app.Fragment; import androidx.navigation.NavController; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class HomeFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false); } }
内容的提问来源于stack exchange,提问作者Matthias Sunbreeze
相关产品推荐
相关产品推荐

