2013-11-02 1 views
1

그래서이 간단한 탭 탐색을 작성했습니다. "조각 1"과 "조각 2"라는 두 개의 탭이 있습니다. 둘 중 하나를 클릭하면 내용 영역에 각 텍스트 "단편 1"또는 "단편 2"가 표시되어야합니다. 내 코드는 처음부터 잘 작동합니다.오리엔테이션을 바꿀 때 액션 바 탭의 버그

그러나 장치를 가로 방향으로 변경하면 텍스트가 엉망입니다. 같은 지점에 1과 2의 조합으로 Fragment를 표시합니다. 나는 두 텍스트가 동시에 표시되고 있다고 생각합니다. 텍스트는이 버그가 발생한 후 탭을 변경하려고하는 방식으로 유지됩니다. 이것은 가로에서 세로로 변경 될 때도 발생합니다. 이 원인은 무엇입니까?

MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ActionBar ab = getActionBar(); 
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    Tab tab = ab.newTab().setText(R.string.frag1).setTabListener(new MyTabListener(this, Fragment1.class.getName())); 
    ab.addTab(tab); 

    tab = ab.newTab().setText(R.string.frag2).setTabListener(new MyTabListener(this, Fragment2.class.getName())); 
    ab.addTab(tab); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

private class MyTabListener implements ActionBar.TabListener 
{ 
    private Fragment mFragment; 
    private final Activity mActivity; 
    private final String mFragName; 

    public MyTabListener(Activity activity, String fragName) { 
     mActivity = activity; 
     mFragName = fragName; 
    } 

    @Override 
    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     mFragment = Fragment.instantiate(mActivity, mFragName); 
     ft.add(android.R.id.content, mFragment); 

    } 

    @Override 
    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     ft.remove(mFragment); 
     mFragment = null; 

    } 

} 

Fragment1.java

public class Fragment1 extends Fragment 
{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) 
    { 
     return inflater.inflate(R.layout.frag1, container, false); 
    } 
} 

Fragment2.java

당신은 대신 onTabSelected에 추가() 메소드의) (대체 사용할 필요가
public class Fragment2 extends Fragment 
{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance) 
    { 
     return inflater.inflate(R.layout.frag2, container, false); 
    } 
} 

답변

2

() 메소드를 호출합니다.

+0

달콤한! 당신의 도움을 주셔서 감사합니다! –