我有以下FragmentActivity
:
public class DiagramsActivity extends FragmentActivity implements TabListener, OnPageChangeListener {
private ViewPager viewPager = null;
private FragmentPagerAdapter fragmentPager = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.fragmentPager = new DiagramsFragmentPagerAdapter(this.getSupportFragmentManager());
this.setContentView(R.layout.activity_diagrams);
this.viewPager = (ViewPager) this.findViewById(R.id.diagrams_pager);
this.viewPager.setOnPageChangeListener(this);
this.viewPager.setAdapter(this.fragmentPager);
this.actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_line)).setTabListener(this));
this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_bar)).setTabListener(this));
this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_scatter)).setTabListener(this));
this.actionBar.addTab(actionBar.newTab().setText(this.getResources().getString(R.string.text_pie)).setTabListener(this));
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
this.viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {}
@Override
public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {}
@Override
public void onPageScrollStateChanged(int state) {}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
@Override
public void onPageSelected(int position) {
this.actionBar.setSelectedNavigationItem(position);
}
}
采用这种布局:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/diagrams_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:padding="10dip"
tools:context="de.controller.activities.DiagramsActivity" >
</android.support.v4.view.ViewPager>
适配器:
public class DiagramsFragmentPagerAdapter extends FragmentPagerAdapter {
public DiagramsFragmentPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0 : {
return new LineFragment();
}
case 1 : {
return new BarFragment();
}
case 2 : {
return new ScatterFragment();
}
case 3 : {
return new PieFragment();
}
}
return null;
}
@Override
public int getCount() {
return 4;
}
}
我有几个标签,例如以下内容:
public class LineFragment extends Fragment {
private GraphicalView graphicalView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_diagrams_line_fragment, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
if (this.graphicalView == null) {
List<Double> xValues = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0);
List<Double> yValues = Arrays.asList(30.0, 34.0, 45.0, 57.0, 77.0, 89.0, 100.0, 111.0, 123.0, 145.0);
this.graphicalView = new LineGraph().getLineGraph(this.getActivity().getApplicationContext(), xValues, yValues);
LinearLayout layout = (LinearLayout) this.getActivity().findViewById(R.id.diagrams_line_layout);
layout.addView(this.graphicalView, new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 500));
} else {
this.graphicalView.repaint();
}
}
}
我在此处创建的用于绘制图表的数据在onResume()
内手动设置以进行测试。
我现在要做的是创建/加载我想在我的FragmentActivity
中表示的数据,我希望我的所有标签都使用FragmentActivity
中的数据,并在{onResume
时更新它1}}被调用。因为我需要在每个标签中有一个按钮,它启动用于选择数据的对话框。
如何在每个标签页面中创建一个对话框,将数据返回到FragmentActivity
,之后更新新数据中的所有标签?
答案 0 :(得分:0)
有两个直接的选择:
在您的片段getActivity()
中使用onResume()
来获取“父”活动的属性或“意图”
通过Fragment instantiation使用bundle
和数据:
公共类DiagramsFragmentPagerAdapter扩展了FragmentPagerAdapter {
@Override
public Fragment getItem(int index) {
Bundle b = new Bundle();
b.putString( "someString", this.someString );
b.putParcelable( "someObject", this.someObject() );
switch (index) {
case 0 : {
return Fragment.instantiate( this, LineFragment.class.getName(), b );
}
}
}