【问题标题】:Open another fragment by clicking通过单击打开另一个片段
【发布时间】:2021-10-27 16:40:55
【问题描述】:

我已经有一个导航栏可以毫无问题地为我更改片段:

<?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/mobile_navigation"
    android:label="@string/app_name"
    app:startDestination="@+id/navigation_home">


    <fragment
        android:id="@+id/navigation_tools"
        android:name="com.example.gsm.ui.tools.toolsFragment"
        android:label="@string/title_Tools"
        tools:layout="@layout/fragment_home" />
 
</navigation>

这是我的 Activity_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"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="0dp">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="@color/background" 
        app:itemIconSize="27dp"
        app:itemIconTint="@color/red"
        app:itemTextColor="@color/red"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的导航栏很容易为我交换片段,但我想禁用导航栏,我需要一些按钮来完成导航栏曾经为我做的事情。

我是 Android/XML 新手,Internet 中片段之间的交易没有与我的文件和代码同步,如果我将内容添加到框架布局中,我的所有设计都会搞砸。

【问题讨论】:

  • 我推荐使用新的谷歌技术 NavigationUI developer.android.com/guide/navigation/navigation-ui 简单、自动并且有很多例子。像这样看起来另一个片段打开Navigation.findNavController(binding.root).navigate(FirstFragmentDirection.actionNavigateToSecondFragment())

标签: java android xml android-fragments


【解决方案1】:

在这种情况下 FragmentContainerView 和 FragmentManager ,Fragment transactions 帮助我们。

FragmentContainerView 是专为 Fragments 设计的自定义 Layout。它扩展了 FrameLayout,因此它可以可靠地处理 Fragment Transactions,并且它还具有与 Fragment 行为协调的附加功能。

在运行时,FragmentManager 可以添加、删除、替换和使用片段执行其他操作,以响应用户交互。

因此,您可以在 activity_main.xml 中使用FragmentContainerView,您可以在其中通过按钮/用户操作添加不同的片段。

所以你的 activity_main.xml 现在看起来像 -

    <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test button"></Button>

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragment_container_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/button">
    </androidx.fragment.app.FragmentContainerView>


</RelativeLayout>

现在为您的项目创建一个新的空白片段进行测试。 IDE 也会为此自动覆盖您的片段的一些方法和布局。

所以你的BlankFragment.class 看起来像 -

package com.example.myapplication;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link BlankFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlankFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public BlankFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment BlankFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static BlankFragment newInstance(String param1, String param2) {
        BlankFragment fragment = new BlankFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }
}

现在从您的活动中,您可以在 FragmentContainerView 中显示空白片段,我们在 activity_main.xml 中定义其 id fragment_container_view。

这里是你的MainActivity.class -

public class MainActivity extends AppCompatActivity {

private ActivityMainBinding binding;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.setReorderingAllowed(true);
            transaction.replace(R.id.fragment_container_view, BlankFragment.class, null);
            transaction.commit();

        }
    });


}

  }

所有这些只是举例。您应该查看文档以了解更多信息。

【讨论】:

    猜你喜欢
    • 2020-08-12
    • 2021-01-02
    • 1970-01-01
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多