如何更新从活动Android的片段? [英] How to update Android fragment from activity?
问题描述
我想根据我的数据从互联网上下载定期更新片段的显示。我创建了一个定时器和可运行定期检索该数据以及该片段中的方法进行更新,但我似乎无法弄清楚如何获得由活动引用片段,以更新它。
I want to periodically update a fragment's display based on data I download from the Internet. I have created a Timer and Runnable to periodically retrieve this data as well as a method within the fragment to update it, but I cannot seem to figure out how to gain a reference from the activity to the fragment in order to update it.
我有大部分是由ADT的Android项目向导生成以下code:
I have the following code which was mostly generated by the ADT's Android Project wizard:
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));
}
下面是用于创建选项卡中的code:
Here is the code used to create the tabs:
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;
}
}
我已经使用这个解决方案的尝试:
I have tried using this solution:
<一个href="http://stackoverflow.com/questions/16295101/how-to-change-fragments-textviews-text-from-activity">How从活动更改片段的TextView的文本
但我没有片段ID。我可以创建标签?如果是这样,我怎么做,在这种情况下?其他SO帖所提到的捆绑,但在创建片段时,我不发送数据;我想有定期更新的片段从活动数据可用。
But I don't have the fragment ID. Can I create tags? If so, how do I do that in this case? Other SO posts have mentioned Bundles but I am not sending data when creating the fragment; I want to have the fragment periodically updated as data becomes available from the Activity.
推荐答案
您不能给你的片段一个标签或标识,但你可以创建你的片段类的定制属性,以纪念他们。
You cannot give your fragments a tag or id but you can create a custom property on your fragment class to mark them.
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;
}
当然后可以通过所有的片段循环,找到你需要的一个
When can then loop through all the fragments and find the one you need
List<Fragment> allFragments = getSupportFragmentManager().getFragments();
if (allFragments != null) {
for (Fragment fragment : allFragments) {
Fragment1 f1 = (Fragment1)fragment;
if (f1.fragmentType == 1)
f1.updateFragmentData();
}
}
}
添加一个公共方法到您的片段会在你的片段更新数据。当你有一个参考吧,现在,你可以从你的活动中调用它。
Add a public method to your fragment that will update the data in your fragment. As you have a reference to it now, you can just call it from your activity.
这篇关于如何更新从活动Android的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!