progressbar

<LinearLayout 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.example.yuekao1.fragment.ShouCangFragment">

    <!--//xml定義進度條-->
    <ProgressBar
        android:id="@+id/pb_ProgressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:layout_gravity="center"/>

</LinearLayout>

 

 

javajava

package com.example.yuekao1.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.example.yuekao1.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShouCangFragment extends Fragment {


    private ProgressBar pb_ProgressBar;
    private int num = 0;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            num += 10;
            if (num == 100) {
                pb_ProgressBar.setVisibility(View.GONE);
            }
            pb_ProgressBar.setProgress(num);
            handler.postDelayed(runnable, 2000);


        }

    };

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shou_cang, container, false);
        initView(view);

        handler.postDelayed(runnable, 2000);

        return view;
    }

    private void initView(View view) {
        pb_ProgressBar = (ProgressBar) view.findViewById(R.id.pb_ProgressBar);
    }
}
相關文章
相關標籤/搜索