2016-05-07 16 views
0

Ich muss nur ein Countdown-Fragment bei bestimmten Triggern anzeigen, das einen Fortschrittsbalken enthält, der die Anzahl der Ticks eines Countdown-Timers reduziert.Android: Fortschrittsbalken über CountDownTimer aktualisieren

Ich bin mit folgenden einfachen Schritten:

  1. Anruf startTimer() in onCreateView des Fragments.
  2. In startTimer() definieren Sie eine CountDownTimer Klasse, wo onTick Methode Fortschritt einer ProgressBar reduziert und onFinish zeigt eine Toast-Nachricht.
  3. Starten dieses Timers auf UI-Thread getActivity().runOnUIThread()

Problem verwendet wird, um den Zeitgeber ausführt aber ProgressBar hält unbestimmten Fortschritt zeigt das heißt es bleibt unverändert. Toast ist auch im Ziel zu sehen, aber nichts ändert sich für den Fortschrittsbalken.

Was läuft hier falsch?

Im Folgenden finden Sie den aktuellen Code:

public class IncomingRequestFragment extends Fragment { 

    private OnFragmentInteractionListener mListener; 
    private FrameLayout root; 
    private CountDownTimer mCountDownTimer; 
    private ProgressBar mProgressBar; 

    public IncomingRequestFragment() { 
     // Required empty public constructor 
    } 


    public static IncomingRequestFragment newInstance(String param1, String param2) { 
     IncomingRequestFragment fragment = new IncomingRequestFragment(); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

    } 
    int progress=25; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     root = (FrameLayout) inflater.inflate(R.layout.fragment_incoming_request, container, false); 
     mProgressBar = (ProgressBar) root.findViewById(R.id.countDown); 
     progress = mProgressBar.getMax(); 
     mProgressBar.setIndeterminate(false); 
     mProgressBar.setProgress(progress); 
     startTimer(); 
     return root; 
    } 

    private void startTimer() { 
     mCountDownTimer = new CountDownTimer(25000,1000) { 
      @Override 
      public void onTick(long millisUntilFinished) { 
       mProgressBar.setProgress(progress--); 
       Log.d("count",String.valueOf(progress)); 
      } 

      @Override 
      public void onFinish() { 
       Toast.makeText(getContext(),"You just missed a trip!",Toast.LENGTH_SHORT).show(); 
      } 
     }; 
     getActivity().runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       mCountDownTimer.start(); 
      } 
     }); 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 


    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 
} 

Layout-XML:

<FrameLayout 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" 
    tools:context="com.taxiwaxidriver.ui.fragments.IncomingRequestFragment"> 

    <ProgressBar 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/countDown" 
     android:layout_margin="60dp" 
     android:max="25" 
     android:progressTint="@android:color/holo_blue_dark" 
     android:progressBackgroundTint="@android:color/holo_blue_dark" 
     android:progress="25"/> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:padding="12dp" 
     android:weightSum="2" 
     android:orientation="horizontal"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:textColor="@color/white" 
      android:text="REJECT" 
      android:id="@+id/rideLater" 
      android:background="@drawable/rounded_button_left"/> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="ACCEPT" 
      android:textColor="@color/white" 
      android:background="@drawable/rounded_button_right" 
      android:id="@+id/rideNow"/> 

    </LinearLayout> 

</FrameLayout> 

Ausgabe des obigen Codes ist:

05-07 10:49:05.623 21455-21455 D/count: 24 
05-07 10:49:06.626 21455-21455 D/count: 23 
05-07 10:49:07.636 21455-21455 D/count: 22 
05-07 10:49:08.653 21455-21455 D/count: 21 
. 
. 
. 
05-07 10:49:28.917 21455-21455 D/count: 1<br> 
+0

ein Layout XML-Datei erstellen, wo Ihre 'R.id.countDown' – pskink

+0

definiert ist, ich habe es jetzt –

+0

add hinzugefügt' style = ""? android: attr/progressBarStyleHorizontal "oder ähnlich in Ihrem' 'tag – pskink

Antwort

0

Fehler war m Issing der Stil-Attribut für ProgressBar in XML
definiert

style=""?android:attr/progressBarStyleHorizontal" 

Dank pskink Kommentar

Verwandte Themen