Android定时器和倒计时实现淘宝秒杀功能
作者:丶ban 发布时间:2023-01-18 02:17:04
标签:Android,定时器,倒计时,秒杀
本文实例为大家分享了Android实现淘宝秒杀的具体代码,供大家参考,具体内容如下
目录结构
效果图:
imageViewHolder
public class imageViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public imageViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView;
}
}
MyViewHolder
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public MyViewHolder(View itemView) {
super(itemView);
textView = (TextView) itemView;
}
}
recycleAdapter
package com.nodeprogress.snapupview.SnapUp;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class recycleAdapter extends RecyclerView.Adapter {
Context context;
public recycleAdapter(Context context) {
this.context = context;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == 0){
return new imageViewHolder(new ImageView(context));
}else {
return new MyViewHolder(new TextView(context));
}
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (getItemViewType(position) == 0) {
imageViewHolder viewHolder = (imageViewHolder) holder;
viewHolder.imageView.setPadding(150,20,20,20);
viewHolder.imageView.setBackgroundColor(Color.BLUE);
} else {
MyViewHolder viewHolder = (MyViewHolder) holder;
viewHolder.textView.setText(" 淘宝 " + position);
}
}
@Override
public int getItemCount() {
return 21;
}
@Override
public int getItemViewType(int position) {
return (position == 20) ? 0 : 1;
}
}
MainActivity
package com.nodeprogress.snapupview;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.nodeprogress.snapupview.SnapUp.recycleAdapter;
import com.nodeprogress.snapupview.View.HorizontalRecycleViewLoadMore;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HorizontalRecycleViewLoadMore recyclerView = (HorizontalRecycleViewLoadMore) findViewById(R.id.recycle);
recyclerView.setAdapter(new recycleAdapter(MainActivity.this));
SnapUpCountDownTimerView rushBuyCountDownTimerView = (SnapUpCountDownTimerView) findViewById(R.id.RushBuyCountDownTimerView);
rushBuyCountDownTimerView.setTime(1,55,3);
rushBuyCountDownTimerView.start();
}
}
SnapUpCountDownTimerView
package com.nodeprogress.snapupview;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Timer;
import java.util.TimerTask;
@SuppressLint("HandlerLeak")
public class SnapUpCountDownTimerView extends LinearLayout {
private TextView tv_hour_decade;
private TextView tv_hour_unit;
private TextView tv_min_decade;
private TextView tv_min_unit;
private TextView tv_sec_decade;
private TextView tv_sec_unit;
private Context context;
private int hour_decade;
private int hour_unit;
private int min_decade;
private int min_unit;
private int sec_decade;
private int sec_unit;
private Timer timer;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
countDown();
}
};
public SnapUpCountDownTimerView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.view_countdowntimer, this);
tv_hour_decade = (TextView) view.findViewById(R.id.tv_hour_decade);
tv_hour_unit = (TextView) view.findViewById(R.id.tv_hour_unit);
tv_min_decade = (TextView) view.findViewById(R.id.tv_min_decade);
tv_min_unit = (TextView) view.findViewById(R.id.tv_min_unit);
tv_sec_decade = (TextView) view.findViewById(R.id.tv_sec_decade);
tv_sec_unit = (TextView) view.findViewById(R.id.tv_sec_unit);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.SnapUpCountDownTimerView);
int size = array.getInteger(R.styleable.SnapUpCountDownTimerView_viewSize, 12);
tv_hour_decade.setTextSize(size);
tv_hour_unit.setTextSize(size);
tv_min_decade.setTextSize(size);
tv_min_unit.setTextSize(size);
tv_sec_decade.setTextSize(size);
tv_sec_unit.setTextSize(size);
((TextView)view.findViewById(R.id.colon_minute)).setTextSize(size);
((TextView)view.findViewById(R.id.colon_hour)).setTextSize(size);
}
public void start() {
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
}, 0, 1000);
}
}
public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
public void setTime(int hour, int min, int sec) {
if (hour >= 60 || min >= 60 || sec >= 60 || hour < 0 || min < 0
|| sec < 0) {
throw new RuntimeException("时间格式错误,请检查你的代码");
}
hour_decade = hour / 10;
hour_unit = hour - hour_decade * 10;
min_decade = min / 10;
min_unit = min - min_decade * 10;
sec_decade = sec / 10;
sec_unit = sec - sec_decade * 10;
tv_hour_decade.setText(hour_decade + "");
tv_hour_unit.setText(hour_unit + "");
tv_min_decade.setText(min_decade + "");
tv_min_unit.setText(min_unit + "");
tv_sec_decade.setText(sec_decade + "");
tv_sec_unit.setText(sec_unit + "");
}
private void countDown() {
if (isCarry4Unit(tv_sec_unit)) {
if (isCarry4Decade(tv_sec_decade)) {
if (isCarry4Unit(tv_min_unit)) {
if (isCarry4Decade(tv_min_decade)) {
if (isCarry4Unit(tv_hour_unit)) {
if (isCarry4Decade(tv_hour_decade)) {
Toast.makeText(context, "计数完成",
Toast.LENGTH_SHORT).show();
stop();
setTime(0, 0, 0);//重置为0
}
}
}
}
}
}
}
private boolean isCarry4Decade(TextView tv) {
int time = Integer.valueOf(tv.getText().toString());
time = time - 1;
if (time < 0) {
time = 5;
tv.setText(time + "");
return true;
} else {
tv.setText(time + "");
return false;
}
}
private boolean isCarry4Unit(TextView tv) {
int time = Integer.valueOf(tv.getText().toString());
time = time - 1;
if (time < 0) {
time = 9;
tv.setText(time + "");
return true;
} else {
tv.setText(time + "");
return false;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.nodeprogress.snapupview.MainActivity">
<include layout="@layout/home_snap_up"></include>
</RelativeLayout>
home_snap_up.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@android:color/white"
android:padding="15dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="秒杀"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp"
/>
<com.nodeprogress.snapupview.SnapUpCountDownTimerView
android:id="@+id/RushBuyCountDownTimerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
app:viewSize="12"
>
</com.nodeprogress.snapupview.SnapUpCountDownTimerView>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|right"
android:text="更多 >"
android:textSize="15sp"
/>
</LinearLayout>
<com.nodeprogress.snapupview.View.HorizontalRecycleViewLoadMore
android:id="@+id/recycle"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.nodeprogress.snapupview.View.HorizontalRecycleViewLoadMore>
</LinearLayout>
view_countdowntimer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_snap_up"
android:padding="5dp"
>
<TextView
android:id="@+id/tv_hour_decade"
style="@style/RushBuyCountDownTimerViewStyle"/>
<TextView
android:id="@+id/tv_hour_unit"
style="@style/RushBuyCountDownTimerViewStyle"
android:layout_marginLeft="1dp"/>
</LinearLayout>
<TextView
android:id="@+id/colon_hour"
android:layout_width="wrap_content"
android:layout_height="match_parent"
style="@style/SnapUpViewColon"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_snap_up"
android:padding="5dp"
>
<TextView
android:id="@+id/tv_min_decade"
style="@style/RushBuyCountDownTimerViewStyle"/>
<TextView
android:id="@+id/tv_min_unit"
style="@style/RushBuyCountDownTimerViewStyle"
android:layout_marginLeft="1dp"/>
</LinearLayout>
<TextView
android:id="@+id/colon_minute"
android:layout_width="wrap_content"
android:layout_height="match_parent"
style="@style/SnapUpViewColon"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_snap_up_red"
android:padding="5dp"
>
<TextView
android:id="@+id/tv_sec_decade"
style="@style/RushBuyCountDownTimerViewStyleRed"/>
<TextView
android:id="@+id/tv_sec_unit"
style="@style/RushBuyCountDownTimerViewStyleRed"
android:layout_marginLeft="1dp"/>
</LinearLayout>
</LinearLayout>
代码全部贴出来了。
Android定时器+倒计时 CountDownTimer实现
来源:http://blog.csdn.net/u010566681/article/details/54019761


猜你喜欢
- 只需要调用该类的一个方法createNewFile(),但是在实际操作中需要注意一些事项,如判断文件是否存在,以及如何向新建文件中写入数据等
- 不知道有人做没做过对日外包,如果做过的话,那么对vb.net应该非常熟悉了,当年我刚毕业的时候也做过四个月的外包,那种日子简直不是人过的,就
- 之前我写过直接用国内镜像的IP地址端口进行配置国内镜像的,如下链接:Android studio配置国内镜像源但是这种方法不一定在每台电脑上
- 本文实例为大家分享了PhotoView实现图片双击放大单击退出的具体代码,供大家参考,具体内容如下实现思路1.复制PhotoView&nbs
- 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成
- 在移动应用满天飞的时代,随着移动支付的盛行,很多应用中都集成了支付功能。之前的支付一直不是我负责,近期这个项目我负责订单模块少不了要做支付,
- 本文实例总结了Java编程实现生成给定范围内不重复随机数的方法。分享给大家供大家参考,具体如下:在Java中的Math类中存在一个rando
- 本文实例讲述了C#实现两接口中同名方法。分享给大家供大家参考。具体分析如下:对于一个类实现两个接口,而这两个接口又有同名方法,C#中的处理方
- Android 显示GIF图片实例详解gif图动画在Android中还是比较常用的,比如像新浪微博中,有很多gif图片,而且展示非常好,所以
- 请求SpringBoot接受前台参数的六种方式,首先因为从前台发送的请求没有界面的话只能是从地址栏发送并且只能是Get请求,为了测试其他的请
- 一、什么是递归方法调用自己的行为就是递归,递归必须要有终止条件,不然它会无限递归。1.先来看一下一个递归的例子此程序的Fact方法从大到小地
- 〇、正则表达式的基本语法符号若只简单匹配固定字符串,则无需任何修饰符,例如:需要匹配字符串 77,则可直接写:new Regex(
- 1)如何获得MediaPlayer实例:可以使用直接new的方式:MediaPlayer mp = new MediaPlayer();也可
- 本文实例讲述了java GUI编程之布局控制器(Layout)。分享给大家供大家参考,具体如下:布局控制器,是用来系统自动分配各个compo
- java 解决异常 2 字节的 UTF-8 序列的字节 2 无效的问题  
- 本文实例讲述了C#使用二分查找法判断指定字符的方法。分享给大家供大家参考,具体如下:private int sort_init(ref st
- spring boot是个好东西,可以不用容器直接在main方法中启动,而且无需配置文件,方便快速搭建环境。可是当我们要同时启动2个spri
- 基本语法C#,又名Csharp,天朝喜欢叫C井。C#是一种面向对象的编程语言。在面向对象的程序设计方法中,程序有各种相互交互的对象组成。相同
- 通过yml配置文件为静态成员变量赋值我们对springboot为普通成员变量的方式很熟悉,所以经常定式思维的认为静态属性的赋值和普通属性一样
- 前言大家都知道Android Studio目前已经更新到2.0 Preview 6了,作为Google大力推崇的开发工具,相对于Eclips