2017-12-25 20 views
0

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

} 
+0

'ViewPagerAdapter'とは - FragmentStatePagerAdapter'とは何ですか?または 'FragmentPagerAdapter' ..? – PPartisan

答えて

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を返す必要があるかもしれませんあなたの中にPagerAdapter

関連する問題