博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【框架】EventBus
阅读量:5825 次
发布时间:2019-06-18

本文共 4207 字,大约阅读时间需要 14 分钟。

EventBus线程问题

 DEMO1:

public class EventBusMessage {    private int what;    private int arg1;    private int arg2;    public EventBusMessage() {    }    public int getArg1() {        return arg1;    }    public void setArg1(int arg1) {        this.arg1 = arg1;    }    public int getArg2() {        return arg2;    }    public void setArg2(int arg2) {        this.arg2 = arg2;    }    public int getWhat() {        return what;    }    public void setWhat(int what) {        this.what = what;    }    public EventBusMessage(int arg1, int arg2, int what) {        this.arg1 = arg1;        this.arg2 = arg2;        this.what = what;    }}
/** * 1.加载依赖库 * 2.发送消息 * 3.订阅消息 */public class MainActivity extends AppCompatActivity {    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        EventBus.getDefault().register(this);        tv = ((TextView) findViewById(R.id.tv));    }    public void btnClick(View view) {        //使用EventBus发送一条消息//        new Thread(new Runnable() {//            @Override//            public void run() {
EventBusMessage event = new EventBusMessage(); event.setWhat(10); EventBus.getDefault().post(event); Log.d("google_lenve_fb", "btnClick: " + Thread.currentThread().getName());// }// }).start(); } //订阅消息 @Subscribe(threadMode = ThreadMode.BACKGROUND) public void changeTextViewText(EventBusMessage msg) { Log.d("google_lenve_fb", "changeTextViewText: " + Thread.currentThread().getName());// tv.setText(str); Toast.makeText(MainActivity.this, "123456:" + msg.getWhat(), Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); }}

 

DEMO2:

效果图:

public class LeftFragment extends Fragment {    private List
list; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.left_fg_layout, null); ListView listView = (ListView) view.findViewById(R.id.lv); initData(); ArrayAdapter
adapter = new ArrayAdapter
(getActivity(), android.R.layout.simple_list_item_1, list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView
parent, View view, int position, long id) { EventBus.getDefault().post(list.get(position)); } }); return view; } private void initData() { list = new ArrayList<>(); list.add("day1.txt"); list.add("day2.txt"); list.add("day3.txt"); list.add("day4.txt"); }}
public class RightFragment extends Fragment {    private TextView tv;    @Nullable    @Override    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.right_fg_layout, null);        tv = ((TextView) view.findViewById(R.id.content));        EventBus.getDefault().register(this);        return view;    }    @Subscribe    public void changeText(String fileName) {        InputStream is = null;        StringBuffer result = new StringBuffer();        try {            is = getActivity().getAssets().open(fileName);            int len = 0;            byte[] buf = new byte[1024];            while ((len = is.read(buf)) != -1) {                result.append(new String(buf, 0, len));            }            tv.setText(result.toString());        } catch (IOException e) {            e.printStackTrace();        }finally {            if (is != null) {                try {                    is.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    @Override    public void onDestroy() {        super.onDestroy();        EventBus.getDefault().unregister(this);    }}

 

转载地址:http://djsdx.baihongyu.com/

你可能感兴趣的文章
倒计时:计算时间差
查看>>
Linux/windows P2V VMWare ESXi
查看>>
Windows XP倒计时到底意味着什么?
查看>>
tomcat一步步实现反向代理、负载均衡、内存复制
查看>>
运维工程师在干什么学些什么?【致菜鸟】
查看>>
Linux中iptables详解
查看>>
java中回调函数以及关于包装类的Demo
查看>>
maven异常:missing artifact jdk.tools:jar:1.6
查看>>
终端安全求生指南(五)-——日志管理
查看>>
Nginx 使用 openssl 的自签名证书
查看>>
创业维艰、守成不易
查看>>
PHP环境安装套件:快速安装LAMP环境
查看>>
CSS3
查看>>
ul下的li浮动,如何是ul有li的高度
查看>>
C++ primer plus
查看>>
python mysqlDB
查看>>
UVALive 3942 Remember the Word Tire+DP
查看>>
从微软的DBML文件中我们能学到什么(它告诉了我们什么是微软的重中之重)~目录...
查看>>
被需求搞的一塌糊涂,怎么办?
查看>>
c_数据结构_队的实现
查看>>