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