2015-05-31 33 views
2

所以我有一個類擴展了一個片段,其中,我有一個FragmentTabHost,我的FragmentTabHost包含3個Tab,每個Tab都會顯示一個項目列表.. 在每個項目中,我可以更新項目屬性,如其名稱/其他屬性。 我的問題是我更新了值並按下後退按鈕後,我的項目列表仍然包含我的項目值未更新的舊列表。我的列表只有在切換到另一個選項卡並再次返回時纔會更新。 我一直在努力爲這個問題幾個小時,任何幫助將非常感激感謝Android - 如何重新加載FragmentTabHost的內容

package com.example.hansel.tapbandung_v00.page.Menu_Parent; 


import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTabHost; 
import android.support.v4.app.FragmentTransaction; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 

import com.example.hansel.tapbandung_v00.R; 
import com.example.hansel.tapbandung_v00.page.submenu_bus.bus_category; 
import com.example.hansel.tapbandung_v00.page.submenu_bus.bus_featured; 
import com.example.hansel.tapbandung_v00.page.submenu_bus.bus_newest; 

/** 
* Created by Hansel on 5/25/2015. 
*/ 
public class Business_parent extends Fragment{ 
private FragmentTabHost mTabHost; 
Context context; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    context = getActivity().getApplicationContext(); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 

    //3 tab category, dll 
    mTabHost = new FragmentTabHost(getActivity()); 
    mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.tabhost); 
    mTabHost.addTab(mTabHost.newTabSpec("featured").setIndicator("Featured"), 
      bus_featured.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("category").setIndicator("Category"), 
      bus_category.class, null); 
    mTabHost.addTab(mTabHost.newTabSpec("newest").setIndicator("Newest"), 
      bus_newest.class, null); 
    return mTabHost; 
} 

@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. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onDestroyView() { 
    super.onDestroyView(); 
    mTabHost = null; 
} 

}

+0

我有一個類似的查詢:我想重新加載項目更新的所有標籤主機。我的列表項可以根據更新從一個列表切換到另一個列表,因此每次在其中一個選項卡中更改項目時,我需要更新整個選項卡主機。 – Supercelo

回答

0

我一直在掙扎了同樣的問題,最後我想出了這樣的解決方案:

mTabHost.getTabWidget().getChildAt(0).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new Tab1Fragment()).commit(); 
      mTabHost.setCurrentTab(0); 
     } 
    }); 

    mTabHost.getTabWidget().getChildAt(1).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new Tab2Fragment()).commit(); 
      mTabHost.setCurrentTab(1); 
     } 
    }); 

    mTabHost.getTabWidget().getChildAt(2).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getSupportFragmentManager().beginTransaction().replace(android.R.id.tabcontent, new Tab3Fragment()).commit(); 
      mTabHost.setCurrentTab(2); 
     } 
    }); 

它的工作原理。我不確定這是一個非常優雅的解決方案。