【发布时间】:2013-10-30 08:28:22
【问题描述】:
我在这里遇到了与这个问题几乎完全相同的问题:Android, How to restart/refresh a fragment from FragmentActivty?
我正在尝试从父 FragmentActivity 调用 ListFragment 的方法。
但是,我使用的是 Eclipse 生成的模板 Swipe + Fixed Tabs。我尝试调用getSupportFragmentManager().findFragmentById(R.id.myfragment),但返回值始终为空。我猜这可能是个问题,因为我没有在我的应用程序的任何地方定义myfragment。但我不确定在哪里定义它,因为所有片段都是动态创建的。
对于不熟悉 Eclipse 中 Android SDK 生成的 Swipe + Fixed Tabs 模板的人,片段是通过覆盖 FragmentPagerAdapter getItem 函数创建的,返回我的片段的新实例。
任何帮助将不胜感激。
相关代码: 我如何设置我的适配器:
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.activity_comment);
mViewPager.setAdapter(mSectionsPagerAdapter);
重写适配器getItem函数:
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
switch(position) {
case 0:
return CommentsFragment.newInstance();
case 1:
default:
return LikesFragment.newInstance();
}
}
newInstance() 函数只是返回它们自己的一个实例,因为我的片段的类是静态的。
【问题讨论】:
-
你应该发布相关的sn-p代码。同样,如果我没有误解您的问题,您可以尝试为片段分配标签并尝试使用 findFragemtByTag 检索它
-
用相关代码更新问题。
标签: android eclipse android-fragments android-fragmentactivity