刷卡片段

时间:2017-02-17 05:34:32

标签: android android-fragments progress-bar

我正在创建一个应用程序,根据所选片段显示进度条中的数据,因此如果选择了特定片段,进度条应该显示该片段中的数据等等,在我的应用程序中有三个片段正在进行中的数据bar更新为两个片段,而不是第三个片段,我尝试了日志一切看起来很好,我试过给日志和我想出的是正在打开的片段只有两个,我没有得到第三个日志,即使我打开第三个片段,并且我得到打开第三个片段的指示,如果我再次移回第二个片段,我无法弄清楚我在哪里错过它。下面是我的代码

public class History extends Fragment {
     final int PAGE_COUNT = 3;
     private String tabTitles[] = new String[]{"Tab1", "Tab2", "Tab3"};
     public static final String ARG_PAGE = "ARG_PAGE";

    private static final int NUM_PAGES =3;

    String medicinename,medicinepercentage,numberdosages,time;
    String date,day,daypercentage;
    String month,monthdata;

    /**
     * The pager widget, which handles animation and allows swiping horizontally to access previous
     * and next wizard steps.
     */
    private ViewPager mPager;

    /**
     * The pager adapter, which provides the pages to the view pager widget.
     */
    private PagerAdapter mPagerAdapter;


    private int selectedPage; // set selected page

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";


    DonutProgress dp;
    DonutProgress dp2;
    DonutProgress dp3;

    public static History newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        History fragment = new History();
        fragment.setArguments(args);
        return fragment;
    }


    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private Paint progressPaint = new Paint();
    private int lineHeight = 30;
    private Paint incompletePaint = new Paint();

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

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment History.
     */
    // TODO: Rename and change types and number of parameters
    public static History newInstance(String param1, String param2) {
        History fragment = new History();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_history, container, false);

        dp = (DonutProgress) v.findViewById(R.id.donut_progress1);
        dp2 = (DonutProgress) v.findViewById(R.id.donut_progress2);
        dp3 = (DonutProgress) v.findViewById(R.id.donut_progress3);
        new Progress(0).execute();
        new Progress2(0).execute();
        new Progress3(0).execute();



        if (getArguments() != null)
        {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);

        }

        mPager = (ViewPager) v.findViewById(R.id.pager); //the UI pager
        mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
        mPager.setAdapter(mPagerAdapter);
        // mPager.setPageTransformer(true, new RotateDownTransformer()); //set the animation

        selectedPage = 0; //current page
        if (savedInstanceState != null) { //if app was paused, you can reopen the same page
            selectedPage = savedInstanceState.getInt("SELECTED_PAGE");
        }
        selectedPage = 1; //current page
        if (savedInstanceState != null) { //if app was paused, you can reopen the same page
            selectedPage = savedInstanceState.getInt("SELECTED_PAGE");
        }
        selectedPage = 2; //current page
        if (savedInstanceState != null) { //if app was paused, you can reopen the same page
            selectedPage = savedInstanceState.getInt("SELECTED_PAGE");
        }

        selectedPage = 3; //current page
        if (savedInstanceState != null) { //if app was paused, you can reopen the same page
            selectedPage = savedInstanceState.getInt("SELECTED_PAGE");
        }


        mPager.setCurrentItem(selectedPage); //set the current page

        return v;


        // return inflater.inflate(R.layout.fragment_history, container, false);

    }


    private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
        public ScreenSlidePagerAdapter(FragmentManager fm) {
            super(fm);
        }
        int i = 0;
        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0: // Fragment # 0 - This will show FirstFragment
                    Log.e("Fragment","Fragment 1::"+String.valueOf(i));

                        new Progress(100).execute();
                        new Progress2(20).execute();
                        new Progress3(30).execute();

                    return SlideFragment.newInstance(String.valueOf(0), " ");

                case 1: // Fragment # 1 - This will show SecondFragment
                    Log.e("Fragment","Fragment 2");

                        new Progress(40).execute();
                        new Progress2(50).execute();
                        new Progress3(90).execute();

                    return Slidetwo.newInstance(String.valueOf(1), " ");
                case 2: // Fragment # 1 - This will show SecondFragment
                    Log.e("Fragment","Fragment 3");

                        new Progress(50).execute();
                        new Progress2(10).execute();
                        new Progress3(0).execute();

                    return SlideThree.newInstance(String.valueOf(2), " ");

                default:
                    return null;
            }
        }
        @Override
        public int getCount()
        {
            return NUM_PAGES;
        }


    }


    class Progress extends AsyncTask<Void, Integer, Integer>
        {
            int dummyVariable = 1;
            Progress(int dummyVariable)
            {
               this.dummyVariable = dummyVariable;
            }
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dp.setMax(100);
             // Progress.getIndeterminateDrawable().setColorFilter(Color.parseColor("#C0D000"), android.graphics.PorterDuff.Mode.SRC_ATOP);
            }

            @Override
            protected Integer doInBackground(Void... params)
            {
                for (int i = 0; i <= dummyVariable; i++)
                {
                    publishProgress(i);
                    try
                    {
                        Thread.sleep(20);
                    }
                    catch (InterruptedException ie)
                    {
                        ie.printStackTrace();
                    }
                }
                return null;

            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
                dp.setProgress(values[0]);
            }

            @Override
            protected void onPostExecute(Integer integer) {
                super.onPostExecute(integer);
            }
        }

        class Progress2 extends AsyncTask<Void, Integer, Integer> {
            int dummyVariable = 0;
            Progress2(int dummyVariable)
            {
                this.dummyVariable = dummyVariable;
            }


            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dp2.setMax(100);
            }

            @Override
            protected Integer doInBackground(Void... params) {
                for (int i = 0; i <= dummyVariable; i++) {
                    publishProgress(i);
                    try {
                        Thread.sleep(20);
                    }
                    catch (InterruptedException ie)
                    {
                        ie.printStackTrace();
                    }
                }
                return null;
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);

                dp2.setProgress(values[0]);
            }

            @Override
            protected void onPostExecute(Integer integer) {
                super.onPostExecute(integer);
            }
        }


    class Progress3 extends AsyncTask<Void, Integer, Integer>
    {

        int dummyVariable = 0;
        Progress3(int dummyVariable)
        {
            this.dummyVariable = dummyVariable;
        }

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            dp3.setMax(100);
        }

        @Override
        protected Integer doInBackground(Void... params)
        {
            for (int i = 0; i <= dummyVariable; i++) {
                publishProgress(i);
                try
                {
                    Thread.sleep(20);
                }
                catch (InterruptedException ie)
                {
                    ie.printStackTrace();
                }
            }
            return null;


        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            dp3.setProgress(values[0]);
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);
        }
    }


        // TODO: Rename method, update argument and hook method into UI event


    }

1 个答案:

答案 0 :(得分:0)

你设置了offscreenPaginglimit吗? 尝试设置

public Animation getWhateverAnimation(int duration){
    return super.getWhateverAnimation(0);
}

请告诉我这是否有帮助!