如何并排设置两个片段并在它们之间进行通信



这是我第一次使用片段,我开始创建一个新的项目,并选择"导航抽屉活动";在Android Studio中。我的第一步是在"content_main.xml"中添加第二个片段;(如下所示(,并将MainActivity.java中对原始nav_host的引用更新到左侧片段。

<?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_left"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
<fragment
android:id="@+id/nav_host_fragment_right"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.50121653" />
</androidx.constraintlayout.widget.ConstraintLayout>

下面是MainActivity.Java

package com.example.test;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import android.view.View;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;
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,
R.id.nav_tools, R.id.nav_share, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_left);
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_left);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}

当我运行应用程序时;这是家的碎片";两侧。我可以使用菜单选择另一个菜单选项,左侧更改为显示新片段,右侧仍然显示主页。我的问题是当我开始尝试在两个片段之间进行交流时。我看到了下面的例子:

https://codinginflow.com/tutorials/android/fragment-to-fragment-communication-with-interfaces

然而,在他们的主要活动中有以下代码

fragmentA = new FragmentA();
fragmentB = new FragmentB();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_a, fragmentA)
.replace(R.id.container_b, fragmentB)
.commit();

我在我当前的MainActivity中没有看到这一点,只是一些NavController项目,我认为它们以类似的方式运行。

所以我有两个问题:

Q1-当我点击菜单项时,示例项目如何替换左侧片段?

Q2-我如何与MainActivity的右侧片段进行通信?我想我应该能够在MainActivity OnCreate((中使用findFragmentById,但这不起作用。

HomeFragment homeFrag = getSupportFragmentManager().findFragmentById(R.id.nav_host_frag_right);

我想我需要修改碎片的创建方式吗?非常感谢您的帮助!

您可以使用ViewModel类,片段可以轻松地与该类通信,它将充当中介,也可以在活动中创建一个接口或实现。碎片无法直接交互。https://developer.android.com/reference/androidx/lifecycle/ViewModelhttps://developer.android.com/training/basics/fragments/communicating

在第二种情况下,您可以通过以下方式调用这些接口方法:

(getActivity)ac.methodA() ac is a reference variable of interface and methods() is 
one of the methods of interface.

最新更新