进度条更新不同的片段

时间:2017-02-16 12:23:44

标签: android android-fragments android-progressbar

公共类Slidetwo扩展Fragment {

private String mParam1;
private String mParam2;


String mon=String.valueOf(2);
String tue=String.valueOf(1);
String wed=String.valueOf(2);
String thur=String.valueOf(0);
String fri = String.valueOf(1);
String sat= String.valueOf(0);
String sun = String.valueOf(2);
String montab =String.valueOf(mon);
String tuetab = String.valueOf(tue);
String wedtab = String.valueOf(wed);
String thurstab = String.valueOf(thur);
String fritab = String.valueOf(fri);
String sattab = String.valueOf(sat);
String suntab = String.valueOf(sun);

String week = String.valueOf(mon+tue+wed+thur+fri+sat+sun);
int convertedVal = Integer.parseInt(week);
int finweek = (convertedVal/7);
int totalpercent= finweek;
String totalvaluedosage = String.valueOf(totalpercent);


private OnFragmentInteractionListener mListener;

public Slidetwo()
{
    // 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 Slidetwo.
 */
// TODO: Rename and change types and number of parameters
public static Slidetwo newInstance(String param1, String param2)
{
    Slidetwo fragment = new Slidetwo();
    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
    //return inflater.inflate(R.layout.fragment_slidetwo, container, false);
    View v = inflater.inflate(R.layout.fragment_slidetwo, container, false);

    TextView txt1=(TextView)v.findViewById(R.id.montab);
    if(txt1 !=null)
    {
        txt1.setText(montab);
    }
    TextView txt2=(TextView)v.findViewById(R.id.tuetab);
    if(txt2 !=null)
    {
        txt2.setText(tuetab);
    }
    TextView txt3=(TextView)v.findViewById(R.id.wedtab);
    if(txt3 !=null)
    {
        txt3.setText(wedtab);
    }
    TextView txt4=(TextView)v.findViewById(R.id.thurstab);
    if(txt4 !=null)
    {
        txt4.setText(thurstab);
    }
    TextView txt5=(TextView)v.findViewById(R.id.fritab);
    if(txt5 !=null)
    {
        txt5.setText(fritab);
    }
    TextView txt6=(TextView)v.findViewById(R.id.sattab);
    if(txt6 !=null)
    {
        txt6.setText(sattab);
    }
    TextView txt7=(TextView)v.findViewById(R.id.suntab);
    if(txt7 !=null)
    {
        txt7.setText(suntab);
    }


    ProgressBar simpleProgressBar1=(ProgressBar)v.findViewById(R.id.progressBarday1); // initiate the progress bar
   simpleProgressBar1.setMax(100); // 100 maximum value for the progress value
   // simpleProgressBar1.setProgress(100);

    ProgressBar simpleProgressBar2=(ProgressBar)v.findViewById(R.id.progressBarday2); // initiate the progress bar
    simpleProgressBar2.setMax(100); // 100 maximum value for the progress value
   // simpleProgressBar2.setProgress(50);

    ProgressBar simpleProgressBar3=(ProgressBar)v.findViewById(R.id.progressBarday3); // initiate the progress bar
    simpleProgressBar3.setMax(100); // 100 maximum value for the progress value
    //simpleProgressBar3.setProgress(100);

    ProgressBar simpleProgressBar4=(ProgressBar)v.findViewById(R.id.progressBarday4); // initiate the progress bar
    simpleProgressBar4.setMax(100); // 100 maximum value for the progress value
   // simpleProgressBar4.setProgress(0);

    ProgressBar simpleProgressBar5=(ProgressBar)v.findViewById(R.id.progressBarday5); // initiate the progress bar
    simpleProgressBar5.setMax(100); // 100 maximum value for the progress value
   // simpleProgressBar5.setProgress(20);

    ProgressBar simpleProgressBar6=(ProgressBar)v.findViewById(R.id.progressBarday6); // initiate the progress bar
    simpleProgressBar6.setMax(100); // 100 maximum value for the progress value
   // simpleProgressBar6.setProgress(75);

    ProgressBar simpleProgressBar7=(ProgressBar)v.findViewById(R.id.progressBarday7); // initiate the progress bar
    simpleProgressBar7.setMax(100); // 100 maximum value for the progress value
   // simpleProgressBar7.setProgress(50);

    return v;
}

// 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 onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

`实际上即时创建一个包含多个片段的应用程序,我在一个活动中创建了三个进度条。基于正在打开的片段,进度条的数据应该更改,例如从一个片段滑动到另一个片段应该更改进度条的值。因此,如果您有任何想法如何实现这一点,请帮助我:))

1 个答案:

答案 0 :(得分:0)

Frgament代码

    public class Slidetwo extends Fragment {

        String mon=String.valueOf(2);
        String tue=String.valueOf(1);
        String wed=String.valueOf(2);
        String thur=String.valueOf(0);
        String fri = String.valueOf(1);
        String sat= String.valueOf(0);
        String sun = String.valueOf(2);
        String montab =String.valueOf(mon);
        String tuetab = String.valueOf(tue);
        String wedtab = String.valueOf(wed);
        String thurstab = String.valueOf(thur);
        String fritab = String.valueOf(fri);
        String sattab = String.valueOf(sat);
        String suntab = String.valueOf(sun);

        String week = String.valueOf(mon+tue+wed+thur+fri+sat+sun);
        int convertedVal = Integer.parseInt(week);
        int finweek = (convertedVal/7);
        int totalpercent= finweek;
        String totalvaluedosage = String.valueOf(totalpercent);


        private OnFragmentInteractionListener mListener;

        public Slidetwo()
        {

        }

        View rootView;
        private String mParam1;
        private String mParam2;
        private static final String ARG_PARAM1 = "param1";
        private static final String ARG_PARAM2 = "param2";


        public static Slidetwo newInstance(String param1, String param2)
        {
            Slidetwo fragment = new Slidetwo();
            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)
        {

            rootView = inflater.inflate(R.layout.fragment_main1, container, false);

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

            /*TextView txt1=(TextView)v.findViewById(R.id.montab);
            if(txt1 !=null)
            {
                txt1.setText(montab);
            }
            TextView txt2=(TextView)v.findViewById(R.id.tuetab);
            if(txt2 !=null)
            {
                txt2.setText(tuetab);
            }
            TextView txt3=(TextView)v.findViewById(R.id.wedtab);
            if(txt3 !=null)
            {
                txt3.setText(wedtab);
            }
            TextView txt4=(TextView)v.findViewById(R.id.thurstab);
            if(txt4 !=null)
            {
                txt4.setText(thurstab);
            }
            TextView txt5=(TextView)v.findViewById(R.id.fritab);
            if(txt5 !=null)
            {
                txt5.setText(fritab);
            }
            TextView txt6=(TextView)v.findViewById(R.id.sattab);
            if(txt6 !=null)
            {
                txt6.setText(sattab);
            }
            TextView txt7=(TextView)v.findViewById(R.id.suntab);
            if(txt7 !=null)
            {
                txt7.setText(suntab);
            }*/

            ProgressBar simpleProgressBar1 =(ProgressBar)rootView.findViewById(R.id.progressbar);

            if(mParam1.equalsIgnoreCase("Slideone"))
            {
                simpleProgressBar1.setMax(100);
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

                    Drawable wrapDrawable = DrawableCompat.wrap(simpleProgressBar1.getIndeterminateDrawable());
                    DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
                    simpleProgressBar1.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
                } else
                {
                    simpleProgressBar1.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
                }
            }
            else  if(mParam1.equalsIgnoreCase("Slidetwo"))
            {
                simpleProgressBar1.setMax(50);
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

                    Drawable wrapDrawable = DrawableCompat.wrap(simpleProgressBar1.getIndeterminateDrawable());
                    DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_orange_dark));
                    simpleProgressBar1.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
                } else
                {
                    simpleProgressBar1.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_orange_dark), PorterDuff.Mode.SRC_IN);
                }
            }
            else  if(mParam1.equalsIgnoreCase("Slidethree"))
            {
                simpleProgressBar1.setMax(60);
                simpleProgressBar1.getProgressDrawable().setColorFilter(Color.CYAN, android.graphics.PorterDuff.Mode.SRC_IN);
            }
            else
            {
                simpleProgressBar1.setMax(60);
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

                    Drawable wrapDrawable = DrawableCompat.wrap(simpleProgressBar1.getIndeterminateDrawable());
                    DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_blue_dark));
                    simpleProgressBar1.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
                } else
                {
                    simpleProgressBar1.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_blue_dark), PorterDuff.Mode.SRC_IN);
                }
            }

            return rootView;
        }

        public void onButtonPressed(Uri uri)
        {
            if (mListener != null)
            {
                mListener.onFragmentInteraction(uri);
            }
        }


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


        public interface OnFragmentInteractionListener
        {
            void onFragmentInteraction(Uri uri);
        }

     2. xml file

    <?xml version="1.0" encoding="utf-8"?>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ProgressBar
            android:id="@+id/progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

    </RelativeLayout>

 1. setViewPager method

 private void setViewPager()
    {
        try
        {
            PagerAdapter pagerAdapter = new PagerAdapter(getChildFragmentManager(),getActivity());
            viewPager.destroyDrawingCache();
            pagerAdapter.notifyDataSetChanged();
            viewPager.setAdapter(pagerAdapter);
            viewPager.setCurrentItem(0);
            simpletababar.setupWithViewPager(viewPager);
        }
        catch (Exception e)
        {
            Log.e(Constants.TAG, "Caught exception", e);
        }
    }

 4. PagerAdapter

public class PagerAdapter extends SimpleTabBarAdapter
{
    private final String[] TITLES = { "Acheivement_", "Pipeline" };
    private final int[] Colors = { R.color.colorAccent, R.color.colorPrimary };
    private List<Fragment> fragments;
    Activity activity;

    public PagerAdapter(FragmentManager fm, Activity activity)
    {
        super(fm);
        this.activity = activity;
    }

    public PagerAdapter(FragmentManager fm, Activity activity, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
        this.activity = activity;
    }

    @Override
    public Fragment getItem(int position)
    {

        switch (position)
        {

            case 0:
//                Bundle bundle = new Bundle();
//                bundle.putInt("position", position);
//                Fragment fragment = fragments.get(position);
//                fragment.setArguments(bundle);
                return Slidetwo.newInstance("Slideone","");

            case 1:
                  return Slidetwo.newInstance("Slidetwo","");

        }
        return null;
    }

    @Override
    public CharSequence getPageTitle(int position)
    {
        return TITLES[position];
    }

    @Override
    public int getColorResource(int position) {
        return Colors[position];
    }

    @Override
    public int getTextColorResource(int position) {
        return Colors[position];
    }

    @Override
    public int getIconResource(int position) {
        return 0;
    }

    @Override
    public int getCount()
    {
        return TITLES.length;
    }


}