안 드 로 이 드 특별 판매 목록 카운트다운 카드 문제 해결 방법
우선 이전 효과 도 를 살 펴 보 겠 습 니 다.
카드 가 걸 리 는 원인 을 말씀 드 리 겠 습 니 다.미 끄 러 질 때 adapter 의 getView 가 자주 생 성 되 고 소각 되면 카드 와 데이터 가 잘못 되 는 문제 가 발생 할 수 있 습 니 다.그러면 모든 item 의 카운트다운 은 따로 유지 해 야 합 니 다.여기 서 제 가 사용 하 는 Handler 와 timer 및 Timer Task 를 결합 하 는 방법 은 Timer Task 가 자신의 하위 스 레 드 에서 실행 되 는 것 을 알 고 있 습 니 다.그 다음 에 Timer 의 schedule()방법 으로 카운트다운 기능 을 실현 하고 마지막 으로 Hander 를 통 해 View 의 리 셋 을 실현 합 니 다.그 핵심 코드 는 다음 과 같 습 니 다.
public class CountDownView extends LinearLayout {
@BindView(R.id.tv_day)
TextView tvDay;
@BindView(R.id.tv_hour)
TextView tvHour;
@BindView(R.id.tv_minute)
TextView tvMinute;
@BindView(R.id.tv_second)
TextView tvSecond;
@BindView(R.id.day)
TextView day;
@BindView(R.id.hour)
TextView hour;
@BindView(R.id.minute)
TextView minute;
private Context context;
private int viewBg;//
private int cellBg;//
private int cellTextColor;//
private int textColor;// :
private int textSize = 14;//
private int cellTextSize = 12;//cell
private TimerTask timerTask = null;
private Timer timer = new Timer();
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
countDown();
}
};
public CountDownView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
this.context = context;
}
public CountDownView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
initAttrs(attrs, defStyleAttr);
initView(context);
}
private void initAttrs(AttributeSet attrs, int defStyle) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CountDownView, defStyle,0);
viewBg = typedArray.getColor(R.styleable.CountDownView_viewBg, Color.parseColor("#FFFFFF"));
cellBg = typedArray.getColor(R.styleable.CountDownView_cellBg, Color.parseColor("#F4F4F4"));
cellTextColor = typedArray.getColor(R.styleable.CountDownView_cellTextColor, Color.parseColor("#646464"));
textColor = typedArray.getColor(R.styleable.CountDownView_TextColor, Color.parseColor("#B3B3B3"));
textSize = (int) typedArray.getDimension(R.styleable.CountDownView_TextSize, UIUtils.dp2px(getContext(), 14));
cellTextSize = (int) typedArray.getDimension(R.styleable.CountDownView_cellTextSize, UIUtils.dp2px(getContext(), 12));
typedArray.recycle();
}
private void initView(Context context) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.layout_countdown_layout, this);
ButterKnife.bind(view);
initProperty();
}
private void initProperty() {
tvDay.setBackgroundColor(cellBg);
tvHour.setBackgroundColor(cellBg);
tvMinute.setBackgroundColor(cellBg);
tvSecond.setBackgroundColor(cellBg);
tvDay.setTextColor(cellTextColor);
tvHour.setTextColor(cellTextColor);
tvMinute.setTextColor(cellTextColor);
tvSecond.setTextColor(cellTextColor);
day.setTextColor(textColor);
hour.setTextColor(textColor);
minute.setTextColor(textColor);
}
public void setLeftTime(long leftTime) {
if (leftTime <= 0) return;
long time = leftTime / 1000;
long day = time / (3600 * 24);
long hours = (time - day * 3600 * 24) / 3600;
long minutes = (time - day * 3600 * 24 - hours * 3600) / 60;
long seconds = time - day * 3600 * 24 - hours * 3600 - minutes * 60;
setTextTime(time);
}
public void start() {
if (timerTask == null) {
timerTask = new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0);
}
};
timer.schedule(timerTask, 1000, 1000);
// timer.schedule(new TimerTask() {
// @Override
// public void run() {
// handler.sendEmptyMessage(0);
// }
// }, 0, 1000);
}
}
public void stop() {
if (timer != null) {
timer.cancel();
timer = null;
}
}
// , , , , 0
private void setTextTime(long time) {
String[] s = TimeUtils.formatTimer(time);
tvDay.setText(s[0]);
tvHour.setText(s[1]);
tvMinute.setText(s[2]);
tvSecond.setText(s[3]);
}
private void countDown() {
if (isCarry4Unit(tvSecond)) {
if (isCarry4Unit(tvMinute)) {
if (isCarry4Unit(tvHour)) {
if (isCarry4Unit(tvDay)) {
stop();
}
}
}
}
}
private boolean isCarry4Unit(TextView tv) {
int time = Integer.valueOf(tv.getText().toString());
time = time - 1;
if (time < 0) {
time = 59;
tv.setText(time + "");
return true;
} else if (time < 10) {
tv.setText("0" + time);
return false;
} else {
tv.setText(time + "");
return false;
}
}
}
다른 표기 법:
public class CountDownTimerView extends LinearLayout {
private TextView hourView, minuteView, secondView;
private LimitTimer mTimer;
private Handler handler;
public static final int START_LIMIT_TIME_MSG = 0x0111;
public static final int END_LIMIT_TIME_MSG = 0x0112;
private long endTime, leftTime;
private LimitTimeListener listener=null;
public CountDownTimerView(Context context) {
super(context);
initView(context);
}
public CountDownTimerView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
private void initView(Context context) {
setOrientation(HORIZONTAL);
setGravity(Gravity.CENTER_VERTICAL);
inflate(context, R.layout.layout_countdown_layout, this);
hourView = (TextView) findViewById(R.id.tv_hour);
minuteView = (TextView) findViewById(R.id.tv_minute);
secondView = (TextView) findViewById(R.id.tv_second);
}
public long getLeftTime() {
return leftTime;
}
//
public void initLeftTime(long endTime) {
endTime=endTime*1000;
if (null != mTimer) {
mTimer.cancel();
mTimer = null;
}
this.endTime = endTime;
mTimer = new LimitTimer(endTime, 1000);
mTimer.start();
if (handler != null) {
handler.sendEmptyMessage(START_LIMIT_TIME_MSG);
}
}
/**
* , , stop
*/
public void stopTimeCount() {
if (null != mTimer) {
mTimer.cancel();
mTimer = null;
}
}
public Handler getHandler() {
return handler;
}
public void setHandler(Handler handler) {
this.handler = handler;
}
private class LimitTimer extends CountDownTimer {
public LimitTimer(long millisInFuture, long countDownInterval) {
super(0 != leftTime ? leftTime : endTime, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
leftTime = millisUntilFinished;
long totalSecond = millisUntilFinished / 1000;
int second = (int) (totalSecond % 60);
int minute = (int) ((totalSecond / 60) % 60);
int hour = (int) ((totalSecond / 3600) % 24);
int day = (int) (totalSecond / (3600 * 24));
formatView(hourView, hour);
formatView(minuteView, minute);
formatView(secondView, second);
}
@Override
public void onFinish() {
String zero = "00";
hourView.setText(zero);
minuteView.setText(zero);
secondView.setText(zero);
if (null != handler) {
handler.sendEmptyMessage(END_LIMIT_TIME_MSG);
}
if (listener!=null){
listener.onTimeOver(true);
}
}
private void formatView(TextView view, int time) {
DecimalFormat df = new DecimalFormat("#00");
view.setText(df.format(time));
}
}
//
public void setOnLimitTimeListener(LimitTimeListener listener) {
this.listener = listener;
}
public interface LimitTimeListener {
void onTimeOver(boolean flag);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (handler != null) {
handler.removeMessages(START_LIMIT_TIME_MSG);
}
}
}
관련 된 레이아웃 파일
<?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:orientation="horizontal">
<TextView
android:id="@+id/tv_hour"
style="@style/style_countdown" />
<TextView
style="@style/style_wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_gravity="center_vertical"
android:text=" "
android:textColor="@color/color_646464" />
<TextView
android:id="@+id/tv_minute"
style="@style/style_countdown" />
<TextView
style="@style/style_wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_gravity="center_vertical"
android:text=" "
android:textColor="@color/color_646464" />
<TextView
android:id="@+id/tv_second"
style="@style/style_countdown" />
<TextView
style="@style/style_wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_gravity="center_vertical"
android:text=" "
android:textColor="@color/color_646464" />
</LinearLayout>
원본 주소 첨부:클릭 하여 링크 열기이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin의 기초 - 2부지난 글에서는 Kotlin이 무엇인지, Kotlin의 특징, Kotlin에서 변수 및 데이터 유형을 선언하는 방법과 같은 Kotlin의 기본 개념에 대해 배웠습니다. 유형 변환은 데이터 변수의 한 유형을 다른 데이터...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.