【发布时间】:2014-09-02 09:12:13
【问题描述】:
我有一个从FragmentActivity 扩展的活动,我有四个Fragments 的TABS,我在主FragmentActivity 中有AsyncTask 以从服务器获取负载数据,所以,我想更新每个片段数据当在父 FragmentActivity 上完成 AsyncTask 函数时,因为我使用 android.support.v4.view.ViewPager 在使用空白数据启动 Activity 时加载所有 Fragment 的 UI,所以,我想用从 AsyncTask 获取的数据更新每个片段字段(TextView),我怎么能更新。感谢你的帮助。以下代码用于获取片段
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment fragmenthome = new HomeFragment();
return fragmenthome;
case 1:
Fragment fragmentdiagnosis = new DiagnosisFragment();
return fragmentdiagnosis;
case 2:
Fragment fragmentcareplan = new CareplanFragment();
return fragmentcareplan;
case 3:
Fragment fragmentnote = new NoteFragment();
return fragmentnote;
case 4:
Fragment fragmenttask = new TaskFragment();
return fragmenttask;
}
return null;
}
这是一个示例片段
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText("Comming Soon...");
return rootView;
}
}
这里是主要活动 公共类 PatientActivity 扩展 FragmentActivity 实现 ActionBar.TabListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_patient);
// AsyncTask function calling
}
【问题讨论】:
-
代码是基本的Activity,带有Fragments的TABS,使用SectionsPagerAdapter mSectionsPagerAdapter;
-
您可以在 onResume() 中使用条件处理,如果 onPost 被执行,然后将标志标记为 true 并在那里执行更新代码
-
实际上,我正在通过回调函数从 Parse 对象中检索数据,该回调在主要活动(FragmentActivity)的 AsyncTask 中,我想将 Parse 对象数据更新为 4 个选项卡(片段)文本视图字段
-
您可以使用创建一个
interface并在所有片段中覆盖它的方法。
标签: android android-fragments android-asynctask