我有一個FragmentActivity
,其中Buttons
和三個Fragment
類。如果按鈕1被點擊,我想顯示FragmentOne
並隱藏FragmentTwo
,FragmentThree
。如果點擊Button2
,我想顯示FragmentTwo
並隱藏FragmentOne
,FragmentThree
,反之亦然。我的代碼沒有工作。在Android中隱藏/顯示片段
private void fManager() {
FragmentManager manager = getSupportFragmentManager();
f1 = manager.findFragmentById(R.id.first);
f2 = manager.findFragmentById(R.id.second);
f2 = manager.findFragmentById(R.id.third);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
if (b1.isPressed()) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.hide(f2);
transaction.hide(f3);
transaction.show(f1);
transaction.commit();
}
break;
case R.id.button2:
if (b2.isPressed()) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.hide(f1);
transaction.hide(f3);
transaction.show(f2);
transaction.commit();
}
break;
case R.id.button3:
if (b3.isPressed()) {
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.hide(f1);
transaction.hide(f2);
transaction.show(f3);
transaction.commit();
}
break;
}
}
經理或交易?我試過'transaction.commit();'它沒有工作 – DroidLearner 2013-04-07 17:45:51
應該是事務。另請參閱編輯 – 2013-04-07 18:19:09
請檢查我的代碼http://pastebin.com/STkUKuSi並讓我知道在哪裏寫setVisibility(); – DroidLearner 2013-04-07 18:39:28