如何动态地更新片段视图 [英] how dynamically update fragment view

查看:229
本文介绍了如何动态地更新片段视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现我的大学申请,我是从一个专家:)需要一些建议。我有使用 PageIndicator 一个活动,每一个页面是一个片段。当前显示最大的一页。在每一个片段有4个窗口小部件包含通过蓝牙应用程序读取值(也就是通过蓝牙发送数据到应用的串行设备)我认为我的活动必须有一个独立的线程,在一个循环与串行设备进行通信。但是,当该线程读取值,如何能够将数据发送到当前片段,特别是在指定窗口小部件?当前控件必须显示读取最新的数据。换句话说,我需要知道如何活动的线程可以发送拿督到当前片段。
对不起,我的英语

I have to implement an application for my university and I need some advice from an expert :) . I have an activity that use PageIndicator, every page is a fragment. Max one page is currently displayed. In every fragment there are 4 widget that contains a value read from application via Bluetooth (there is a serial device that send data via Bluetooth to the application) I think that my activity must have a independent thread that in a loop communicate with the serial devices. But when this thread reads value, how can send the data to the current fragment, in particular to the specified widget? the current widget must show most recent data read. In other words i need to know how a thread of activity can send a dato to the current fragment . sorry for my english

推荐答案

在创建片段,记录他们在一个ArrayList,并创建一个扩展片段定制的片断类

while creating the fragments, record them in an ArrayList and create a custom fragment class that extends Fragment

static public ArrayList<CustomFragment> fragmentsList = new ArrayList<CustomFragment>();

// create new fragment
// Example:
// CustomFragment myFragment = new FragmentOne();

fragmentsList.add(newlyCreatedFragment);

和片段中创建一个方法来接收更新,例如:

and inside the fragment create a method to receive the updates, for example

您片段类:

public class CustomFragment extends Fragment {

    public void receiveUpdate(String update) {
      // Do something
    }
}

然后,而不是延伸片段使用扩展CustomFragment

then instead of "extends Fragment" use "extends CustomFragment"

然后再从其他类

CustomFragment theFragment = SomeClass.fragmentsList.get(0); // where 0 is the fragment's index
theFragment.receiveUpdate("the update");

这篇关于如何动态地更新片段视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆