进度条和碎片

时间:2017-02-17 11:51:30

标签: android android-fragments

我试图根据片段的滑动更新进度条,完全有三个片段和三个循环进度条,即时取得进度条以及测试值没有问题,但我没有得到片段显示,这是代码,请让我知道我错在哪里。

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) {

            Log.e("History ", String.valueOf(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;
    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) {
        Log.e("History2 ", param1+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
        final 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(100).execute();
        new Progress2(20).execute();
        new Progress3(30).execute();
        new Progress(40).execute();
        new Progress2(50).execute();
        new Progress3(90).execute();
        new Progress(50).execute();
        new Progress2(10).execute();
        new Progress3(30).execute();
       /* 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(pager); //the UI pager
        mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager()) {
            @Override
            public int getCount()
            {
                return 0;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return false;
            }



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




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

        mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            public void onPageScrollStateChanged(int state) {}
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}



            public void onPageSelected(int position)
            {
                // Check if this is the page you want.
               getItem(position);
                Log.e("Started","hello");

            }


            public Fragment getItem(int position) {
                int i = 0;

                switch (position) {
                    case 0: { // Fragment # 0 - This will show FirstFragment


                        if (!(i < 3)) {

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

                    case 1: { // Fragment # 1 - This will show SecondFragment
                        Log.e("Fragment", "Fragment 2");
                        if (!(i < 3)) {
                            /*new Progress(40).execute();
                            new Progress2(50).execute();
                            new Progress3(90).execute();*/
                        }
                        ++i;
                        return Slidetwo.newInstance(String.valueOf(1), " ");
                    }

                    case 2: { // Fragment # 1 - This will show SecondFragment
                        Log.e("Fragment", "Fragment 3");
                        if (!(i < 3)) {
                           /* new Progress(50).execute();
                            new Progress2(10).execute();
                            new Progress3(0).execute();*/
                        }
                        ++i;

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

                    default:
                        return null;
                }
            }
        });

        return v;
    }

    class Progress extends AsyncTask<Void, Integer, Integer>
        {
            int dummyVariable = 0;
            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);
        }
    }

    private class ScreenSlidePagerAdapter extends PagerAdapter {
        public ScreenSlidePagerAdapter(Object fragmentManager) {
        }

        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return false;
        }
    }


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


}

`

0 个答案:

没有答案