如何在onTabChanged(String tabTag)获取内容片段

时间:2015-05-18 00:28:22

标签: android android-fragments fragment-tab-host

搜索并查看了类似问题的一些帖子,但没有可行的解决方案。 发布在这里,看看是否有人解决了它。

在片段中使用FragmentTabHost,并尝试获取当前标签的内容片段。

在mTabHost.setOnTabChangedListener的onTabChanged(String tabTag)中 frgmt = getChildFragmentManager()。findFragmentByTag(tabTag);返回null。

mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabTag) {

            Fragment frgmt = getChildFragmentManager().findFragmentByTag(tabTag);

        }
    });

在FragmentTabHost中,它的onTabChanged()调用doTabChanged()来实例化内容片段并使用FragmentTransaction添加,或者如果片段存在则附加到它。

我想当FragmentTabHost :: onTabChanged()调用侦听器的onTabChanged()时,片段仍未在FragmentManager中通过getChildFragmentManager()准备好。

有没有办法从FragmentTabHost获取mLastTab?它应该有当前的内容片段。

感谢您的帮助!

@Override
public void onTabChanged(String tabId) {
    if (mAttached) {
        FragmentTransaction ft = doTabChanged(tabId, null);
        if (ft != null) {
            ft.commit();
        }
    }
    if (mOnTabChangeListener != null) {
        mOnTabChangeListener.onTabChanged(tabId);
    }
}

private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
    TabInfo newTab = null;
    for (int i=0; i<mTabs.size(); i++) {
        TabInfo tab = mTabs.get(i);
        if (tab.tag.equals(tabId)) {
            newTab = tab;
        }
    }
    if (newTab == null) {
        throw new IllegalStateException("No tab known for tag " + tabId);
    }
    if (mLastTab != newTab) {
        if (ft == null) {
            ft = mFragmentManager.beginTransaction();
        }
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mContext,
                        newTab.clss.getName(), newTab.args);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
    }
    return ft;
}

2 个答案:

答案 0 :(得分:1)

你可以看到问题是在OntabChanged()中的ft.commit然后我们调用

if (mOnTabChangeListener != null) {
    mOnTabChangeListener.onTabChanged(tabId);
}

当我们尝试在OnTabChnaged回调中获取Tabfragement实例时,仍未创建制表符片段,因此会抛出空指针异常。

我的解决方案: we should use the way two fragments communicate with each other using activity.

这是活动

public class MainActivity extends ActionBarActivity implements OnHeadlineSelectedListener{

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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.
    int id = item.getItemId();

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

    return super.onOptionsItemSelected(item);
}

@Override
public void OnTabCompletelyChanged(String tag) {
    ((MainActivityFragment)getSupportFragmentManager().findFragmentById(R.id.fragment)).OnTabChanged(tag);

}

}

MainFragment持有标签

public class MainActivityFragment extends Fragment {
private FragmentTabHost mTabHost;
private FragmentTab tab;
public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_main, container, false);

    mTabHost = (FragmentTabHost) v.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
            FragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
            FragmentTab.class, null);

    return v;
}


public void OnTabChanged(String tag) {
    tab = (FragmentTab)getChildFragmentManager().findFragmentByTag(tag); // Not null
}

}

FragmentTab:

public class FragmentTab extends Fragment {

private OnHeadlineSelectedListener mCallback;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (OnHeadlineSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}

@Override
public void onStart() {
    super.onStart();
    mCallback.OnTabCompletelyChanged(this.getTag());
}

public String getString(){
    return "hello";
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_layout, container, false);
    TextView tv = (TextView) v.findViewById(R.id.text);
    tv.setText(this.getTag() + " Content");
    return v;
}}

答案 1 :(得分:-1)

你应该尝试这样的事情: - FragmentManager mFragmentManager = getActivity().getSupportFragmentManager();

我希望它可以帮到你