生活随笔
收集整理的這篇文章主要介紹了
仿拼多多评团列表倒计时循环滚动 Android
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
仿拼多多評團列表倒計時循環滾動,每段時間自動滾動?Android?
?
?
現在開始動手擼代碼
?
package com.hugh.basis.rvlooper;import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;import java.lang.ref.WeakReference;/*** Created by chenyw on 2019-08-15.*/
public class AutoRollRecyclerView extends RecyclerView {private static final long TIME_AUTO_POLL = 3000;AutoPollTask autoPollTask;private boolean running; //表示是否正在自動輪詢private boolean canRun;//表示是否可以自動輪詢public AutoRollRecyclerView(Context context, @Nullable AttributeSet attrs) {super(context, attrs);autoPollTask = new AutoPollTask(this);}static class AutoPollTask implements Runnable {private int i = 3;private final WeakReference<AutoRollRecyclerView> mReference;//使用弱引用持有外部類引用->防止內存泄漏public AutoPollTask(AutoRollRecyclerView reference) {this.mReference = new WeakReference<AutoRollRecyclerView>(reference);}@Overridepublic void run() {AutoRollRecyclerView recyclerView = mReference.get();if (recyclerView != null && recyclerView.running && recyclerView.canRun) {recyclerView.smoothScrollToPosition(i);i += 2;recyclerView.postDelayed(recyclerView.autoPollTask, TIME_AUTO_POLL);}}}//開啟:如果正在運行,先停止->再開啟public void start() {if (running)stop();canRun = true;running = true;postDelayed(autoPollTask, TIME_AUTO_POLL);}public void stop() {running = false;removeCallbacks(autoPollTask);}@Overridepublic boolean onTouchEvent(MotionEvent e) {switch (e.getAction()) {case MotionEvent.ACTION_DOWN:case MotionEvent.ACTION_UP:case MotionEvent.ACTION_CANCEL:case MotionEvent.ACTION_OUTSIDE:break;}return false;}
}
?
package com.hugh.basis.rvlooper;import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;import com.hugh.basis.R;
import com.hugh.basis.rvlooper.bean.GroupBookingEntity;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;/*** Created by chenyw on 2019-08-15.*/
public class AutoRollAdapter extends RecyclerView.Adapter<AutoRollAdapter.BaseViewHolder> {HashMap<String, CountDownTimer> timerMap = new HashMap<>();ArrayList<String> timerKeyList = new ArrayList<>();private List<GroupBookingEntity> mData;public AutoRollAdapter(List<GroupBookingEntity> list) {this.mData = list;}@Overridepublic BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_group_booking_vh, parent, false);BaseViewHolder holder = new BaseViewHolder(view);return holder;}@Overridepublic void onBindViewHolder(final BaseViewHolder holder, final int position) {final GroupBookingEntity data = mData.get(position % mData.size());holder.mTvName.setText(data.nickname);String allPeople = data.collage_people;String nowPeople = data.now_people;int leavePeople = Integer.valueOf(allPeople) - Integer.valueOf(nowPeople);long currenTime = Long.parseLong(data.current_time);long endTime = Long.parseLong(data.end_time);long seconds = endTime - currenTime;final String mCollageId = data.id;holder.mLayoutGoGourp.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//去參團}});SpannableString spannableString = new SpannableString("還差" + leavePeople + "人成團");spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFD4747")), 2, spannableString.length() - 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);holder.mTvleaveMembers.setText(spannableString);String id = data.id;if (!timerKeyList.contains(id)) {timerKeyList.add(id);}// -----一開始初始化數據if (holder.countDownTimer != null) {holder.countDownTimer.cancel();}holder.countDownTimer = new CountDownTimer(seconds * 1000, 1000) {public void onTick(long l) {holder.mTvCountDown.setText(getTimeStr(l));}public void onFinish() {//倒計時結束
// holder.timeTv.setText("00:00");}}.start();timerMap.put(id, holder.countDownTimer);}private String getTimeStr(long l) {long day = l / (1000 * 24 * 60 * 60); //單位天long hour = (l - day * (1000 * 24 * 60 * 60)) / (1000 * 60 * 60); //單位時long minute = (l - day * (1000 * 24 * 60 * 60) - hour * (1000 * 60 * 60)) / (1000 * 60); //單位分long second = (l - day * (1000 * 24 * 60 * 60) - hour * (1000 * 60 * 60) - minute * (1000 * 60)) / 1000;//單位秒String hourStr = String.valueOf(hour);if (hourStr.length() == 1) {hourStr = "0" + hourStr;}String minStr = String.valueOf(minute);if (minStr.length() == 1) {minStr = "0" + minStr;}String secondStr = String.valueOf(second);if (secondStr.length() == 1) {secondStr = "0" + secondStr;}//如果day為0的時候天不顯示if (day == 0) {return hourStr + ":" + minStr + ":" + secondStr;} else {return day + "天" + " " + hourStr + ":" + minStr + ":" + secondStr;}}@Overridepublic int getItemCount() {return Integer.MAX_VALUE;}public void onDestroy() {for (int i = 0; i < timerKeyList.size(); i++) {if (timerKeyList.get(i) != null) {if (timerMap.get(timerKeyList.get(i)) != null) {CountDownTimer timer = timerMap.get(timerKeyList.get(i));if (timer != null) {timer.cancel();}}}}}class BaseViewHolder extends RecyclerView.ViewHolder {private TextView mTvCountDown;private ImageView mIvAvator;private TextView mTvName;private TextView mTvleaveMembers;private LinearLayout mLayoutGoGourp;private CountDownTimer countDownTimer;public BaseViewHolder(View view) {super(view);mIvAvator = view.findViewById(R.id.iv_avator);mTvName = view.findViewById(R.id.tv_name);mTvleaveMembers = view.findViewById(R.id.tv_leave_book);mTvCountDown = view.findViewById(R.id.tv_count_down);mLayoutGoGourp = view.findViewById(R.id.layout_go_group);}}}
activity界面
?
package com.hugh.basis.rvlooper;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;import com.hugh.basis.R;
import com.hugh.basis.rvlooper.bean.GroupBookingEntity;import java.util.ArrayList;
import java.util.List;/*** Created by chenyw on 2019-08-15.*/
public class LooperActivity extends AppCompatActivity {private AutoRollRecyclerView mRecyclerView;private List<GroupBookingEntity> list = new ArrayList<>();private AutoRollAdapter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_looper_rv);initData();initView();}private void initData() {for (int i = 0; i < 5; i++) {GroupBookingEntity entity = new GroupBookingEntity();entity.nickname = "我是一個游客"+i;entity.collage_people = "10";entity.now_people = i+"";entity.end_time = "86400";entity.current_time =(100+i*30)+"";entity.id="id"+i;list.add(entity);}}private void initView() {mRecyclerView = (AutoRollRecyclerView) findViewById(R.id.rv_recycleView);mAdapter = new AutoRollAdapter(list);mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));mRecyclerView.setAdapter(mAdapter);LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);mRecyclerView.setLayoutManager(linearLayoutManager);mRecyclerView.start();}@Overrideprotected void onDestroy() {super.onDestroy();if (null != mRecyclerView) {mRecyclerView.stop();}if(mAdapter!=null){mAdapter.onDestroy();}}
}
?
package com.hugh.basis.rvlooper.bean;import java.io.Serializable;/*** Created by chenyw on 2019-08-16.*/
public class GroupBookingEntity implements Serializable {public String id;public String now_people;public String end_time;public String collage_people;public String nickname;public String current_time;
}
?
以上就是完整的代碼,要是還是有問題的話,可以參考下面的源碼鏈接
源碼傳送門
總結
以上是生活随笔為你收集整理的仿拼多多评团列表倒计时循环滚动 Android的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。