导航抽屉活动:在按钮单击时从片段移动到片段

Posted

技术标签:

【中文标题】导航抽屉活动:在按钮单击时从片段移动到片段【英文标题】:Navigation Drawer Activity: move from fragment to fragment on button click 【发布时间】:2020-12-26 14:36:17 【问题描述】:

通过单击按钮从片段移动到另一个片段,会导致以下内容重叠。当我点击按钮时,主页和幻灯片片段都会显示。

我使用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;
    

fragment_home.xml 中按钮的 XML

<Button
    android:id="@+id/button"
    android:layout_
    android:layout_
    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" />

containercontent_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_
    android:layout_
    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_
        android:layout_
        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();
    


【问题讨论】:

你在哪里定义容器 nav_host_fragment? content_main.xml. 你为什么要使用 navcontroller 还要自己替换片段? 【参考方案1】:

好的,你的问题,用 slide_show_container (FrameLayout) 替换 nav_host_fragment。 name 可以是相同的想法是用 Framelayout 替换片段。

在 Home 活动中创建添加此代码

               getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.nav_host_fragment, new HomeFragment())
                .commit();

并删除之前建议的代码。

<FrameLayout
    android:id="@+id/nav_host_fragment"
    android:layout_
    android:layout_
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

【讨论】:

使用slide_show_container 的结果也相同,因为两个片段都在重叠 更新了答案。将此块放在按钮下方的主页片段中。并使用 getChildFragmentManager() 代替 getragmentManager() 两者都在显示

以上是关于导航抽屉活动:在按钮单击时从片段移动到片段的主要内容,如果未能解决你的问题,请参考以下文章

想要使用片段从导航抽屉活动移动到另一个屏幕,以在所有屏幕上显示抽屉

如何在主片段中单击按钮时打开导航抽屉?

如何在 Android 中单击 ImageView 时从一个片段移动到另一个片段?

从片段中的按钮单击打开导航抽屉(Kotlin)

如何在单击 RecyclerView 项目时从一个片段移动到另一个片段

在导航抽屉管理的片段之间移动