如何将数据从 activity 发送到片段以更新图表?

How to send data from activity to the fragment for update a graph?

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 用数据更新图表。

最好的方法是什么?我已经阅读了如何将数据从 activity 发送到带有接口或捆绑包的片段,但是为了保持图表的更新,最好的解决方案是什么?

例如,您可以在 GraphUpdate class 中创建一个名为 updateFragment() 的方法。然后,从您的 activity.

调用该方法

你不需要使用接口也只需要在该片段中写一个 public 方法。只需调用该方法即可。如果你想发送数据,请使用方法 will arguments.

只需在片段中创建一个实际更新图形的方法。并使用来自 activity 调用更新图形方法的片段对象,该方法已在片段 simple.e.x.

中声明
frag=(cast)getSupportFragmentManager().findFragmentByTag("fragment tag");
frag.updateGraph() from your activity. 
and declare updateGraph method in fragment.  

在您的片段中使用具有您选择的 post 延迟的处理程序,并在您的 activity 中调用您需要的方法,获取数据并更新您的图表,继续更新使用 post 延迟在你的可运行文件中