|
| 1 | +package com.fadisu.cpurun.fragment; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.os.Bundle; |
| 5 | +import android.os.Handler; |
| 6 | +import android.os.Message; |
| 7 | +import android.support.v4.app.Fragment; |
| 8 | +import android.view.LayoutInflater; |
| 9 | +import android.view.View; |
| 10 | +import android.view.ViewGroup; |
| 11 | +import android.widget.ListView; |
| 12 | +import android.widget.TextView; |
| 13 | + |
| 14 | +import com.fadisu.cpurun.R; |
| 15 | +import com.fadisu.cpurun.adapter.CustomAdapter; |
| 16 | +import com.fadisu.cpurun.util.CpuUtils; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | + |
| 22 | +public class CpuVoltageFragment extends Fragment implements CustomAdapter.LayoutView { |
| 23 | + |
| 24 | + public static final int UPDATE_UI = 0; |
| 25 | + |
| 26 | + private boolean isRun; |
| 27 | + private Context mContext; |
| 28 | + private List<String> result; |
| 29 | + private Thread mThread; |
| 30 | + private CustomAdapter<String> mCustomAdapter; |
| 31 | + |
| 32 | + private View mView; |
| 33 | + private ListView mListView; |
| 34 | + |
| 35 | + private Handler mHandler = new Handler() { |
| 36 | + |
| 37 | + @Override |
| 38 | + public void handleMessage(Message msg) { |
| 39 | + switch (msg.what) { |
| 40 | + case UPDATE_UI: |
| 41 | + mCustomAdapter.updateData((ArrayList<String>) result); |
| 42 | + break; |
| 43 | + default: |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + }; |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onAttach(Context context) { |
| 51 | + super.onAttach(context); |
| 52 | + mContext = context; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 57 | + mView = inflater.inflate(R.layout.fragment_base, container, false); |
| 58 | + initViews(); |
| 59 | + initValues(); |
| 60 | + initListeners(); |
| 61 | + return mView; |
| 62 | + } |
| 63 | + |
| 64 | + private void initViews() { |
| 65 | + mListView = (ListView) mView.findViewById(R.id.listview); |
| 66 | + } |
| 67 | + |
| 68 | + private void initValues() { |
| 69 | + result = CpuUtils.getCpuVoltage(); |
| 70 | + mCustomAdapter = new CustomAdapter<String>(result); |
| 71 | + mListView.setAdapter(mCustomAdapter); |
| 72 | + mHandler.sendEmptyMessage(UPDATE_UI); |
| 73 | + } |
| 74 | + |
| 75 | + private void initListeners() { |
| 76 | + mCustomAdapter.setLayoutView(this); |
| 77 | + } |
| 78 | + |
| 79 | + private void startTask() { |
| 80 | + mThread = new Thread(new Runnable() { |
| 81 | + @Override |
| 82 | + public void run() { |
| 83 | + while (isRun) { |
| 84 | + try { |
| 85 | + Thread.sleep(2000); |
| 86 | + } catch (InterruptedException e) { |
| 87 | + e.printStackTrace(); |
| 88 | + } |
| 89 | + |
| 90 | + if (null != result) { |
| 91 | + result.clear(); |
| 92 | + result.addAll(CpuUtils.getCpuVoltage()); |
| 93 | + mHandler.sendEmptyMessage(UPDATE_UI); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + mThread.start(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onResume() { |
| 105 | + super.onResume(); |
| 106 | + isRun = true; |
| 107 | + |
| 108 | + startTask(); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void onStop() { |
| 113 | + isRun = false; |
| 114 | + |
| 115 | + if (mThread != null) { |
| 116 | + mThread.interrupt(); |
| 117 | + } |
| 118 | + super.onStop(); |
| 119 | + } |
| 120 | + |
| 121 | + class ViewHolder { |
| 122 | + TextView tv_info; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public <T> View setView(int position, View convertView, ViewGroup parent) { |
| 127 | + ViewHolder holder = null; |
| 128 | + if (convertView == null) { |
| 129 | + convertView = LayoutInflater.from(mContext).inflate(R.layout.item_base, null); |
| 130 | + holder = new ViewHolder(); |
| 131 | + convertView.setTag(holder); |
| 132 | + |
| 133 | + holder.tv_info = (TextView) convertView.findViewById(R.id.tv_info); |
| 134 | + } else { |
| 135 | + holder = (ViewHolder) convertView.getTag(); |
| 136 | + } |
| 137 | + |
| 138 | + holder.tv_info.setText(result.get(position)); |
| 139 | + |
| 140 | + return convertView; |
| 141 | + } |
| 142 | +} |
0 commit comments