有一些与此问题相关的问题。例如,this one。但它不起作用。
让我展示一下我在代码中所做的事情。
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity" tools:ignore="MergeRootFrame" >
<fragment
android:id="@+id/my_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.zhouhao.test.MyFragment"
tools:layout="@layout/fragment_my" />
</FrameLayout>
fragment_my.xml (我的主要片段包含FragmentTabHost)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
android:orientation="horizontal">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="95"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/panel_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
fragment_tab1.xml (对于与不同标签对应的片段,它们类似,所以我只向您显示一个代码)
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.example.zhouhao.test.TabFragment">
<!-- TODO: Update blank fragment layout -->
<TextView android:layout_width="match_parent" android:layout_height="match_parent"
android:text="@string/fragment_tab1" />
</FrameLayout>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
FragmentManager fm = getSupportFragmentManager();
mMyFragment = (MyFragment) fm.findFragmentById(R.id.my_fragment);
}
}
公共类MyFragment扩展Fragment实现TabHost.OnTabChangeListener {
FragmentTabHost mFragmentTabHost;
public MyFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
if (rootView != null) {
mFragmentTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
mFragmentTabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.panel_content);
TabFragment1 tab1Fragment = new TabFragment1();
TabFragment2 tab2Fragment = new TabFragment2();
TabFragment3 tab3Fragment = new TabFragment3();
TabHost.TabSpec spec1 = mFragmentTabHost.newTabSpec("1").setIndicator("");
TabHost.TabSpec spec2 = mFragmentTabHost.newTabSpec("2").setIndicator("");
TabHost.TabSpec spec3 = mFragmentTabHost.newTabSpec("3").setIndicator("");
mFragmentTabHost.addTab(spec1,tab1Fragment.getClass(), null);
mFragmentTabHost.addTab(spec2,tab2Fragment.getClass(), null);
mFragmentTabHost.addTab(spec3,tab3Fragment.getClass(), null);
mFragmentTabHost.setOnTabChangedListener(this);
return rootView;
} else {
return super.onCreateView(inflater, container, savedInstanceState);
}
}
@Override
public void onTabChanged(String tabId) {
BaseFragment f = (BaseFragment)getActivity().getSupportFragmentManager().findFragmentByTag(tabId);
Log.d("Log",tabId);
}
}
我的问题是我的onTabChanged中的BaseFragment始终为null。有人可以帮忙吗?感谢。
答案 0 :(得分:4)
如果片段从未被实例化,则无法立即获取所选片段。
@Override
public void onTabChanged(final String tabId) {
Fragment fg = getSupportFragmentManager().findFragmentByTag(tabId);
Log.d(TAG, "onTabChanged(): " + tabId + ", fragment " + fg);
if (fg == null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Fragment fg = getSupportFragmentManager().findFragmentByTag(tabId);
Log.d(TAG, "onTabChanged() delay 50ms: " + tabId + ", fragment " + fg);
}
}, 50);
}
}
输出:
// cannot get the selected fragment immediately if the fragment has never been instantiated.
onTabChanged(): 1, fragment null
onTabChanged() delay 50ms: 1, fragment HistoryFragment{6f7a9d5 #2 id=0x7f09006e 1}
onTabChanged(): 2, fragment null
onTabChanged() delay 50ms: 2, fragment HistoryFragment{10c59e72 #3 id=0x7f09006e 2}
// can get the selected fragment immediately if the fragment already instantiated.
onTabChanged(): 1, fragment HistoryFragment{6f7a9d5 #2 id=0x7f09006e 1}
onTabChanged(): 2, fragment HistoryFragment{10c59e72 #3 id=0x7f09006e 2}
答案 1 :(得分:1)
我认为我找到了解决此问题的正确方法,以下是我的答案。
private int imgId;
private int textId;
private Class fragment;
public MainTabBean(int textId, int imgId, Class fragment) {
this.textId = textId;
this.imgId = imgId;
this.fragment = fragment;
}
public int getTextId() {
return textId;
}
public void setTextId(int textId) {
this.textId = textId;
}
public int getImgId() {
return imgId;
}
public void setImgId(int imgId) {
this.imgId = imgId;
}
public Class getFragment() {
return fragment;
}
public void setFragment(Class fragment) {
this.fragment = fragment;
}
List<MainTabBean> tabs = new ArrayList<>();
// three TabBean instances
TabBean homeTab = new MainTabBean(R.string.home, R.drawable.tab_home, HomepageFragment.class);
TabBean postcardTab = new MainTabBean(R.string.postcard, R.drawable.tab_postcard, PostcardFragment.class);
Bean notificationTab = new MainTabBean(R.string.notification, R.drawable.tab_notification, NotificationFragment.class);
// put tabs into a list
tabs.add(homeTab);
tabs.add(postcardTab);
tabs.add(notificationTab);
// Obtain FragmentTabHost
tabHost = findViewById(android.R.id.tabhost);
// Associate fragmentManager with container
tabHost.setup(this, getSupportFragmentManager(), R.id.main_content);
// Create TabSpecs with TabBean and add it on TabHost
for(MainTabBean tab : tabs) {
TabHost.TabSpec spec = tabHost.newTabSpec(String.valueOf(tab.getTextId()))// set a tag
.setIndicator(getTabIndicator(tab));// set an indicator
tabHost.addTab(spec, tab.getFragment(), null);// add a tab
}
getSupportFragmentManager()
.findFragmentByTag(String.valueOf(tabs.get(1).getTextId()));
相反,我的答案完全是这样:
tabs.get(0).getFragment().newInstance();
并广播。
我终于得到了片段。