android - 导航抽屉活动:在按钮单击时从片段移动到片段
问题描述
通过单击按钮从片段移动到另一个片段,会导致如下所示的覆盖内容。当我单击按钮时,将显示主页和幻灯片片段。
我用来使用按钮从主页片段切换到幻灯片片段的代码HomeFragment.java
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
Button button = root.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
requireActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.nav_host_fragment, new SlideshowFragment())
.commit();
}
});
return root;
}
用于 Button 的 XMLfragment_home.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.854"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.241" />
container
在content_main.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
重现问题的步骤:
- 使用导航抽屉活动创建新项目
- 在里面添加一个按钮
fragment_home.xml
- 为按钮创建监听器
HomeFragment.java
在单击按钮时单独显示幻灯片片段我缺少什么?
MainActivity.java
供参考
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// 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_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
解决方案
好的,你的问题,用 slide_show_container (FrameLayout) 替换 nav_host_fragment。名称可以相同的想法是用 Framelayout 替换片段。
在创建主页活动中添加此代码
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.nav_host_fragment, new HomeFragment())
.commit();
并删除之前建议的代码。
<FrameLayout
android:id="@+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
推荐阅读
- php - 如何将 bitbucket 克隆项目添加到已安装的 Magento 项目中
- r - 我应该如何在 Trimatch 中获取匹配的数据?
- angular - 将 Angular 6 部署到 Heruko 错误
- java - 是否可以在不在 jsp 中创建新会话的情况下将 EL 用于 sessionScoped bean
- java - Apache Gaucamole:与 guacd 的连接在 EC2 机器中超时
- elasticsearch - 如何在elasticsearch中查询特定的源字段
- c# - 如何在 C# 中使用键盘挂钩检测组合键
- vba - VBA代码从url获取附近位置到excel
- pandas - 在 Pandas.read_sql 中使用 Flask_sqlAlchemy Cross 数据库引擎
- java - 在spring控制器中上传带有其他参数的文件