Android我自己的听众

时间:2015-01-15 18:28:51

标签: java android listener event-listener

我有一个ActionBar,它有Tabs。 在MainActivity中有一个String变量。 在一个选项卡中有一个TextView和一个方法setTv(String string),可以更改其中的文本。

当str字符串是MainActivity中的更改时,我想创建一个可以调用setTv(String string)方法的Listener接口。

有人可以帮助我并在实施中填写此代码吗?

这是MainActivity代码:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener, LocationListener{

    ActionBar actionbar;
    ViewPager viewpager;
    FragmentPageAdapter ft;
    String str;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        ft = new FragmentPageAdapter(getSupportFragmentManager());

        actionbar = getActionBar();
        viewpager.setAdapter(ft);
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.addTab(actionbar.newTab().setText("Run").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("Map").setTabListener(this));
        actionbar.addTab(actionbar.newTab().setText("Statistics").setTabListener(this));
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {            

        });

}

这是片段代码:

public class RunFragment extends Fragment {

    TextView tv;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.run_layout, container, false);
        tv =(TextView)view.findViewById(R.id.textView1);

        return view;

    }


    public void setTv(String string){
        tv.setText(string);
    }

}

2 个答案:

答案 0 :(得分:0)

在viewpager中获取当前片段:

RunFragment fragment = (RunFragment) ft.getItem(viewPager.getCurrentItem());

在您想要的地方调用您的方法(在更改str之后):

fragment.setTv("string");

答案 1 :(得分:0)

我建议你做一个扩展Application

的课程

来自文档:

  

需要维护全局应用程序状态的人员的基类

因此,您可以将str变量存储在此类中,并通知任何已启动的变量状态更改活动。实际上我建议使用Observer pattern

MyApplication:

public class MyApplication extends Application {
    private static MyApplication singleton;
    private String str;
    private ArrayList<StringObserver> observerList = new ArrayList<StringObserver>();
    public MyApplication getInstance(){
        return singleton;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        singleton = this;
    }

    public void setString(String str) {
        this.str = str;
        notifyObservers();
    }

    public void addObserver(StringObserver obs) {
        observerList.add(obs);
    }

    public void removeObserver(StringObserver obs) {
        observerList.remove(obs);
    }

    private void notifyObservers() {
        for(StringObserver obs : observableList) {
            obs.notifyAboutStringChanged(str);
        }
    }
}

StringObserver:

public interface StringObserver {
    void notifyAboutStringChanged(String str);
}

用法:

public class MainActivity extends FragmentActivity/*or Activity*/ implements StringObserver {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
        MyApplication appState = ((MyApplication )this.getApplication());
        appState.addObserver(this);
        appState.setString("This string was changed");
    }

    @Override
    public void notifyAboutStringChanged(String str) {
     // do something
    }

}

在其他活动/片段中:

public class RunFragment extends Fragment {

    TextView tv;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.run_layout, container, false);
        tv =(TextView)view.findViewById(R.id.textView1);

        return view;

    }


    public void setTv(String string){
        tv.setText(string);
        (MyApplication) getActivity().getApplication().setString(string); // this string notify your activity about value change.
    }

}