gpt4 book ai didi

android - 屏幕上的两个动态 fragment (背面)

转载 作者:行者123 更新时间:2023-11-30 02:03:54 27 4
gpt4 key购买 nike

我有一个带有 Fragment 的应用程序。它从屏幕上的两个动态 fragment 开始。在 fragment B 上,我有一个列表。当我点击该项目时,我将这两个 fragment 更改为 C 和 D。

_____    _____                      _____               _____
| A | | C | | A | | A |
_____ => _____ when back, I have : _____ but I would _____
| B | | D | | D | | B |
_____ _____ _____ _____

当我按下按钮返回时,我只有最上面的 fragment 发生了变化, fragment D 保持可见,而我想看到 A/B。

这是我的课:

public class MainActivity extends Activity implements
FragmentBListener,
FragmentCListener {

private static final String TAG_FRAGMENT_A = "FragmentATag";
private static final String TAG_FRAGMENT_B = "FragmentBTag";
private static final String TAG_FRAGMENT_C = "FragmentCTag";
private static final String TAG_FRAGMENT_D = "FragmentDTag";

FragmentManager fragmentManager = getFragmentManager();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
protected void onRestart() {
super.onRestart();
}

@Override
protected void onStart() {
super.onStart();
initFragments();
}

private void initFragments() {
setFragment(TAG_FRAGMENT_A, R.id.top_framelayout);
setFragment(TAG_FRAGMENT_B, R.id.main_framelayout);
}

private Fragment setFragment( String tagFragment, int containerViewId) {
FragmentTransaction fTransaction = fragmentManager.beginTransaction();
Fragment mFragment = null;
if(tagFragment.equals(TAG_FRAGMENT_A)) mFragment = setFragmentA(fTransaction, containerViewId);
else if(tagFragment.equals(TAG_FRAGMENT_B)) mFragment = setFragmentB(fTransaction, containerViewId);
else if(tagFragment.equals(TAG_FRAGMENT_C)) mFragment = setFragmentC(fTransaction, containerViewId);
else if(tagFragment.equals(TAG_FRAGMENT_D)) mFragment = setFragmentD(fTransaction, containerViewId);
fTransaction.addToBackStack(null);
fTransaction.commit();
return mFragment;
}

private Fragment setFragmentA(FragmentTransaction fTransaction, int containerViewId) {
FragmentA fragment = (FragmentA) fragmentManager.findFragmentByTag(TAG_FRAGMENTA);
if (fragment == null) {
fragment = new FragmentA();
if(fragment != null)
fTransaction.replace(containerViewId, fragment,TAG_FRAGMENTA);
}
return fragment;
}

private Fragment setFragmentB(FragmentTransaction fTransaction, int containerViewId) {
FragmentB fragment = (FragmentB) fragmentManager.findFragmentByTag(TAG_FRAGMENT_B);
if (fragment == null) {
fragment = new FragmentB();
if(fragment != null)
fTransaction.replace(containerViewId, fragment,TAG_FRAGMENT_B);
}
return fragment;
}

private Fragment setFragmentC(FragmentTransaction fTransaction, int containerViewId) {
FragmentC fragment = (FragmentC) fragmentManager.findFragmentByTag(TAG_FRAGMENT_C);
if (fragment == null) {
fragment = new FragmentC();
if(fragment != null)
fTransaction.replace(containerViewId, fragment,TAG_FRAGMENT_C);
}
return fragment;
}

private Fragment setFragmentD(FragmentTransaction fTransaction, int containerViewId) {
FragmentD fragment = (FragmentD) fragmentManager.findFragmentByTag(TAG_FRAGMENT_D);
if (fragment == null) {
fragment = new FragmentD();
if(fragment != null)
fTransaction.replace(containerViewId, fragment,TAG_FRAGMENT_D);
}
return fragment;
}

@Override
public void onUpdateFragmentB() {
FragmentB fragmentB = (FragmentB) fragmentManager.findFragmentByTag(TAG_FRAGMENT_B);
if (fragmentB != null) {
fragmentB.setData(...);
}

}

@Override
public void onUpdateD() {
FragmentManager fragmentManager = getFragmentManager();
FragmentD fragment = (FragmentD) fragmentManager.findFragmentByTag(TAG_FRAGMENT_D);
fragment.setData();
}

@Override
public void changeFragments() {
FragmentD fragmentD = (FragmentD) setFragment(TAG_FRAGMENT_D, R.id.main_framelayout);
if(fragmentD != null)
fragmentD.setData(...);

FragmentC fragmentC = (FragmentC) setFragment(TAG_FRAGMENT_C, R.id.top_framelayout);
if(fragmentC != null)
fragmentC.setData(...);
}


@Override
public void onUpdateFragmentsAB {
FragmentManager fragmentManager = getFragmentManager();
FragmentB fragmentB =(FragmentB) fragmentManager.findFragmentByTag(TAG_FRAGMENT_B);
if(fragmentB != null)
fragmentB.setData(...);
FragmentA fragmentA = (FragmentA) fragmentManager.findFragmentByTag(TAG_FRAGMENT_A);
if(fragmentA != null)
fragmentA.setData(...);

}
}

最佳答案

您需要一次替换两个 fragment ,然后它会像以前一样返回。

 public void setFragment(Fragment topFrag, Fragment bottomFrag){
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.frag_top, topFrag);
transaction.replace(R.id.frag_bottom, bottomFrag);
transaction.addToBackStack(null);
transaction.commit();
}

关于android - 屏幕上的两个动态 fragment (背面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31051470/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com