从其父活动刷新片段 [英] refresh a fragment from its parent activity

查看:163
本文介绍了从其父活动刷新片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎完全一样的问题,因为在这里这个问题:<一href="http://stackoverflow.com/questions/11578000/android-how-to-restart-refresh-a-fragment-from-fragmentactivty">Android,如何重新启动/从FragmentActivty?

I have almost the exact same problem as this question here: Android, How to restart/refresh a fragment from FragmentActivty?

我想从父调用 ListFragment 的方法 FragmentActivity

不过,我使用的模板,刷卡+ Eclipse生成固定标签。我打过电话 getSupportFragmentManager()。findFragmentById(R.id.myfragment),但返回的值总是空。我猜这可能是一个问题,因为我没有定义 myfragment 的任何地方我的应用程序。但我不能确定在哪里把它定义为所有碎片,动态创建的。

However, I'm using the template Swipe + Fixed Tabs generated by eclipse. I tried calling getSupportFragmentManager().findFragmentById(R.id.myfragment) but the return value is always null. I'm guessing this might be a problem because I did not define myfragment anywhere in my app. But I am unsure where to define it as all fragments are created dynamically.

对于那些谁不熟悉在Eclipse中,创建了片段的Andr​​oid SDK中产生的刷卡+固定标签模板,通过覆盖 FragmentPagerAdapter 的getItem 函数,返回我的片段,一个新的实例。

For those who are not familiar with the Swipe + Fixed Tabs template generated by the Android SDK in Eclipse, the fragments are created by overriding the FragmentPagerAdapter getItem function, returning a new instance of my fragment.

任何帮助将是AP preciated。

Any help would be appreciated.

相关code: 如何设置我的连接器:

Relevant code: How I setup my adapter:

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.activity_comment);
mViewPager.setAdapter(mSectionsPagerAdapter);

覆盖适配器的getItem功能:

Overriding the adapter getItem function:

@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()函数简单地返回他们自己的一个实例中,由于类我的片段是静态的。

The newInstance() functions simply return an instance of themselves, since the classes for my fragment are static.

推荐答案

您可以通过标签找到你的片段,当然你需要给这个标签给它同时加入了片段。

You can find your fragment by the Tag, but of course you need to give this tag to it while adding the fragment.

先用一个标签添加您的片段:

First add your fragment with a tag:

        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        SomeFragment fragment = new ManageLinksFragment();
        fragmentTransaction.add(R.id.fragment_container1,fragment, "sometag");
        fragmentTransaction.commit();

,然后在活动的网站:

And then on the Activity's site:

SomeFragment mSomeFragment = (SomeFragment) getFragmentManager().findFragmentByTag("sometag");
// now mSomeFragment.callsomething();

这篇关于从其父活动刷新片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆