当前位置: 移动技术网 > IT编程>开发语言>Java > Fragment之间的传递数据的方法

Fragment之间的传递数据的方法

2020年07月27日  | 移动技术网IT编程  | 我要评论

 其实如果面试官问了这个问题,主要是想引入EventBus

这图中的左侧是一个FirstFragment(里面是一个ListView),右侧是SecondFragment(里面是一个TextView)

1.传统方法

在Activity中

 FirstFragment firstFragment = new FirstFragment();
        SecondFragment secondFragment = new SecondFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_first,firstFragment,"firstFragment").commit();
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_second,secondFragment,"secondFragment").commit();

FirstFragment中 

ListView listView = view.findViewById(R.id.list_first);
        final String[] strings = new String[]{"1", "2", "3", "4", "5"};
        ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, strings);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                SecondFragment secondFragment =
                        (SecondFragment) getActivity().getSupportFragmentManager()
                                .findFragmentByTag("secondFragment");
                secondFragment.setData(strings[position]);
            }
        });

 SecndFragment中

public void setData(String str) {
        mTextView.setText(str);
    }

2.EventBus

EventBus一种优化的事件总线,在Fragment,Activity,Service,线程间传递消息,有三要素

  • Event:事件,可以是任意类型的对象
  • Subscriber:事件订阅者,就是接受信息的
  • Publisher:事件发布者,就是发送消息的,可以在任意位置发送事件,调用post方法

用法很简单,先导入依赖

    implementation 'org.greenrobot:eventbus:3.0.0'

在接收消息的那个地方的oncreateView里进行注册

 EventBus.getDefault().register(this);

在发送消息的地方发出消息,这里还可以自己去定义一个数据类 

ListView listView = view.findViewById(R.id.list_first);
        final String[] strings = new String[]{"1", "2", "3", "4", "5"};
        ArrayAdapter arrayAdapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, strings);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                EventBus.getDefault().post(strings[position]);
            }
        });

在接收的界面收到消息,头上的类型有四种

分别是:

  • POSTING(默认,也就是和发送是一种线程)
  • MAIN:主线程
  • BACKGROUND:如果发送线程是UI,那么就新建线程,如果发送线程是子线程,就在发送线程
  • ASYNC:无论事件在哪个线程发布,都会在新建的子线程中

还有这个函数名是自己顺便命名,但头上有@subscribe这个标志

@Subscribe(threadMode = ThreadMode.MAIN)
    public void ReceiveMess(String str){
        mTextView.setText(str);
    }

最后别忘了注销

@Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

发布版需要加混淆规则

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

EventBus的相关问题

EventBus的优点:

  • 这个开源框架是针对Android优化的发布/订阅事件总线,减少程序的耦合;
  • 不依赖于context;
  • 可以自己定义传递数据的类型

EventBus2.x和3.0的区别:

  • 2.x采用反射的方法来对所有的方法进行扫描完成注册;
  • 3.0提供了注解处理器在编译期通过读取@Subscribe注解来解析,比在运行时使用反射速度要快
  • 3.0可以自定义方法名

 

 

 

 

 

 

 

 

 

 

 

 

 

本文地址:https://blog.csdn.net/qq873044564/article/details/107584244

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网