public class GraphUpdate extends Fragment {
private final Handler mHandler = new Handler();
private Runnable mTimer;
private LineGraphSeries<DataPoint> mSeries;
private double graph2LastXValue = 5d;
private int value;
private String TAG = "FRAGMENT";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
GraphView graph = (GraphView) rootView.findViewById(R.id.graph);
mSeries = new LineGraphSeries<DataPoint>();
graph.addSeries(mSeries);
graph.getViewport().setXAxisBoundsManual(true);
graph.getViewport().setMinX(0);
graph.getViewport().setMaxX(40);
return rootView;
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
mTimer = new Runnable() {
@Override
public void run() {
Log.d(TAG, "onResume");
graph2LastXValue += 1d;
mSeries.appendData(new DataPoint(graph2LastXValue, value), true, 40);
mHandler.postDelayed(this, 1000);
}
};
mHandler.postDelayed(mTimer, 300);
}
我必须使用我的 Main_Activity 的数据更新此图表。
MainActivity 在运行时发送一些数据到我的 Fragment。 GraphUpdate 使用数据更新图表。
最好的方法是什么?我已经阅读了如何使用接口(interface)或 bundle 将数据从 Activity 发送到 fragment ,但是为了保持更新图表,最好的解决方案是什么?
最佳答案
只需在 fragment 中创建一个实际更新图形的方法。并使用 Activity 中的 fragment 对象调用已在 fragment simple.e.x 中声明的更新图形方法。
frag=(cast)getSupportFragmentManager().findFragmentByTag("fragment tag");
frag.updateGraph() from your activity.
and declare updateGraph method in fragment.
关于android - 如何将数据从 Activity 发送到 fragment 以更新图表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29892681/