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("開(kāi)始");
}
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("開(kāi)始");
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類(lèi)
/**
* @Date 2018/4/26
* @Introduction 倒計(jì)時(shí)數(shù)據(jù)實(shí)體類(lèi)
*/
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手電筒兼容各個(gè)手機(jī)與版本
欄 目:Android
下一篇:Android 7.0 手電筒控制實(shí)現(xiàn)
本文標(biāo)題:android實(shí)現(xiàn)條目倒計(jì)時(shí)功能
本文地址:http://www.jygsgssxh.com/a1/Android/9181.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在A(yíng)ndroid JNI中的兼容性問(wèn)題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶(hù)名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在A(yíng)ndroid JNI中的兼容性問(wèn)題詳
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什


