android實(shí)現(xiàn)條目倒計(jì)時(shí)功能
網(wǎng)上對(duì)于這樣的功能已經(jīng)是泛濫成河了,但是最近遇到這樣的一個(gè)需求,還是要值得我們學(xué)習(xí)一下,并將他記錄下來(lái)。
布局文件:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.rui.demo.list_count_down.ListCountDownActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/rv_list_count_down" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v7.widget.RecyclerView> </FrameLayout>
Activity界面:
public class ListCountDownActivity extends AppCompatActivity { RecyclerView mRecyclerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_count_down); initView(); } private void initView() { mRecyclerView = (RecyclerView) findViewById(R.id.rv_list_count_down); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); CountDownAdapter adapter = new CountDownAdapter(); mRecyclerView.setAdapter(adapter); List<DataInfo> list = new ArrayList<>(); for (int i = 1; i < 101; i++) { list.add(new DataInfo("我是條目" + i, i * 100)); } adapter.setmDatas(list); } }
倒計(jì)時(shí)條目適配器:
/** * @Date 2018/4/26 * @Introduction 倒計(jì)時(shí)條目適配器 */ public class CountDownAdapter extends RecyclerView.Adapter<CountDownAdapter.MyViewHoder> { private final String TAG = CountDownAdapter.class.getSimpleName(); private final int STOP = 0x01; private final int START = 0x02; private final int LOOP = 0x03; private List<DataInfo> mDatas; public CountDownAdapter() { } public void setmDatas(List<DataInfo> mDatas) { this.mDatas = mDatas; notifyDataSetChanged(); } @Override public MyViewHoder onCreateViewHolder(ViewGroup parent, int viewType) { return new MyViewHoder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_count_down, parent, false)); } @Override public void onBindViewHolder(final MyViewHoder holder, int position) { final DataInfo info = mDatas.get(position); holder.tvName.setText(info.getName()); holder.tvTime.setText(info.getTime() + ""); if (info.isCountDown()) { holder.btnStart.setText("停止"); } else { holder.btnStart.setText("開始"); } holder.btnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Message msg = Message.obtain(); if (!info.isCountDown()) { holder.btnStart.setText("停止"); msg.what = START; } else { holder.btnStart.setText("開始"); msg.what = STOP; } msg.obj = info; mHandler.sendMessage(msg); info.setCountDown(!info.isCountDown()); } }); } @Override public int getItemCount() { return mDatas == null ? 0 : mDatas.size(); } static class MyViewHoder extends RecyclerView.ViewHolder { private final TextView tvName; private final TextView tvTime; private final Button btnStart; public MyViewHoder(View itemView) { super(itemView); tvName = (TextView) itemView.findViewById(R.id.tv_name_count_down_item); tvTime = (TextView) itemView.findViewById(R.id.tv_time_count_down_item); btnStart = (Button) itemView.findViewById(R.id.btn_time_count_down_item); } } private Handler mHandler = new Handler() { private List<DataInfo> mCountDownList = new ArrayList<>(); @Override public void handleMessage(Message msg) { setChange(msg); } private synchronized void setChange(Message msg) { switch (msg.what) { case STOP: DataInfo stopInfo = (DataInfo) msg.obj; Log.e(TAG, "------------stop:" + stopInfo.getName()); mCountDownList.remove(stopInfo); notifyDataSetChanged(); if (mCountDownList.size() <= 0) { mHandler.removeCallbacksAndMessages(null); } break; case START: DataInfo startInfo = (DataInfo) msg.obj; Log.e(TAG, "------------start:" + startInfo.getName()); if (startInfo.getTime() > 0) { mCountDownList.add(startInfo); mHandler.sendEmptyMessageDelayed(LOOP, 1000); } break; case LOOP: if (mCountDownList.size() <= 0) { return; } for (Iterator<DataInfo> iterator = mCountDownList.iterator(); iterator.hasNext(); ) { DataInfo dataInfo = iterator.next(); int time = dataInfo.getTime(); time--; dataInfo.setTime(time); if (time <= 0) { iterator.remove(); } } notifyDataSetChanged(); mHandler.removeCallbacksAndMessages(null); mHandler.sendEmptyMessageDelayed(LOOP, 1000); break; default: break; } } }; }
JavaBean類
/** * @Date 2018/4/26 * @Introduction 倒計(jì)時(shí)數(shù)據(jù)實(shí)體類 */ public class DataInfo { private String name; private int time; private boolean isCountDown = false; public DataInfo(String name, int time) { this.name = name; this.time = time; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTime() { return time; } public void setTime(int time) { this.time = time; } public boolean isCountDown() { return isCountDown; } public void setCountDown(boolean countDown) { isCountDown = countDown; } }
以上就是條目中倒計(jì)時(shí)的一個(gè)小Demo。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義圓環(huán)倒計(jì)時(shí)控件
- Android倒計(jì)時(shí)控件 Splash界面5秒自動(dòng)跳轉(zhuǎn)
- Android倒計(jì)時(shí)的開始與停止 剩余時(shí)分秒的展示
- Android仿Keep運(yùn)動(dòng)休息倒計(jì)時(shí)圓形控件
- android自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開屏布局
- Android 實(shí)現(xiàn)閃屏頁(yè)和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- android自定義圓形倒計(jì)時(shí)顯示控件
相關(guān)文章
Android獲取手機(jī)SIM卡運(yùn)營(yíng)商信息的方法
這篇文章主要介紹了Android獲取手機(jī)SIM卡運(yùn)營(yíng)商信息的方法,可獲得手機(jī)的型號(hào)、運(yùn)營(yíng)商信息及系統(tǒng)版本等,需要的朋友可以參考下2014-09-09Android ListView實(shí)現(xiàn)圖文列表顯示
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)圖文列表顯示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Android 媒體庫(kù)數(shù)據(jù)更新方法總結(jié)
這篇文章主要介紹了Android 媒體庫(kù)數(shù)據(jù)更新方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04Android開發(fā)graphics?bufferqueue整體流程
這篇文章主要為大家介紹了Android開發(fā)graphics?bufferqueue整體流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08Android?AccessibilityService?事件分發(fā)原理分析總結(jié)
這篇文章主要介紹了Android?AccessibilityService?事件分發(fā)原理分析總結(jié),AccessibilityService有很多用來(lái)接收外部調(diào)用事件變化的方法,這些方法封裝在內(nèi)部接口Callbacks中,文章圍繞AccessibilityService相關(guān)資料展開詳情,需要的朋友可以參考一下2022-06-06Android?Flutter實(shí)現(xiàn)"斑馬紋"背景的示例代碼
本文將通過(guò)實(shí)現(xiàn)一個(gè)canvas繪制斑馬紋類。使用Stack布局,將斑馬紋放在下方作為背景板,需要展示的內(nèi)容在上方。從而實(shí)現(xiàn)?“斑馬紋”背景,感興趣的可以了解一下2022-06-06