如何从活动中更新Android片段?

gon*_*ins 8 android android-fragments

我想根据从Internet上下载的数据定期更新片段的显示.我已经创建了一个Timer和Runnable来定期检索这些数据以及片段中的一个方法来更新它,但我似乎无法弄清楚如何获取从活动到片段的引用以便更新它.

我有以下代码,主要由ADT的Android项目向导生成:

    protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    Log.d(tag, "onCreate()::Entering...");

    setContentView(R.layout.activity_main);

    // Set up the action bar.
    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(
            getSupportFragmentManager());

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

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager
            .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    actionBar.setSelectedNavigationItem(position);
                }
            });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) 
    {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }   
Run Code Online (Sandbox Code Playgroud)

以下是用于创建选项卡的代码:

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) 
    {
        // getItem is called to instantiate the fragment for the given page.

        switch (position)
        {
        case FRAG1_POS:
            return Fragment1.newInstance();

        case FRAG2_POS:
            return Fragment2.newInstance(); 

        default:
            return null;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我尝试过使用这个解决方案:

如何从活动中更改片段的textView文本

但是我没有片段ID.我可以创建标签吗?如果是这样,在这种情况下我该怎么做?其他SO帖子提到了Bundles但我在创建片段时没有发送数据; 我想让片段定期更新,因为活动中的数据可用.

Szy*_*mon 6

您不能为片段提供标记或标识,但可以在片段类上创建自定义属性以标记它们.

switch (position)
    {
    case FRAG1_POS:
        Fragment1 f = Fragment1.newInstance();
        f.fragmentType = 1;
        return f;

    case FRAG2_POS:
        Fragment1 f = Fragment1.newInstance();
        f.fragmentType = 2;
        return f;

    default:
        return null;
    }
Run Code Online (Sandbox Code Playgroud)

什么时候可以循环遍历所有片段并找到你需要的片段

List<Fragment> allFragments = getSupportFragmentManager().getFragments();
if (allFragments != null) {
    for (Fragment fragment : allFragments) {
        Fragment1 f1 = (Fragment1)fragment;
        if (f1.fragmentType == 1)
            f1.updateFragmentData();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

向片段添加一个公共方法,用于更新片段中的数据.正如您现在引用它,您可以从您的活动中调用它.

  • 不,这更容易,因为您不必以任何方式标记碎片.只需检查循环中每个片段的类型:`if(fragmentofofof YourFragment)` (3认同)