如何从活动更新 Android 片段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22751127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update Android fragment from activity?
提问by gonzobrains
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.
我想根据从 Internet 下载的数据定期更新片段的显示。我创建了一个 Timer 和 Runnable 来定期检索这些数据以及片段中的一个方法来更新它,但我似乎无法弄清楚如何从活动中获取对片段的引用以更新它。
I have the following code which was mostly generated by the ADT's Android Project wizard:
我有以下代码,主要由 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));
}
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:
我曾尝试使用此解决方案:
How to change fragment's textView's text from activity
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.
但我没有片段 ID。我可以创建标签吗?如果是这样,在这种情况下我该怎么做?其他 SO 帖子提到了 Bundles,但我在创建片段时没有发送数据;当数据从活动中可用时,我希望定期更新片段。
采纳答案by Szymon
You cannot give your fragments a tag or id but you can create a custom property on your fragment class to mark them.
你不能给你的片段一个标签或 ID,但你可以在片段类上创建一个自定义属性来标记它们。
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.
向片段添加一个公共方法,该方法将更新片段中的数据。由于您现在有对它的引用,因此您可以从您的活动中调用它。
回答by Eenvincible
Here is something you could try:
您可以尝试以下方法:
Since you mention updating the fragment from the activity (with retrieved data), you could do something like this:
由于您提到从活动更新片段(使用检索到的数据),您可以执行以下操作:
In your Runnable or AsyncTask, you can update the adapter with the data retrieved and in your fragment, call the onDatasetChanged() method on the adapter so it will automatically update the view.
在 Runnable 或 AsyncTask 中,您可以使用检索到的数据更新适配器,并在片段中调用适配器上的 onDatasetChanged() 方法,以便它自动更新视图。
If you have multiple fragments, inside those fragments, you could define an interface and then let the activity implement it and then override the method. From within that method in the activity, update the adapter that holds the data. You will have to make the adapter static!
如果您有多个片段,在这些片段中,您可以定义一个接口,然后让活动实现它,然后覆盖该方法。从活动中的该方法中,更新保存数据的适配器。您必须使适配器静态!
I hope this helps!
我希望这有帮助!