我正在开发一个 android 应用程序,我在其中使用 FragmentTabHost,我为每个选项卡维护一个容器,但是当我重新单击选项卡时,我遇到了重新加载选项卡内容的问题。
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
import com.eDeftsoft.FragmentsContainer.AboutContainerFragment;
import com.eDeftsoft.FragmentsContainer.BaseContainerFragment;
import com.eDeftsoft.FragmentsContainer.CityContainerFragment;
import com.eDeftsoft.FragmentsContainer.HomeContainerFragment;
import com.eDeftsoft.FragmentsContainer.PhotosContainerFragment;
import com.eDeftsoft.Utility.CommonDialogues;
public class HomeScreen extends FragmentActivity {
private static final String TAB_1_TAG = "Home";
private static final String TAB_2_TAG = "Photos";
private static final String TAB_3_TAG = "City";
private static final String TAB_4_TAG = "About";
private FragmentTabHost mTabHost;
TabWidget tbwidget;
HomeContainerFragment homeFragment;
PhotosContainerFragment photosFragment;
CityContainerFragment cityFragment;
AboutContainerFragment aboutFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_screen);
initView();
homeFragment = new HomeContainerFragment();
photosFragment = new PhotosContainerFragment();
cityFragment = new CityContainerFragment();
aboutFragment = new AboutContainerFragment();
}
private void initView() {
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(setMyCustomIndicator(this, TAB_1_TAG, "Home"),
HomeContainerFragment.class, null);
mTabHost.addTab(setMyCustomIndicator(this, TAB_2_TAG, "Photos"),
PhotosContainerFragment.class, null);
mTabHost.addTab(setMyCustomIndicator(this, TAB_3_TAG, "City"),
CityContainerFragment.class, null);
mTabHost.addTab(setMyCustomIndicator(this, TAB_4_TAG, "About"),
AboutContainerFragment.class, null);
setTabHostColors();
mTabHost.setCurrentTabByTag("TAB_1_TAG");
mTabHost.setCurrentTab(0);
mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
mTabHost.getTabWidget().setStripEnabled(false);
tbwidget = mTabHost.getTabWidget();
/*I had Also used This But getting error when i reclick on sametabs*/
// mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
//
// @Override
// public void onTabChanged(String tabId) {
// // TODO Auto-generated method stub
// if (tabId.equals(TAB_1_TAG)) {
// pushFragments(TAB_1_TAG, homeFragment);
// } else if (tabId.equals(TAB_2_TAG)) {
// pushFragments(TAB_1_TAG, photosFragment);
//
// } else if (tabId.equals(TAB_3_TAG)) {
// pushFragments(TAB_1_TAG, cityFragment);
// } else {
// pushFragments(TAB_1_TAG, aboutFragment);
// }
//
// }
// });
}
/*
* insert the fragment into the FrameLayout
*/
// public void pushFragments(String tag, Fragment class1) {
//
// FragmentManager manager = getSupportFragmentManager();
// FragmentTransaction ft = manager.beginTransaction();
//
// ft.replace(R.id.realtabcontent, class1);
// ft.commit();
// }
public TabSpec setMyCustomIndicator(Context con, String tag,
String labeltext) {
TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, null, false);
((TextView) tabIndicator.findViewById(R.id.title)).setText(labeltext);
// ((ImageView) tabIndicator.findViewById(R.id.icon))
// .setImageResource(resid);
return spec.setIndicator(tabIndicator);
}
@Override
public void onBackPressed() {
boolean isPopFragment = false;
String currentTabTag = mTabHost.getCurrentTabTag();
FragmentManager fm = getSupportFragmentManager();
if (currentTabTag.equals(TAB_1_TAG)) {
for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
String ide = fm.getBackStackEntryAt(entry).getName();
Log.i("TAG" + TAB_1_TAG, "Found fragment: " + ide);
}
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
.findFragmentByTag(TAB_1_TAG)).popFragment();
}
else if (currentTabTag.equals(TAB_2_TAG)) {
for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
String ide = fm.getBackStackEntryAt(entry).getName();
Log.i("TAG" + TAB_2_TAG, "Found fragment: " + ide);
}
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
.findFragmentByTag(TAB_2_TAG)).popFragment();
}
else if (currentTabTag.equals(TAB_3_TAG)) {
for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
String ide = fm.getBackStackEntryAt(entry).getName();
Log.i("TAG" + TAB_3_TAG, "Found fragment: " + ide);
}
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
.findFragmentByTag(TAB_3_TAG)).popFragment();
}
else if (currentTabTag.equals(TAB_4_TAG)) {
for (int entry = 0; entry < fm.getBackStackEntryCount(); entry++) {
String ide = fm.getBackStackEntryAt(entry).getName();
Log.i("TAG" + TAB_4_TAG, "Found fragment: " + ide);
}
isPopFragment = ((BaseContainerFragment) getSupportFragmentManager()
.findFragmentByTag(TAB_4_TAG)).popFragment();
}
if (!isPopFragment) {
CommonDialogues.showAlertDialog(HomeScreen.this,
"Application Will Exit", "Do you Want to Exit");
}
}
private void setTabHostColors() {
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.TRANSPARENT);
final TextView tv = (TextView) mTabHost.getTabWidget()
.getChildAt(i).findViewById(android.R.id.title);
if (tv == null)
continue;
else
tv.setTextSize(12);
}
}
@Override
public void onDestroy() {
super.onDestroy();
mTabHost = null;
}
}
当我进入 tab1 的内部片段时,例如从 fragmentA -> Fragment B 和 From FragmentB -> fragmentC (最后我在 fragmentC 处),当我再次选择 Tab1 时,我想重新加载选项卡并且 FragmentA 应该出现。任何帮助将不胜感激。我浏览了一些教程和代码库,但找不到解决问题的方法。再次单击第一个选项卡时,如何重新加载第一个选项卡的内容。