等着批概率论的作业,解决了之前一直困扰我的底部导航栏一直无法与fragment关联起来的问题
现在做一个详细总结(傻瓜教程):
Fragment是嵌在Activity里的,有自己的生命周期,要想实现底部导航栏与上方fragment相关联,关键是要下方按钮的id与fragment的id一致,这样才能实现跳转。
首先将activity的xml文件分成两部分(只是起到了一个划分区域的作用),一部分给按钮,一部分给fragment,然后再在这两部分中分别写详细的布局。
下面是activity的java部分代码(创建activity自动生成):
package com.example.kaoyanpai; import android.os.Bundle; import com.google.android.material.bottomnavigation.BottomNavigationView; import androidx.appcompat.app.AppCompatActivity; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView navView = findViewById(R.id.nav_view); // Passing each menu ID as a set of Ids because each // menu should be considered as top level destinations. AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder( R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications,R.id.shequ,R.id.wode) .build(); NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment); NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); NavigationUI.setupWithNavController(navView, navController); } }
AppBarConfiguration:NavigationUI使用AppBarConfiguration 对象来管理应用程序显示区域左上角的“导航”按钮的行为。构造函数中传递的参数是所有按钮的id
NavController是导航控制器组件的基类,如Nav和Tab。 您可以使用导航控制器导航到应用程序中的页面。将当前activity与其布局文件中的关于fragment那部分关联起来
接下来是fragment部分代码
package com.example.kaoyanpai.ui.notifications;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.kaoyanpai.R;
public class NotificationsFragment extends Fragment {
private NotificationsViewModel notificationsViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
notificationsViewModel =
ViewModelProviders.of(this).get(NotificationsViewModel.class);
View root = inflater.inflate(R.layout.fragment_notifications, container, false);
final TextView textView = root.findViewById(R.id.text_notifications);
notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
下面是
NotificationsViewModel部分代码
package com.example.kaoyanpai.ui.notifications; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.ViewModel; public class NotificationsViewModel extends ViewModel { private MutableLiveData<String> mText; public NotificationsViewModel() { mText = new MutableLiveData<>(); mText.setValue("This is notifications fragment"); } public LiveData<String> getText() { return mText; } }