0

J'ai un minuteur dans chaque liste d'un Recyclerview pour lequel j'ai appliqué la pagination, tout cela fonctionne bien.Maintenant, par exemple un de mon compte à rebours est 02: 50 minutes Je suis allé à l'API et je l'ai tracé à la page 0 maintenant si je fais défiler vers d'autres pages dans Pagination et revenez à la page 0 dire dans 10 secondes de retour donc il est supposé être 02.39 ou 02:40 mais il reprend le ancienne valeur de l'API et le tracé 02:50 et commence à compter à nouveau ce qui est faux. Est-il possible de gérer le compte à rebours dans recyclerView avec Pagination.Pour garder le compte à rebours en cours d'exécution sans affecter dans Recyclerview lorsqu'il est appliqué Pagination

// In Activity Pagination and a Call API method based on PageNo 
     buyeroffer_rv.addOnScrollListener(new EndlessScrollListener(mLayoutManager) { 
      @Override 
      public void onLoadMore(int page, int totalItemsCount) { 

       System.out.print("NUMPAGE_SCROLLED"); 
       System.out.print("PAge No"+page); 


       progress_loader.setVisibility(View.VISIBLE); 
       if (Internet.isConnectedToInternet()) { 

        pageno=page; 
        callApi(page); 
       } 
       else 
       { 
        progress_loader.setVisibility(View.VISIBLE); 
        CustomToast.getInstance().toastMsg(activity,Internet.interNetMsg,ApiService.INTERNET_FLAG); 
       } 


      } 
     }); 

ensuite dans Recycle Voir Adaptateur Sur BindViewHolder La minuterie est extrait de l'API en quelques secondes et démarre le chronomètre en fonction de cette valeur.

// Adapter Code Snippet 
    @Override 
    public void onBindViewHolder(final MyViewHolder holder, final int position) { 

     // Timer current value is fetched from API for each Item 
     if (res.getTimeSloteOne() != null && !res.getTimeSloteOne().toString().isEmpty()) { 
      timer = res.getTimeSloteOne(); 
     } 

     // Here based on activeFlag hide or show Timer text 
     if ((activeflag.equalsIgnoreCase("0") && !res.getBidtype().toString().equalsIgnoreCase("5")) || (activeflag.equalsIgnoreCase("4")) && (res.getBidtype().toString().equalsIgnoreCase("0") || res.getBidtype().toString().equalsIgnoreCase("0.0"))) { 
      if (timer > 0) { 
       timer = timer * 1000; 
       holder.timer = new CountDownTimer(timer, 1000) { 
        public void onTick(long millisUntilFinished) { 

         holder.timmertext.setText("" + String.format(FORMAT, 
           TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(
             TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), 
           TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(
             TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)))); 

         if (TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)) >= 30) { 
          holder.timmertext.setTextColor(activity.getResources().getColor(R.color.timer1)); 
         } else if (TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)) >= 10 && 
           TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)) < 30) { 
          holder.timmertext.setTextColor(activity.getResources().getColor(R.color.timer2)); 
         } else if (TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)) < 10) { 
          holder.timmertext.setTextColor(activity.getResources().getColor(R.color.timer3)); 
         } 
        } 

        public void onFinish() { 
         holder.timmertext.setText("00:00"); 
         holder.timmertext.setTextColor(activity.getResources().getColor(R.color.color_black)); 
        } 
       }.start(); 
      } 
     } 
     else { 
      // 
      holder.timmertext.setVisibility(View.GONE); 
     } 
    } 
} 

// XML pour l'activité

<?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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context=".uibuyer.BuyerOfferAct" 
    android:background="#f0f0f0"> 
    <android.support.v4.widget.SwipeRefreshLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/swipe_layout" 
       android:background="#e6e6e6" 
       android:layout_marginBottom="50dp"> 
       <android.support.v7.widget.RecyclerView 
        android:id="@+id/recycler_view" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_marginBottom="50dp" 
        android:clipToPadding="false" 
        android:paddingBottom="10dp" /> 
      </android.support.v4.widget.SwipeRefreshLayout> 
      <RelativeLayout 
       android:id="@+id/loader_lay" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@color/loader_bg" 
       android:layout_below="@+id/toolbar_container" 
       android:layout_marginBottom="50dp" 
       android:visibility="gone"> 
       <include 
        android:id="@+id/loader" 
        layout="@layout/common_loader" 
        /> 
      </RelativeLayout> 
</RelativeLayout> 

adaptateur de la mise en page Snippet

  <com.xelpmoc.zommodity.utils.TextViewWithFont 
      android:id="@+id/time_lft_txt" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Time Left" 
      android:textAlignment="textEnd" 
      android:textAllCaps="true" 
      android:textColor="@color/texttwo" 
      android:textSize="@dimen/font8" 
      font:name="FONT_OPENSANS_BOLD" /> 

     <com.xelpmoc.zommodity.utils.TextViewWithFont 
      android:id="@+id/timmertext" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="03:50" 
      android:textAlignment="textEnd" 
      android:textAllCaps="true" 
      android:textSize="15dp" 
      font:name="FONT_OPENSANS_BOLD" /> 

     <com.xelpmoc.zommodity.utils.TextViewWithFont 
      android:id="@+id/time_mins" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="MINS" 
      android:textAlignment="viewEnd" 
      android:textAllCaps="true" 
      android:textColor="@color/text_one" 
      android:textSize="11dp" 
      font:name="FONT_OPENSANS_BOLDITALIC" /> 
+0

Je suppose que vous commencez les countdowntimers au mauvais endroit ..... s'il vous plaît fournir un code pertinent .. – Opiatefuchs

+0

countdowntimer n'est pas commun, son ensemble pour chaque article et pour chaque article que nous devrions chercher à partir de l'API, obtenir le contexte. Maintenant mis à jour mon extrait de code. –

Répondre

2

D'abord, nous devons créer une variable Boolen dans viewHolder et nous devons faire en tant que faux .

public class MyViewHolder extends RecyclerView.ViewHolder { 
       boolean timerflag=false; 
      CountDownTimer timer; 
        public MyViewHolder(View view) { 
         super(view); 
      } 
      } 

Et onBindViewHolder nous devons gérer le drapeau

@Override 
    public void onBindViewHolder(final MyViewHolder holder, final int position) { 
     if (holder.timerflag == false) { 

      if (holder.timer != null) { 
       holder.timer.cancel(); 
      } 


      if (timeslot != null) { 
       timer = timeslot; 
       holder.timerflag = true; 
      } 


      if (timer > 0) { 
       timer = timer * 1000; 
       holder.timer = new CountDownTimer(timer, 1000) { 
        public void onTick(long millisUntilFinished) { 

         //do the task 
        } 

        public void onFinish() { 

        } 
       }.start(); 
      } 
     } 
} 
+0

Timerflag J'ai essayé à l'intérieur du MyViewHolder et vérifié en fonction de cela, cela a fonctionné. Merci. –