0
我想從後面的堆棧中重用片段,即隱藏並顯示它是否存在於後臺堆棧中。這裏是我的代碼:隱藏並顯示片段,片段視圖不可見
活動:`
public class MyActivity extends Activity implements View.OnClickListener{
Button b1;
Button b2;
Button b3;
Button b4;
FrameLayout frameLayout;
FrameLayout frameLayout2;
BlankFragment fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
frameLayout =(FrameLayout)findViewById(R.id.frame1);
frameLayout2 =(FrameLayout)findViewById(R.id.frame2);
getFragmentManager().addOnBackStackChangedListener(listener);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:{
manageFragment(1);
break;
}
case R.id.button2:{
manageFragment(2);
break;
}
case R.id.button3:{
manageFragment(3);
break;
}
}
}
private void manageFragment(int i) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
BlankFragment blankFragment = (BlankFragment)manager.findFragmentByTag(""+i);
if(blankFragment !=null){
Log.v("123456","found the fragment in stack");
transaction.hide(fragment);
transaction.show(blankFragment);
transaction.commitAllowingStateLoss();
//if(i!=1){
//frameLayout2.setVisibility(View.GONE);
//frameLayout.setVisibility(View.VISIBLE);
//frameLayout.bringToFront();
//}else {
//frameLayout2.setVisibility(View.VISIBLE);
//frameLayout.setVisibility(View.GONE);
//frameLayout2.bringToFront();
}
fragment =blankFragment;
return;
}
fragment = BlankFragment.newInstance("", "" + i);
// if(i!=1){
// transaction.replace(R.id.frame1,fragment,""+i);
// frameLayout2.setVisibility(View.GONE);
// frameLayout.setVisibility(View.VISIBLE);
// frameLayout.bringToFront();
// }else{
// transaction.replace(R.id.frame2,fragment,""+i);
// frameLayout2.setVisibility(View.VISIBLE);
// frameLayout.setVisibility(View.GONE);
// frameLayout2.bringToFront();
// }
transaction.replace(android.R.id.content,fragment,""+i);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.addToBackStack(""+i);
transaction.commitAllowingStateLoss();
}
FragmentManager.OnBackStackChangedListener listener = new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
int i =getFragmentManager().getBackStackEntryCount();
Log.v("123456","backstag count : "+i);
}
};
}
`
//爲註釋代碼的解釋。 由於我有兩個容器,我可以隱藏和顯示(檢索)只有2個fragments.As我的第三個碎片容器被重用,它的視圖不顯示。
所以我嘗試使用android容器,並且根本不能顯示來自雄鹿的片段。
如何從堆棧中檢索所有的片段(這裏是計數3,可能是N個片段),我想隱藏並顯示片段不彈出。
爲什麼不使用'transaction.replace'? – 2014-09-30 05:17:03
它將分離並重新附加。視圖將再次加載,我不想再次加載它 – Meher 2014-09-30 05:17:52