gpt4 book ai didi

java - Android RecyclerView CountDownTimer 删除并插入新项目后出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 09:12:21 26 4
gpt4 key购买 nike

我有带有适配器的 Rcylerview 每个项目需要 60 秒 60 秒后项目从列表中删除:这是我的适配器代码:

public class LafzAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>  {

private Activity activity;
public List<Lafz> Events;
public Typeface tempfont,tempfont2;
private MyClickListener myClickListener;
private final List<RecyclerView.ViewHolder> lstHolders=new ArrayList<>();

private Handler mHandler = new Handler();
private Runnable updateRemainingTimeRunnable = new Runnable() {
@Override
public void run() {
synchronized (lstHolders) {
for (RecyclerView.ViewHolder holder : lstHolders) {
((UserViewHolder) holder).updateTimeRemaining();
}
}
}
};

public LafzAdapter(List<Lafz> Events, Activity activity, Typeface ttemp) {
this.Events = Events;
this.activity = activity;
this.tempfont = ttemp;
}

public LafzAdapter(final List<Lafz> Events, Activity activity, Typeface ttemp, Typeface ttemp2) {
this.Events = Events;
this.activity = activity;
this.tempfont = ttemp;
this.tempfont2=ttemp2;
//lstHolders = new ArrayList<>();
startUpdateTimer();
}

@Override
protected void finalize() throws Throwable {
super.finalize();
mHandler.removeCallbacks(updateRemainingTimeRunnable);
}


private void startUpdateTimer() {
Timer tmr = new Timer();
tmr.schedule(new TimerTask() {
@Override
public void run() {
mHandler.post(updateRemainingTimeRunnable);
}
}, 1000, 1000);
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapterlayoutlafz, parent, false);
UserViewHolder dataObjectHolder = new UserViewHolder(view);
return dataObjectHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
((UserViewHolder) holder).setdata(Events.get(position));

synchronized (lstHolders) {
lstHolders.add(holder);
}
((UserViewHolder) holder).updateTimeRemaining();
}


class UserViewHolder extends RecyclerView.ViewHolder
implements View
.OnClickListener{
Lafz lafztemp;
public TextView titlelafz;
public TextView timerlafz;

public TextView idnumber;
public TextView idtype;
public TextView iduser;



public LinearLayout llcolor,llcolor2;

CountDownTimer timer;
CircularProgressBar circularProgressBar;



public UserViewHolder(View view) {
super(view);

titlelafz=(TextView)view.findViewById(R.id.titlelafz);
titlelafz.setTypeface(tempfont2);

timerlafz=(TextView)view.findViewById(R.id.timerlafz);
timerlafz.setTypeface(tempfont);


idnumber=(TextView)view.findViewById(R.id.idnumber);
idtype=(TextView)view.findViewById(R.id.idtype);
iduser=(TextView)view.findViewById(R.id.iduser);

circularProgressBar=(CircularProgressBar) view.findViewById(R.id.circularProgressBar);




llcolor = (LinearLayout) view.findViewById(R.id.llcolor);
llcolor2 = (LinearLayout) view.findViewById(R.id.llcolor2);


view.setOnClickListener(this);



}

public void setdata(Lafz l)
{
lafztemp = l;
if(lafztemp.typelafz==0)
{
this.idnumber.setText(String.valueOf(lafztemp.id));
this.idtype.setText(String.valueOf(lafztemp.typelafz));
this.iduser.setText(String.valueOf(lafztemp.iduser));
this.titlelafz.setText(lafztemp.geram+" "+"گرم"+" "+String.format("%,d",lafztemp.mazane)+" "+"تومان");
this.timerlafz.setText(String.valueOf(lafztemp.getTimer()/1000));
this.llcolor2.setBackgroundColor(ContextCompat.getColor(activity,R.color.frooshred));
}
// خرید
else {
this.idnumber.setText(String.valueOf(lafztemp.id));
this.idtype.setText(String.valueOf(lafztemp.typelafz));
this.iduser.setText(String.valueOf(lafztemp.iduser));
this.titlelafz.setText(lafztemp.geram+" "+"گرم"+" "+String.format("%,d", lafztemp.mazane)+" "+"تومان");
this.timerlafz.setText(String.valueOf(lafztemp.getTimer()/1000));
this.llcolor2.setBackgroundColor(ContextCompat.getColor(activity,R.color.kharidgreen));
}
}


public void updateTimeRemaining() {

lafztemp.timer-=1000;
this.timerlafz.setText(String.valueOf((lafztemp.getTimer()/1000)));
Iterator<Lafz> dataIterator = Events.iterator();
int y=0;
if(lafztemp.timer==0)
{
while (dataIterator.hasNext()) {
Lafz rd = dataIterator.next();
if (rd.equals(lafztemp)) {
lafztemp.timer=60000;

this.circularProgressBar.setProgress(60f);
dataIterator.remove();
notifyItemRemoved(y);
notifyDataSetChanged();
break;
}
y++;
}
}
this.circularProgressBar.setProgress(this.circularProgressBar.getProgress()-1f);
}


@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}

@Override
public int getItemCount() {
return Events == null ? 0 : Events.size();
}

public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public int getItemViewType(int position) {
return position;
}


public interface MyClickListener {
public void onItemClick(int position, View v);
}}

但是当旧项目从列表中删除并添加新项目时,新项目每秒下降两秒清晰的屏幕截图:请参阅这些屏幕截图以了解第一张 Gif 图像:第一次添加的项目没问题:

First Time Item Added

但是在四个项目过期并从列表中删除后:四个第一个项目每秒下降两秒,并且在四个第一个项目删除后所有其他项目计时器正在闪烁,请参阅第二个 Gif: After Old Item Delete And New Item Added

最佳答案

我在最近的一个项目中实现了相同的功能,我将 CountDownTimer 值存储在 SparseArray 中,并计算 Activity onDestroy 方法上的所有计时器,以及取消单击 recyclerview 项目上的按钮的单个计时器。请检查以下代码:

class DatingRoundAdapter(
var context: Context,
var arrayList: ArrayList<TempDatingModels>,
var clickListener: DatingListClickListener) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {

val USER_TYPE = 1
val WATING_TYPE = 2
private var countDownMap: SparseArray<CountDownTimer>? = null

init {
countDownMap = SparseArray()
}

public fun cancelAllTimers() {
if (countDownMap == null) {
return
}
Log.e("TAG", "size : " + countDownMap!!.size())
var i = 0
val length = countDownMap!!.size()
while (i < length) {
val cdt = countDownMap!!.get(countDownMap!!.keyAt(i))
cdt?.cancel()
i++
}
}

private fun millToMins(milliSec: Long): String {
return CommonMethods.displayTickTimerString(milliSec)
}

fun setData(arrayList: ArrayList<TempDatingModels>) {
this.arrayList.clear()
this.arrayList.addAll(arrayList)
notifyDataSetChanged()
//setTimer()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

var vh: RecyclerView.ViewHolder? = null
var v: View? = null
if (viewType == WATING_TYPE) {
v = LayoutInflater.from(parent.context)
.inflate(R.layout.item_dating_user_waiting, parent, false)
vh = WaitingHolder(v)
} else {
v = LayoutInflater.from(parent.context)
.inflate(R.layout.item_dating_user_dating, parent, false)
vh = UserHolder(v)
}
return vh
}

override fun getItemCount(): Int {
return arrayList.size
}

override fun getItemViewType(position: Int): Int {
if (arrayList.get(position).type == 1)
return USER_TYPE
else
return WATING_TYPE
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (holder.itemViewType == WATING_TYPE) {

} else {

if ((holder as UserHolder) != null)
(holder as UserHolder).timer?.cancel()

if (arrayList.get(position).status == 0) {
setNoButton((holder as UserHolder).itemView.btnYes)
setNoButton((holder as UserHolder).itemView.btnNo)
} else if (arrayList.get(position).status == 1) {
setYesButton((holder as UserHolder).itemView.btnYes)
setNoButton((holder as UserHolder).itemView.btnNo)
} else {
setYesButton((holder as UserHolder).itemView.btnNo)
setNoButton((holder as UserHolder).itemView.btnYes)
}

if (arrayList.get(position).isCompleted == 1) {
(holder as UserHolder).itemView.txtTimer.text = ""
(holder as UserHolder).timer?.cancel()
} else {
var rtime = arrayList.get(position).endTime - (System.currentTimeMillis() / 1000)

if (arrayList.get(position).endTime > (System.currentTimeMillis() / 1000)) {
(holder as UserHolder).timer =
object : CountDownTimer(rtime * 1000, Constant.CountDownInterval) {
override fun onFinish() {
if ((holder as UserHolder).timer != null) {
(holder as UserHolder).timer!!.cancel()
(holder as UserHolder).timer = null
}
arrayList.get(position).isCompleted = 1
arrayList.get(position).status = 1;

(context as LatestDatingRoundsActivity).runOnUiThread {
notifyDataSetChanged()
}
}

override fun onTick(millisUntilFinished: Long) {
(holder as UserHolder).itemView.txtTimer.text =
millToMins(millisUntilFinished)
}
}.start()

Log.e(
"key : ",
(holder as UserHolder).itemView.txtTimer.hashCode().toString() + " nn "
)

countDownMap!!.put(
(holder as UserHolder).itemView.txtTimer.hashCode(),
(holder as UserHolder).timer
);
}
}


(holder as UserHolder).itemView.btnYes.setOnClickListener({
clickListener.itemClickListener(position, arrayList.get(position), 1)
})

(holder as UserHolder).itemView.btnNo.setOnClickListener({

for (i in 0 until countDownMap!!.size()) {
val key = countDownMap!!.keyAt(i)

if (key.equals((holder as UserHolder).itemView.txtTimer.hashCode())) {
val cdt = countDownMap!!.get(key)
cdt!!.cancel()
}
}
//clickListener.itemClickListener(position, arrayList.get(position), 2)
})
}
}

private fun setYesButton(btnYes: Button) {
btnYes.background = context.resources.getDrawable(R.drawable.button_dialog_gredient)
btnYes.setTextColor(context.resources.getColor(R.color.white))
}

private fun setNoButton(btnYes: Button) {
btnYes.background = context.resources.getDrawable(R.drawable.button_dialog_white)
btnYes.setTextColor(context.resources.getColor(R.color.dark_grey))

}

public class UserHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

public var timer: CountDownTimer? = null

}

private class WaitingHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

}

}

取消所有计时器:

override fun onDestroy() {
super.onDestroy()
if (adapter != null) {
adapter!!.cancelAllTimers()
}
}

或取消recyclerview特定项目的计时器

for (i in 0 until countDownMap!!.size()) {
val key = countDownMap!!.keyAt(i)

if (key.equals((holder as UserHolder).itemView.txtTimer.hashCode())) {
val cdt = countDownMap!!.get(key)
cdt!!.cancel()
}
}

关于java - Android RecyclerView CountDownTimer 删除并插入新项目后出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59539326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com