android:切换片段重叠的问题

时间:2017-03-20 04:06:25

标签: android android-layout android-fragments

我有一个FriendActivity,onCreate我创建了FriendFragment并显示。 当switchFrag被调用时,我切换到AddFriendFragment。我所拥有的问题是我切换到AddFriendFragment,这两个片段都是重叠的。我只想显示AddFriendFragment,我该如何隐藏FriendFragment?

 public class FriendActivity extends AppCompatActivity {
    private static final String TAG = FriendFragment.class.getSimpleName();

    private FriendContract.Presenter mPresenter;
    public static final String EXTRA_USER_ID = "USER_ID";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_friend);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    String userId = getIntent().getStringExtra(EXTRA_USER_ID);
    FriendFragment friendFragment = (FriendFragment) getSupportFragmentManager().findFragmentById(R.id.friendListFrame);
    if (friendFragment == null) {
        friendFragment = FriendFragment.newInstance(userId);
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.friendListFrame, friendFragment);
        transaction.commit();
    }
    mPresenter = new FriendPresenter(userId, friendFragment, this);

}
public void switchFrag(){
    Log.i(TAG,"switch to add friend");
    String userId = getIntent().getStringExtra(EXTRA_USER_ID);
    AddFriendFragment addFriendFragment = AddFriendFragment.newInstance(userId);
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.friendListFrame, addFriendFragment);
    transaction.commit();
}

}

我试图为FrameLayout设置android:background =“@ android:color / white”,但它没有用。 任何帮助将不胜感激

0 个答案:

没有答案