3 回答
TA贡献1828条经验 获得超3个赞
每当您在活动上实例化片段时,都可以保留指向它们的链接。假设您的 ActivityA 和片段创建块如下所示:
public class ActivityA extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentTransaction fragmentTransactionA = getSupportFragmentManager().beginTransaction();
fragmentTransactionA.replace(R.id.yourFragmentAContainerID, new FragmentA(), "fragmentA");
fragmentTransactionA.commit();
FragmentTransaction fragmentTransactionB = getSupportFragmentManager().beginTransaction();
fragmentTransactionB.replace(R.id.yourFragmentBContainerID, new FragmentB(), "fragmentB");
fragmentTransactionB.commit();
}
}
您可以为活动创建属性并为其分配新片段的值,而不是仅使用 或 ,从而有效地在 ActivityA 和片段之间创建链接。new FragmentA()new FragmentB()
public class ActivityA extends AppCompatActivity {
private FragmentA fragmentA;
private FragmentB fragmentB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FragmentTransaction fragmentTransactionA = getSupportFragmentManager().beginTransaction();
fragmentA = new FragmentA();
fragmentTransactionA.replace(R.id.yourFragmentAContainerID, fragmentA, "fragmentA");
fragmentTransactionA.commit();
FragmentTransaction fragmentTransactionB = getSupportFragmentManager().beginTransaction();
fragmentB = new FragmentB();
fragmentTransactionB.replace(R.id.yourFragmentBContainerID, fragmentB, "fragmentB");
fragmentTransactionB.commit();
}
}
也就是说,现在您可以随时在活动时从片段中获取变量。假设您想要获取片段 A 的“propertyX”和片段 B 的“属性Y”,您现在可以在更改为 ActivityB 之前调用 和 您的活动,只要您已经拥有这些方法,并且它们是公共的。这只是一个示例,因为我们没有关于您的问题的所有代码,因此我冒昧地创建了一个示例类。fragmentA.getPropertyX()fragmentB.getPropertyY()
如果您仍然需要帮助,请进来。
TA贡献1795条经验 获得超7个赞
在片段之间进行通信的推荐方法是创建一个共享的视图模型对象。
这是来自 Developer.android.com 的链接:
您应该寻找的是使用活动作为片段之间的桥梁。使用接口是常见的做法。
TA贡献1803条经验 获得超6个赞
非常感谢,这是我的主要活动的代码:
//FOR DESIGN
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private NavigationView navigationView;
//FOR FRAGMENTS
private Fragment fragmentIntro;
private Fragment fragmentPhoto;
private Fragment fragmentProfile;
private Fragment fragmentDossier;
private Fragment fragmentDate;
private Fragment fragmentZone;
private Fragment fragmentTypeIntervention;
//FOR DATAS
private static final int FRAGMENT_INTRO = 0;
private static final int FRAGMENT_PHOTO = 1;
private static final int FRAGMENT_PROFILE = 2;
private static final int FRAGMENT_DOSSIER = 3;
private static final int FRAGMENT_DATE = 4;
private static final int FRAGMENT_ZONE = 5;
private static final int FRAGMENT_TYPE_INTERVENTION = 6;
// FOR STRINGS
private static TextView mFragmentPageNewsTitle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Configure all views
this.configureToolBar();
this.configureDrawerLayout();
this.configureNavigationView();
// Show First Fragment
this.showFirstFragment();
}
@Override
public void onBackPressed() {
// Handle back click to close menu
if (this.drawerLayout.isDrawerOpen(GravityCompat.START)) {
this.drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
// Show fragment after user clicked on a menu item
switch (id){
case R.id.activity_main_drawer_intro :
this.showFragment(FRAGMENT_INTRO);
break;
case R.id.activity_main_drawer_photo :
this.showFragment(FRAGMENT_PHOTO);
break;
case R.id.activity_main_drawer_profile:
this.showFragment(FRAGMENT_PROFILE);
break;
case R.id.activity_main_drawer_dossier:
this.showFragment(FRAGMENT_DOSSIER);
break;
case R.id.activity_main_drawer_date:
this.showFragment(FRAGMENT_DATE);
break;
case R.id.activity_main_drawer_zone:
this.showFragment(FRAGMENT_ZONE);
break;
case R.id.activity_main_drawer_typeintervention:
this.showFragment(FRAGMENT_TYPE_INTERVENTION);
break;
default:
break;
}
this.drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
// ---------------------
// CONFIGURATION
// ---------------------
// Configure Toolbar
private void configureToolBar(){
this.toolbar = (Toolbar) findViewById(R.id.activity_main_toolbar);
setSupportActionBar(toolbar);
}
// Configure Drawer Layout
private void configureDrawerLayout(){
this.drawerLayout = (DrawerLayout) findViewById(R.id.activity_main_drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
// Configure NavigationView
private void configureNavigationView(){
this.navigationView = (NavigationView) findViewById(R.id.activity_main_nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
// ---------------------
// FRAGMENTS
// ---------------------
// Show first fragment when activity is created
private void showFirstFragment(){
Fragment visibleFragment = getSupportFragmentManager().findFragmentById(R.id.activity_main_frame_layout);
if (visibleFragment == null){
// Show Photo Fragment
this.showFragment(FRAGMENT_INTRO);
// Mark as selected the menu item corresponding to PhotoFragment
this.navigationView.getMenu().getItem(0).setChecked(true);
}
}
// Show fragment according an Identifier
private void showFragment(int fragmentIdentifier){
switch (fragmentIdentifier){
case FRAGMENT_INTRO :
this.showIntroFragment();
break;
case FRAGMENT_PHOTO :
this.showPhotoFragment();
break;
case FRAGMENT_PROFILE:
this.showProfileFragment();
break;
case FRAGMENT_DOSSIER:
this.showClientsFragment();
break;
case FRAGMENT_DATE:
this.showDateFragment();
break;
case FRAGMENT_ZONE:
this.showZoneFragment();
break;
case FRAGMENT_TYPE_INTERVENTION:
this.showTypeInterventionFragment();
break;
default:
break;
}
}
// ---
// Create each fragment page and show it
private void showIntroFragment() {
if (this.fragmentIntro == null) this.fragmentIntro = IntroFragment.newInstance();
this.startTransactionFragment(this.fragmentIntro);
}
private void showPhotoFragment(){
if (this.fragmentPhoto == null) this.fragmentPhoto = PhotoFragment.newInstance();
this.startTransactionFragment(this.fragmentPhoto);
}
private void showClientsFragment(){
if (this.fragmentDossier == null) this.fragmentDossier = DossierFragment.newInstance();
this.startTransactionFragment(this.fragmentDossier);
}
private void showProfileFragment(){
if (this.fragmentProfile == null) this.fragmentProfile = ProfileFragment.newInstance();
this.startTransactionFragment(this.fragmentProfile);
}
private void showDateFragment(){
if (this.fragmentDate == null) this.fragmentDate = DateFragment.newInstance();
this.startTransactionFragment(this.fragmentDate);
}
private void showZoneFragment(){
if (this.fragmentZone == null) this.fragmentZone = ZoneFragment.newInstance();
this.startTransactionFragment(this.fragmentZone);
}
private void showTypeInterventionFragment(){
if (this.fragmentTypeIntervention == null) this.fragmentTypeIntervention = TypeInterventionFragment.newInstance();
this.startTransactionFragment(this.fragmentTypeIntervention);
}
// ---
// Generic method that will replace and show a fragment inside the MainActivity Frame Layout
private void startTransactionFragment(Fragment fragment){
if (!fragment.isVisible()){
getSupportFragmentManager().beginTransaction()
.replace(R.id.activity_main_frame_layout, fragment).commit();
}
}
}
添加回答
举报