2014-11-27 3 views
1

У меня есть FragmentTabHost и у меня есть некоторые проблемы с ним при переключении фрагментов. После попытки переключения фрагмента, а затем я нажимаю на другую вкладку, содержимое вкладки перекрывается. Затем я пытаюсь переопределить метод onTabChanged (не знаю, может ли это быть решением, хотя ....), однако я обнаружил, что onTabChanged не запускается!onTabChanged не работает и перекрывает фрагменты

--Update--

onTabChanged теперь работоспособный, но проблема перекрытия по-прежнему здесь.

вот мой код:

MainActivity

public class MainActivity extends FragmentActivity implements OnTabChangeListener{ 
    private static final String TAB = "MainActivity"; 

    private FragmentTabHost tabMenu; 
    public static final String NEWS_TAB = "news"; 
    public static final String SHARE_TAB = "share"; 
    public static final String CAMERA_TAB = "camera"; 
    public static final String STATUS_TAB = "status"; 
    public static final String OTHER_TAB = "other"; 


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

     tabMenu = (FragmentTabHost)findViewById(R.id.main_tabhost); 
     tabMenu.setup(this, getSupportFragmentManager(), android.R.id.tabcontent); 

     String[] tabTitle = getResources().getStringArray(R.array.tab_title); 
     for (int i=0; i<tabTitle.length; i++){ 

      String tagId = ""; 
      int tabIconDrawable = 0; 
      Class tabClass = BlankTab.class; 

      switch(tabTitle[i]){ 
      case "最新消息": 
       tabIconDrawable = R.drawable.ic_action_view_as_list;    
       tagId = this.NEWS_TAB; 
       tabClass = News.class; 
       break; 
      case "貨件分享": 
       tabIconDrawable = R.drawable.ic_action_important; 
       tagId = this.SHARE_TAB; 
       tabClass = GoodsShare.class; 
       break; 
      case "": 
       tabIconDrawable = R.drawable.ic_action_camera; 
       tagId = this.CAMERA_TAB; 
       break; 
      case "物流狀態": 
       tabIconDrawable = R.drawable.ic_action_airplane_mode_on; 
       tagId = this.STATUS_TAB; 
       tabClass = LogisticStatus.class; 
       break; 
      case "其他": 
       tabIconDrawable = R.drawable.ic_action_overflow; 
       tagId = this.OTHER_TAB; 
       tabClass = Others.class; 
       break; 
      } 

      View tabView = getLayoutInflater().inflate(R.layout.tab_layout, null, false); 
      ImageView tabIcon = (ImageView)tabView.findViewById(R.id.tab_icon); 
      TextView tabText = (TextView)tabView.findViewById(R.id.tab_title); 

      tabIcon.setImageDrawable(getResources().getDrawable(tabIconDrawable)); 
      tabText.setText(tabTitle[i]); 

      TabSpec spec = tabMenu.newTabSpec(tagId).setIndicator(tabView); 

      tabMenu.addTab(spec, tabClass, null); 
      tabMenu.getTabWidget().getChildAt(i).setBackground(getResources().getDrawable(R.drawable.main_tab_selector)); 
     } 

     tabMenu.setOnTabChangedListener(this); 

    } 

    @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; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 

     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public void onTabChanged(String tabId) { 
     // TODO Auto-generated method stub 
     Log.d(TAB, tabId); 
     FragmentTransaction t = getSupportFragmentManager().beginTransaction(); 
     Fragment fragment = null; 
     if (tabId.equals(this.NEWS_TAB)){ 
      fragment = new News(); 
     } else if (tabId.equals(this.SHARE_TAB)){ 
      fragment = new GoodsShare(); 
     } else if (tabId.equals(this.CAMERA_TAB)){ 
      fragment = new Camera(); 
     } else if (tabId.equals(this.STATUS_TAB)){ 
      fragment = new LogisticStatus(); 
     } else if (tabId.equals(this.OTHER_TAB)){ 
      fragment = new Others(); 
     } 

     if (fragment != null){ 
      t.replace(android.R.id.tabcontent, fragment); 
     } else { 
      Log.e(TAB, "fragment creation error"); 
     } 
    } 


} 

< Другие --- один имеет фрагмент переключения

public class Others extends Fragment{ 
    private View view; 
    private Controller controller; 

    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     controller = (Controller)this.getActivity().getApplication(); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     view = inflater.inflate(R.layout.others, container, false); 
     setLayout(); 
     return view; 
    } 

    private void setLayout(){ 
     ListView othersMenu = (ListView)view.findViewById(R.id.others_menu); 
     String[] othersItem = getResources().getStringArray(R.array.others); 

     ArrayAdapter<String> adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, othersItem); 
     othersMenu.setAdapter(adapter); 
     othersMenu.setOnItemClickListener(new OnItemClickListener(){ 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // TODO Auto-generated method stub 
       String item = (String)parent.getItemAtPosition(position); 
       Fragment fragment = null; 
       switch(item){ 
       case "貨倉地址": 
        fragment = new WarehouseAddress(); 
        break; 
       case "托運流程": 
        fragment = new TransProcess(); 
        break; 
       case "常見問題": 
        fragment = new FAQ(); 
        break; 
       case "聯絡我們": 
        fragment = new ContactUs(); 
        break; 
       case "運費計算器": 
        fragment = new FeeCalculater(); 
        break; 
       default: 
        fragment = new BlankTab(); 
       } 

       controller.fragmentSwitch(getActivity(), fragment); 

      } 

     }); 
    } 
} 

метод fragmentSwitch

public void fragmentSwitch(Fragment fragment){ 
    FragmentManager fragmentManager = slideMenuActivity.getSupportFragmentManager(); 
    fragmentManager.beginTransaction() 
      //.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right) 
      .replace(android.R.id.tabcontent, fragment) 
      .commit(); 
    fragmentManager.executePendingTransactions(); 
} 

ответ

0

Наконец, я нашел его. Ошибка commit() пропущена в onTabChanged.

@Override 
public void onTabChanged(String tabId) { 
    // TODO Auto-generated method stub 
    Log.d(TAB, tabId); 
    FragmentTransaction t = getSupportFragmentManager().beginTransaction(); 
    Fragment fragment = null; 
    if (tabId.equals(this.NEWS_TAB)){ 
     fragment = new News(); 
    } else if (tabId.equals(this.SHARE_TAB)){ 
     fragment = new GoodsShare(); 
    } else if (tabId.equals(this.CAMERA_TAB)){ 
     fragment = new Camera(); 
    } else if (tabId.equals(this.STATUS_TAB)){ 
     fragment = new LogisticStatus(); 
    } else if (tabId.equals(this.OTHER_TAB)){ 
     fragment = new Others(); 
    } 

    if (fragment != null){ 
     t.replace(android.R.id.tabcontent, fragment).commit(); //missing commit() here 
    } else { 
     Log.e(TAB, "fragment creation error"); 
    } 
} 
Смежные вопросы