将数据从活动传输到片段

时间:2017-12-25 09:02:29

标签: android android-fragments

如何在MainActivity的onTimeSet触发DataFragment并刷新它时传输数据? 点击setTodayBtn时会触发进程。

主要活动:

public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {

    public static MySQLiteHelper db;
    private ViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = findViewById(R.id.pager);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new DataFragment(), "Data");
        adapter.addFragment(new SummaryFragment(), "Summary");
        adapter.addFragment(new VacationFragment(), "Missing");
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new 
        ViewPager.OnPageChangeListener() {
            int currentPosition = 0;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                FragmentLifecycle fragmentToHide = (FragmentLifecycle) adapter.getItem(currentPosition);
                fragmentToHide.onPauseFragment();

                FragmentLifecycle fragmentToShow = (FragmentLifecycle) adapter.getItem(position);
                fragmentToShow.onResumeFragment();

                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        TabLayout tabLayout = findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }


    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {

        //Transfer i data to DataFragment
        //Refresh DataFragment


    }
}

数据片段:

public class DataFragment extends Fragment implements FragmentLifecycle{


    private Button setTodayBtn;


    public DataFragment() {

    }


    public static DataFragment newInstance(String param1, String param2) {
        DataFragment fragment = new DataFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


         View RootView = inflater.inflate(R.layout.fragment_data, container, false);
        setTodayBtn = RootView.findViewById(R.id.button_set_today);

        setTodayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                DialogFragment newFragment = new TimePickerFragment();
                newFragment.show(getActivity().getFragmentManager(), "timePicker");

            }
        });

        return RootView;
    }


    private void refreshData() {


    }



    @Override
    public void onAttachFragment(Fragment childFragment) {
        super.onAttachFragment(childFragment);

    }

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

    @Override
    public void onPauseFragment() {

    }

    @Override
    public void onResumeFragment() {

        refreshData();

    }



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



}

TimePickerFragment:

public class TimePickerFragment extends DialogFragment {

    private TimePickerDialog.OnTimeSetListener mListener;
    private Context context;


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;

        try {
            mListener = (TimePickerDialog.OnTimeSetListener) context;
        } catch (ClassCastException e) {
            throw new ClassCastException(context.toString() + " must implement OnTimeSetListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        return new TimePickerDialog(context, mListener, hour, minute, DateFormat.is24HourFormat(context));
    }

}

1 个答案:

答案 0 :(得分:1)

public class MainActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener {

    //...
    private DataFragment dataFragment;
    private ViewPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ViewPager viewPager = findViewById(R.id.pager);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        dataFragment = new DataFragment();
        adapter.addFragment(dataFragment, "Data");
        adapter.addFragment(new SummaryFragment(), "Summary");
        adapter.addFragment(new VacationFragment(), "Missing");
        viewPager.setAdapter(adapter);
    }
    //....
    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i1) {
        dataFragment.setData(i);
        adapter.notifyDataSetChanged();
    }
}

您可能还需要从POSITION_NONE内的getItemPosition()返回PagerAdapter