我试图从父FragmentActivity调用ListFragment的方法.
但是,我正在使用eclipse生成的模板Swipe Fixed Tabs.我尝试调用getSupportFragmentManager().findFragmentByID(R.ID.myfragment),但返回值始终为null.我猜这可能是一个问题,因为我没有在我的应用程序中的任何地方定义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()函数只返回自己的实例,因为我的片段的类是静态的.
解决方法 您可以通过标签找到您的片段,但当然您需要在添加片段时为其添加此标记.首先使用标记添加片段:
fragmentManager = getFragmentManager(); fragmentTransaction = fragmentManager.beginTransaction(); SomeFragment fragment = new ManagelinksFragment(); fragmentTransaction.add(R.ID.fragment_container1,fragment,"soMetag"); fragmentTransaction.commit();
然后在活动的网站上:
SomeFragment mSomeFragment = (SomeFragment) getFragmentManager().findFragmentByTag("soMetag");// Now mSomeFragment.callsomething();总结
以上是内存溢出为你收集整理的android – 从其父活动中刷新片段全部内容,希望文章能够帮你解决android – 从其父活动中刷新片段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)