简体   繁体   English

Android我自己的听众

[英]Android my own listener

I have an ActionBar and it has Tabs. 我有一个ActionBar,并且有标签。 In the MainActivity there is a String variable. 在MainActivity中,有一个String变量。 In one Tab there is a TextView and a method setTv(String string) what can change the text in it. 在一个选项卡中,有一个TextView和一个setTv(String string)方法,可以更改其中的文本。

I would like to make a Listener interface what can call the setTv(String string) method, when the str String is changes in the MainActivity. 我想做一个侦听器接口,当str字符串在MainActivity中更改时,可以调用setTv(String string)方法。

Could anybody help me and fill in this code with the implementation? 有人可以帮我在实现中填写此代码吗?

This is the MainActivity code: 这是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() {            

        });

}

This is the Fragment code: 这是片段代码:

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);
    }

}

Get your current fragment in your viewpager: 在viewpager中获取当前片段:

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

Call your method where you want(after change str): 在所需的位置调用您的方法(更改str后):

fragment.setTv("string");

For the best way i recommend to you make a class that extends Application 为了最好的方式,我建议您制作一个扩展Application的类

from the docs : 从文档:

Base class for those who need to maintain global application state 需要维护全局应用程序状态的人员的基类

So you can store your str variable in this class and notify any launched Activities of variable state change. 因此,您可以将str变量存储在此类中,并通知任何已启动的Activity变量状态更改。 Actually i recommend to use 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 : StringObserver:

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

And usage : 和用法:

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
    }

}

In other Activity/Fragment : 在其他活动/片段中:

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.
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM