android - 如何将数据从 Activity 发送到 fragment 以更新图表?

标签 android android-fragments android-activity android-graphview

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/

相关文章:

android - 在仪器测试执行期间切换 Android 设备网络

android - 适用于 Android/iPhone 应用程序的广告中介 - 服务器端

java - 从 native 代码的回调中写入 AudioTrack 实例时出现 NullPointerException

android - 首次恢复时 Adob​​e AIR 24+ Android 黑屏

android - 从外部类文件中定义的监听器启动 Android Activity

java - Android VM 有命令行选项吗?

android - 为什么在按下后退按钮时会调用当前 fragment 的 oncreateview?

android - 类型 类型不匹配 : cannot convert from RegisterFragment to Fragment

android - 同时制作 fragment 过渡动画 View

java - 2 名玩家井字游戏