|
| 1 | +package pers.example.xiayong.rxjavasamples.fragments; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.os.Handler; |
| 5 | +import android.os.Looper; |
| 6 | +import android.support.annotation.Nullable; |
| 7 | +import android.view.LayoutInflater; |
| 8 | +import android.view.View; |
| 9 | +import android.view.ViewGroup; |
| 10 | +import android.widget.Button; |
| 11 | +import android.widget.ListView; |
| 12 | + |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import butterknife.Bind; |
| 17 | +import butterknife.ButterKnife; |
| 18 | +import butterknife.OnClick; |
| 19 | +import pers.example.xiayong.rxjavasamples.R; |
| 20 | +import pers.example.xiayong.rxjavasamples.wiring.LogAdapter; |
| 21 | +import rx.Observable; |
| 22 | +import rx.functions.Action1; |
| 23 | + |
| 24 | +public class IterateArrayFragment extends BaseFragment { |
| 25 | + |
| 26 | + @Bind(R.id.btn_start) |
| 27 | + Button btnStart; |
| 28 | + @Bind(R.id.list_threading_log) |
| 29 | + ListView listThreadingLog; |
| 30 | + |
| 31 | + private LogAdapter _adapter; |
| 32 | + private List<String> _logs; |
| 33 | + @Override |
| 34 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, |
| 35 | + Bundle savedInstanceState) { |
| 36 | + // Inflate the layout for this fragment |
| 37 | + View view = inflater.inflate(R.layout.fragment_iterate_array, container, false); |
| 38 | + ButterKnife.bind(this, view); |
| 39 | + return view; |
| 40 | + } |
| 41 | + @Override |
| 42 | + public void onActivityCreated(@Nullable Bundle savedInstanceState) { |
| 43 | + super.onActivityCreated(savedInstanceState); |
| 44 | + _setupLogger(); |
| 45 | + } |
| 46 | + @OnClick(R.id.btn_start) |
| 47 | + public void iterateArray() { |
| 48 | + String[] names = {"Tom", "Lily", "Alisa", "Sheldon", "Bill"}; |
| 49 | + Observable |
| 50 | + .from(names) |
| 51 | + .subscribe(new Action1<String>() { |
| 52 | + @Override |
| 53 | + public void call(String s) { |
| 54 | + _log(s); |
| 55 | + } |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void onDestroyView() { |
| 61 | + super.onDestroyView(); |
| 62 | + ButterKnife.unbind(this); |
| 63 | + } |
| 64 | + |
| 65 | + // ----------------------------------------------------------------------------------- |
| 66 | + // Methods that help wiring up the example (irrelevant to RxJava) |
| 67 | + |
| 68 | + private void _setupLogger() { |
| 69 | + _logs = new ArrayList<>(); |
| 70 | + _adapter = new LogAdapter(getActivity(), new ArrayList<String>()); |
| 71 | + listThreadingLog.setAdapter(_adapter); |
| 72 | + } |
| 73 | + |
| 74 | + private void _log(String logMsg) { |
| 75 | + |
| 76 | + if (_isCurrentlyOnMainThread()) { |
| 77 | + _logs.add(0, logMsg + " (main thread) "); |
| 78 | + _adapter.clear(); |
| 79 | + _adapter.addAll(_logs); |
| 80 | + } else { |
| 81 | + _logs.add(0, logMsg + " (NOT main thread) "); |
| 82 | + |
| 83 | + // You can only do below stuff on main thread. |
| 84 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 85 | + |
| 86 | + @Override |
| 87 | + public void run() { |
| 88 | + _adapter.clear(); |
| 89 | + _adapter.addAll(_logs); |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private boolean _isCurrentlyOnMainThread() { |
| 96 | + return Looper.myLooper() == Looper.getMainLooper(); |
| 97 | + } |
| 98 | +} |
0 commit comments