java - OnClick 片段事务什么都不做
问题描述
所以我在片段交易方面遇到了一些麻烦。我有导航抽屉,并且我实现了在片段事务的文档中编写的代码,但是当我单击按钮时,什么也没有发生。
MainFeedActivity.class:
public class MainFeedActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainFeedBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainFeedBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMainFeed.toolbar);
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_memories, R.id.nav_snack_memories, R.id.nav_favorite_memories)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main_feed);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
Button button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create new fragment and transaction
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setReorderingAllowed(true);
// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.nav_memories, GalleryFragment.class, null);
// Commit the transaction
transaction.commit();
}
});
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main_feed);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
MainFeedActivity.class 中的按钮 onClick:
Button button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Create new fragment and transaction
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setReorderingAllowed(true);
// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.nav_memories, GalleryFragment.class, null);
// Commit the transaction
transaction.commit();
}
});
图库片段 XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:id="@+id/gallery_fragment"
android:background="@color/purple_200"
tools:context=".ui.gallery.GalleryFragment">
<TextView
android:id="@+id/text_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</FrameLayout>
GalleryFragment Java:
public class GalleryFragment extends Fragment {
private FragmentGalleryBinding binding;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
GalleryViewModel galleryViewModel = new ViewModelProvider(this).get(GalleryViewModel.class);
binding = FragmentGalleryBinding.inflate(inflater, container, false);
View root = binding.getRoot();
final TextView textView = binding.textGallery;
galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
@Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
解决方案
所以我注意到当我按下按钮并打开抽屉时,片段显示在抽屉项目旁边。所以我将 R.id.navMemories 更改为drawer_layout,现在它的显示
最终代码:
// Create new fragment and transaction
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
GalleryFragment galleryFragment = new GalleryFragment();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.setReorderingAllowed(true);
transaction.addToBackStack(null);
// Replace whatever is in the fragment_container view with this fragment
transaction.replace(R.id.drawer_layout, galleryFragment, null);
// Commit the transaction
transaction.commit();
推荐阅读
- java - 如何覆盖文件中的前一个字节?
- database - 为什么不能导入数据库(Firebase)
- aws-api-gateway - AWS API Gateway Cognito 授权模拟
- android - 房间新功能自动迁移报错:不兼容的类型无法转换为List
- data-binding - SwiftUI 动态绑定变量数组崩溃
- c - 有没有办法让这个程序更快?
- android - 以 ViewPager2 作为父级的 RecyclerView ViewHolder 中的 ViewPager 不起作用
- python - 如何在 Python Anaconda 中使用 Selenium 来按下加载更多按钮?
- python - Python中关于浏览器的多处理暂停代码
- python - 转储 json 会创建 unicode 字符