Android:通过CountDownTimer更新进度条

时间:2016-05-07 05:43:07

标签: android android-fragments progress-bar

我只是在某个触发器上显示一个倒计时片段,其中包括一个进度条,可以减少倒数计时器的滴答计数。

我使用以下简单步骤:

  1. startTimer()片段中调用onCreateView
  2. startTimer()中定义一个CountDownTimer类,其中onTick方法可以减少ProgressBar的进度,onFinish显示Toast留言。
  3. 使用getActivity().runOnUIThread()
  4. 在UI线程上启动此计时器

    问题是计时器执行但ProgressBar一直显示不确定的进度,即它保持不变。在结束时也会看到Toast,但进度条没有任何变化。

    这里出了什么问题?

    以下是当前代码:

    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);
        }
    }
    

    布局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>
    

    以上代码的输出为:

    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>
    

1 个答案:

答案 0 :(得分:0)

错误是缺少XML中定义的ProgressBar的样式属性

style=""?android:attr/progressBarStyleHorizontal"

感谢pskink的评论