我有一个带有倒数计时器的 Recyclerview,它以天、小时、分钟和秒的形式显示剩余的游戏时间。现在我遇到了问题,当我离开活动时,倒计时计时器仍在运行。 这是我的适配器:
Public class RvAdapterShowVsTeamChallenges extends RecyclerView.Adapter<RvAdapterShowVsTeamChallenges.ViewHolderClass> {
private Boolean boFirstTime = true;
private int [] ayColors = {R.drawable.circle_red,R.drawable.circle_purple,R.drawable.circle_orange,R.drawable.circle_blue,R.drawable.circle_tile};
private Context context;
private CountDownTimer countDownTimerTest;
private DatabaseWrite databaseWrite;
private SharedPrefs sharedPrefs;
public RvAdapterShowVsTeamChallenges(final Context context)
{
this.context = context;
}
public class ViewHolderClass extends RecyclerView.ViewHolder{
private TextView txtChallenger;
private TextView txtOponnent;
private TextView txtChallengerScore;
private TextView txtOponnentScore;
private TextView txtTime;
private TextView txtChallengerLetters;
private TextView txtOponnentLetters;
private ProgressBar prgChallenger;
private ProgressBar prgOponnent;
public ViewHolderClass(View itemView) {
super(itemView);
sharedPrefs = new SharedPrefs(context);
txtChallenger = (TextView) itemView.findViewById(R.id.txtChallenger);
txtOponnent = (TextView) itemView.findViewById(R.id.txtOponnent);
txtChallengerScore = (TextView) itemView.findViewById(R.id.txtChallengerScore);
txtOponnentScore = (TextView) itemView.findViewById(R.id.txtOponnentScore);
txtChallengerLetters = (TextView)itemView.findViewById(R.id.txtChallengerLetter);
txtOponnentLetters = (TextView)itemView.findViewById(R.id.imgOponnent);
txtTime = (TextView) itemView.findViewById(R.id.txtTime);
prgChallenger = (ProgressBar) itemView.findViewById(R.id.prgTimeLeft);
prgOponnent = (ProgressBar) itemView.findViewById(R.id.prgOponnent);
}
}
@Override
public ViewHolderClass onCreateViewHolder(final ViewGroup parent, final int viewType) {
View itemview1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_show_vs_team_challenges,null);
if(boFirstTime)
{
for(int i=0; i<ShowVsTeam.ayListTimeLeftChallenge.size(); i++)
{
long substractFalseValue = ((ShowVsTeam.ayListTimeLeftChallenge.get(i)-1209600033)/1000);
long total = ((substractFalseValue + 1209600)*1000);
long difference = (total - System.currentTimeMillis());
ShowVsTeam.ayListTimeLeftChallenge.set(i, difference);
}
boFirstTime = false;
}
countDownTimerTest = new CountDownTimer(1800000,1000) {
@Override
public void onTick(long millisUntilFinished) {
for(int i=0; i<ShowVsTeam.ayListTimeLeftChallenge.size(); i++)
{
long lngUntilNow = ShowVsTeam.ayListTimeLeftChallenge.get(i);
long lngNow = (lngUntilNow-(1000/ShowVsTeam.ayListTimeLeftChallenge.size()));
ShowVsTeam.ayListTimeLeftChallenge.set(i, Long.valueOf(lngNow));
}
notifyDataSetChanged();
}
@Override
public void onFinish() {
}
}.start();
return new ViewHolderClass(itemview1);
}
@Override
public void onBindViewHolder(final ViewHolderClass holder, final int position) {
holder.txtChallenger.setText(sharedPrefs.getTeamnameServer(context));
holder.txtOponnent.setText(ShowVsTeam.aylistOponnentTeamsChallenge.get(position));
holder.txtChallengerScore.setText(ShowVsTeam.ayListScoreChallengerChallenge.get(position));
holder.txtOponnentScore.setText(ShowVsTeam.ayListScoreOppnnentChallenge.get(position));
String strLetterChallenger = sharedPrefs.getTeamnameServer(context).substring(0,1);
String strLetterOpponnent = ShowVsTeam.aylistOponnentTeamsChallenge.get(position).substring(0,1);
holder.txtChallengerLetters.setText(strLetterChallenger);
holder.txtOponnentLetters.setText(strLetterOpponnent);
holder.txtOponnentLetters.setBackgroundResource(ayColors[position]);
holder.prgChallenger.setProgress(Integer.parseInt(ShowVsTeam.ayListScoreChallengerChallenge.get(position)));
holder.prgOponnent.setProgress(Integer.parseInt(ShowVsTeam.ayListScoreOppnnentChallenge.get(position)));
long seconds = TimeUnit.MILLISECONDS.toSeconds(ShowVsTeam.ayListTimeLeftChallenge.get(position));
int intDay = (int)TimeUnit.SECONDS.toDays(seconds);
long lngHours = TimeUnit.SECONDS.toHours(seconds) - (intDay *24);
long lngMinutes = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
long lngSeconds = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);
holder.txtTime.setText(String.valueOf(intDay)+"d:"+String.valueOf(lngHours)+"h:"+String.valueOf(lngMinutes)+"m:"+String.valueOf(lngSeconds)+"s");
}
@Override
public int getItemCount() {
return ShowVsTeam.aylistOponnentTeamsChallenge.size();
}
}
我尝试取消 Activity 中的 CountDownTimer onStop(),但我得到一个空指针,尽管倒数计时器仍在运行。
在适配器中
public void finishTimer(){
if(countDownTimerTest!=null){
countDownTimerTest.cancel();
}
}
活动中
@Override
protected void onStop() {
if(rvAdapterShowVsTeamChallenges!=null){
rvAdapterShowVsTeamChallenges.finishTimer();
}
super.onStop();
}
}
你能帮我吗?
更新
适配器
public class RvAdapterShowVsTeamChallenges extends RecyclerView.Adapter<RvAdapterShowVsTeamChallenges.ViewHolderClass> {
private Boolean boFirstTime = true;
private int [] ayColors = {R.drawable.circle_red,R.drawable.circle_purple,R.drawable.circle_orange,R.drawable.circle_blue,R.drawable.circle_tile};
private Context context;
private DatabaseWrite databaseWrite;
private SharedPrefs sharedPrefs;
private CountDownTimerTest countDownTimerTest;
public RvAdapterShowVsTeamChallenges(final Context context)
{
this.context = context;
}
public class ViewHolderClass extends RecyclerView.ViewHolder{
private TextView txtChallenger;
private TextView txtOponnent;
private TextView txtChallengerScore;
private TextView txtOponnentScore;
private TextView txtTime;
private TextView txtChallengerLetters;
private TextView txtOponnentLetters;
private ProgressBar prgChallenger;
private ProgressBar prgOponnent;
public ViewHolderClass(View itemView) {
super(itemView);
sharedPrefs = new SharedPrefs(context);
txtChallenger = (TextView) itemView.findViewById(R.id.txtChallenger);
txtOponnent = (TextView) itemView.findViewById(R.id.txtOponnent);
txtChallengerScore = (TextView) itemView.findViewById(R.id.txtChallengerScore);
txtOponnentScore = (TextView) itemView.findViewById(R.id.txtOponnentScore);
txtChallengerLetters = (TextView)itemView.findViewById(R.id.txtChallengerLetter);
txtOponnentLetters = (TextView)itemView.findViewById(R.id.imgOponnent);
txtTime = (TextView) itemView.findViewById(R.id.txtTime);
prgChallenger = (ProgressBar) itemView.findViewById(R.id.prgTimeLeft);
prgOponnent = (ProgressBar) itemView.findViewById(R.id.prgOponnent);
}
}
@Override
public ViewHolderClass onCreateViewHolder(final ViewGroup parent, final int viewType) {
View itemview1 = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_show_vs_team_challenges,null);
if(boFirstTime)
{
for(int i=0; i<ShowVsTeam.ayListTimeLeftChallenge.size(); i++)
{
long substractFalseValue = ((ShowVsTeam.ayListTimeLeftChallenge.get(i)-1209600033)/1000);
long total = ((substractFalseValue + 1209600)*1000);
long difference = (total - System.currentTimeMillis());
ShowVsTeam.ayListTimeLeftChallenge.set(i, difference);
}
boFirstTime = false;
}
return new ViewHolderClass(itemview1);
}
@Override
public void onBindViewHolder(final ViewHolderClass holder, final int position) {
holder.txtChallenger.setText(sharedPrefs.getTeamnameServer(context));
holder.txtOponnent.setText(ShowVsTeam.aylistOponnentTeamsChallenge.get(position));
holder.txtChallengerScore.setText(ShowVsTeam.ayListScoreChallengerChallenge.get(position));
holder.txtOponnentScore.setText(ShowVsTeam.ayListScoreOppnnentChallenge.get(position));
String strLetterChallenger = sharedPrefs.getTeamnameServer(context).substring(0,1);
String strLetterOpponnent = ShowVsTeam.aylistOponnentTeamsChallenge.get(position).substring(0,1);
holder.txtChallengerLetters.setText(strLetterChallenger);
holder.txtOponnentLetters.setText(strLetterOpponnent);
holder.txtOponnentLetters.setBackgroundResource(ayColors[position]);
holder.prgChallenger.setProgress(Integer.parseInt(ShowVsTeam.ayListScoreChallengerChallenge.get(position)));
holder.prgOponnent.setProgress(Integer.parseInt(ShowVsTeam.ayListScoreOppnnentChallenge.get(position)));
long seconds = TimeUnit.MILLISECONDS.toSeconds(ShowVsTeam.ayListTimeLeftChallenge.get(position));
int intDay = (int)TimeUnit.SECONDS.toDays(seconds);
long lngHours = TimeUnit.SECONDS.toHours(seconds) - (intDay *24);
long lngMinutes = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds)* 60);
long lngSeconds = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) *60);
holder.txtTime.setText(String.valueOf(intDay)+"d:"+String.valueOf(lngHours)+"h:"+String.valueOf(lngMinutes)+"m:"+String.valueOf(lngSeconds)+"s");
}
@Override
public int getItemCount() {
return ShowVsTeam.aylistOponnentTeamsChallenge.size();
}
private class CountDownTimerTest extends CountDownTimer {
private CountDownTimerTest(long millis, long interval) {
super(millis, interval);
}
@Override
public void onTick(long millisUntilFinished) {
for(int i=0; i<ShowVsTeam.ayListTimeLeftChallenge.size(); i++)
{
long lngUntilNow = ShowVsTeam.ayListTimeLeftChallenge.get(i);
long lngNow = (lngUntilNow-(1000/ShowVsTeam.ayListTimeLeftChallenge.size()));
ShowVsTeam.ayListTimeLeftChallenge.set(i, Long.valueOf(lngNow));
}
notifyDataSetChanged();
}
@Override
public void onFinish() {
}
}
public void startTimer(){
if(countDownTimerTest==null){
countDownTimerTest= new CountDownTimerTest(1800000,1000);
}
countDownTimerTest.start();
}
public void cancelTimer(){
countDownTimerTest.cancel();
}
}
活动
public class ShowVsTeam extends AppCompatActivity {
public int intTextBo = 0;
private String strMethod;
static ArrayList<String> ayListChallengerTeamsRequest;
static ArrayList<String> ayListChallengerPlayedRequest;
static ArrayList<String> ayListChallengerNumberOfMembersRequest;
static ArrayList<String> ayListChallengerAcceptedRequest;
static ArrayList<String> ayListOponnentTeamsRequest;
static ArrayList<String> ayListOponnnetPlayedRequest;
static ArrayList<String> ayListOponnentNumberOfMembersRequest;
static ArrayList<String> ayListOnlyChallengerTeamsRequest;
static ArrayList<String> ayListOnlyAcceptedRequest;
static ArrayList<String> ayListOnlyRequiredRequest;
static ArrayList<String> aylistOponnentTeamsChallenge;
static ArrayList<String> ayListScoreChallengerChallenge;
static ArrayList<String> ayListScoreOppnnentChallenge;
static ArrayList<Long> ayListTimeLeftRequestChallenger;
static ArrayList<Long> ayListTimeLeftRequestOponnent;
static ArrayList<Long> ayListTimeLeftChallenge;
private TextView txtToolbarTitle;
static TextView txtNo;
private Toolbar toolbar;
private RecyclerView recyclerViewRequests;
private RecyclerView.Adapter rvAdapterRequests;
private RecyclerView.LayoutManager rvLayoutManagerRequests;
private RecyclerView recyclerViewChallenges;
private RecyclerView.Adapter rvAdapterChallenges;
private RecyclerView.LayoutManager rvLayoutManagerChallenges;
private RvAdapterShowVsTeamChallenges rvAdapterShowVsTeamChallenges;
private SharedPrefs sharedPrefs;
private DatabaseWrite databaseWrite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.show_vs_team);
initialize();
declare();
navigation();
getVS();
}
private void initialize() {
txtNo = (TextView)findViewById(R.id.txtNo);
toolbar = (Toolbar)findViewById(R.id.toolbar);
txtToolbarTitle = (TextView)toolbar.findViewById(R.id.txtToolbarTitle);
recyclerViewRequests = (RecyclerView)findViewById(R.id.recyclerShowVsTeamRequests);
rvLayoutManagerRequests = new LinearLayoutManager(this);
rvAdapterRequests = new RvAdapterShowVsTeamRequests(this);
recyclerViewChallenges = (RecyclerView)findViewById(R.id.recyclerShowVsTeamChallenges);
rvLayoutManagerChallenges = new LinearLayoutManager(this);
rvAdapterChallenges = new RvAdapterShowVsTeamChallenges(this);
rvAdapterShowVsTeamChallenges = new RvAdapterShowVsTeamChallenges(this);
sharedPrefs = new SharedPrefs(this);
}
private void declare() {
recyclerViewRequests.setLayoutManager(rvLayoutManagerRequests);
recyclerViewChallenges.setLayoutManager(rvLayoutManagerChallenges);
}
private void navigation() {
txtToolbarTitle.setText("BeforeTeam");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
private void getVS()
{
strMethod = "showVsTeamRequests";
databaseWrite = new DatabaseWrite(this);
databaseWrite.passTheClassShowVsTeam(this);
databaseWrite.execute(strMethod,sharedPrefs.getTeamnameServer(this));
strMethod = "showVsTeamChallenges";
databaseWrite = new DatabaseWrite(this);
databaseWrite.passTheClassShowVsTeam(this);
databaseWrite.execute(strMethod,sharedPrefs.getTeamnameServer(this));
}
public void showVsTeamRequests()
{
recyclerViewRequests.setAdapter(rvAdapterRequests);
}
public void showVsteamChallenges()
{
recyclerViewChallenges.setAdapter(rvAdapterChallenges);
rvAdapterShowVsTeamChallenges.startTimer();
}
@Override
public void onBackPressed() {
super.onBackPressed();
rvAdapterShowVsTeamChallenges.cancelTimer();
}
@Override
protected void onStop() {
super.onStop();
rvAdapterShowVsTeamChallenges.cancelTimer();
}
@Override
protected void onPause() {
super.onPause();
rvAdapterShowVsTeamChallenges.cancelTimer();
}
}
我实际上使用的是 onViewDetachedFromWindow 和 onViewAttachedFromWindow 函数。然而,当活动销毁时,我看到 onViewRecycled 函数工作了一次。这些代码非常适合我:
将 onViewRecycled 添加到您的 RecycleViewAdapter
@Override
public void onViewRecycled(@NonNull ViewHolder holder) {
super.onViewRecycled(holder);
//add cancel function
if (holder.timer != null) {
holder.timer.cancel();
}
}
之后,将 onDestroy 添加到您的 Activity
@Override
protected void onDestroy() {
super.onDestroy();
//set adapter
recylerView.setAdapter(null);
}
在您的 RvAdapterShowVsTeamChallenges 类中,您有一个名为“countDownTimerTest”的成员,但它只能保存对 CountDownTimer 的一个对象的引用。 我认为您遇到的问题是您尝试在 RecyclerView 中显示多个项目,并且每次调用 onCreateViewHolder() 时都会覆盖“countDownTimerTest”的引用。
我认为防止这种情况的最佳解决方案是使用
Map<Integer, CountDownTimer> timerMap = new HashMap<Integer, CountDownTimer>();
来存储所有 CountDownTimers。然后在 onBindViewHolder() 中检查地图是否包含该位置的项目,如果不包含 - 然后创建新的倒计时器
CountDownTimer timer = timerMap.get(position);
if(timer == null){
timer = new CountDownTimer(1800000,1000) {
@Override
public void onTick(long millisUntilFinished) {
for(int i=0; i<ShowVsTeam.ayListTimeLeftChallenge.size(); i++){
long lngUntilNow = ShowVsTeam.ayListTimeLeftChallenge.get(i);
long lngNow = (lngUntilNow-(1000/ShowVsTeam.ayListTimeLeftChallenge.size()));
ShowVsTeam.ayListTimeLeftChallenge.set(i, Long.valueOf(lngNow));
}
notifyDataSetChanged();
}
@Override
public void onFinish() {
}
}
timer.start();
timerMap.put(position, timer);
}
完成 Activity 后,只需调用一个方法即可执行此操作:
for(CountDownTimer timer : timerMap.values()){
timer.cancel();
}
在适配器中创建一个公共方法,例如:
public void finishTimer(){
if(countDownTimerTest!=null){
countDownTimerTest.cancel();
countDownTimer = null;
}
}
如果您完成了活动,则从您的活动中调用它:
@Override
protected void onStop() {
if(mYourRecyclerViewAdapter!=null){
mYourRecyclerViewAdapter.finishTimer();
}
super.onStop();
}
您应该将其作为一个类,而不是在视图持有者中创建 CountDownTimer:
private class CountDownTimerTest extends CountDownTimer {
private CountDownTimerTest(long millis, long interval) {
super(millis, interval);
}
@Override
public void onTick(long millisUntilFinished) {
for(int i=0; i<ShowVsTeam.ayListTimeLeftChallenge.size(); i++)
{
long lngUntilNow = ShowVsTeam.ayListTimeLeftChallenge.get(i);
long lngNow = (lngUntilNow-(1000/ShowVsTeam.ayListTimeLeftChallenge.size()));
ShowVsTeam.ayListTimeLeftChallenge.set(i, Long.valueOf(lngNow));
}
notifyDataSetChanged();
}
@Override
public void onFinish() {
}
}
然后你可以在你的适配器中创建一个全局对象:
private CountDownTimerTest countDownTimerTest;
以及适配器中的公共启动方法(如果您需要在外部启动它):
public void startTimer(){
if(countDownTimerTest==null){
countDownTimerTest= new CountDownTimerTest(1800000,1000);
}
countDownTimerTest.start();
}
如果您仍想在视窗中启动它,只需在内部调用
startTimer()
即可。如果您想在活动中启动它,请致电 mYourAdapter.startTimer();
。然后你可以按照上面的建议停止它。
从您的评论中我了解到,每次您开始活动时,都会创建一个新的计时器。如果你这样做,通常问题就会消失。否则,您可以通过将清单中的
android:launchMode="singleTop"
设置到活动标记中来避免创建活动的新实例。通过这样做,您的活动只有在之前被销毁时才会被创建。
在
CountDownTimer
中设置 SparseArray
并取消 Activity onDestroy
方法中的所有计时器,
请检查此链接,它对我的项目有帮助
正如 @Atakan Yildirim 指出的那样,当你为你的
onViewRecycled()
制作 adapter = null
时,肯定会调用 recyclerview
所以你的代码将是这样的
override fun onStop() {
super.onStop()
currentItemPosition =
(binding.recyclerView.layoutManager as? LinearLayoutManager)
?.findFirstVisibleItemPosition() ?: 0
binding.recyclerView.adapter = null
}
override fun onStart() {
super.onStart()
binding.recyclerView.adapter = myCustomAdapter
binding.recyclerView.scrollToPosition(currentItemPosition)
}
然后确保正确实施
onViewRecycled()
以释放资源。