CalendarView在片段中不起作用?安卓

huangapple go评论117阅读模式
英文:

CalendarView does not work in fragments? Android

问题

如果我在xml文件中为主要活动添加一个CalendarView,然后在主要活动中编写一些代码:

  1. public class MainActivity extends AppCompatActivity {
  2. private CalendarView mCalendarView;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. mCalendarView = findViewById(R.id.calendarView);
  8. mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
  9. @Override
  10. public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
  11. String date = year + "/" + month + "/" + dayOfMonth;
  12. Log.wtf("wtf", "onSelectedDayChange" + date);
  13. }
  14. });
  15. }
  16. }

setOnDateChangeListener将按预期工作,Log.wtf将被执行。

如果我尝试在一个片段的xml文件中添加一个CalendarView,日历将出现在片段中,我可以点击日期。但由于某种原因,setOnDateChangeListener不起作用。这是我片段的代码。抱歉,这是默认代码,所以有点多。我是新手使用片段,所以我不确定什么是所有重要的。我在onCreateView方法中实现了setOnDateChangeListener。

  1. public class ThirdFragment extends Fragment {
  2. private CalendarView mCalendarView;
  3. @Override
  4. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  5. Bundle savedInstanceState) {
  6. View view = inflater.inflate(R.layout.fragment_third, container, false);
  7. mCalendarView = (CalendarView) view.findViewById(R.id.calendarView);
  8. mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
  9. @Override
  10. public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
  11. String date = year + "/" + month + "/" + dayOfMonth;
  12. Log.wtf("wtf", "onSelectedDayChange" + date);
  13. }
  14. });
  15. return view;
  16. }
  17. }

但不知何故,setOnDateChangeListener不起作用。我真的不知道为什么这不起作用,因为我在项目中有其他片段,我在其中有按钮,按钮点击事件是有效的,我是这样实现的:

  1. public class YourFragment extends Fragment {
  2. private OnFragmentInteractionListener mListener;
  3. @Override
  4. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  5. Bundle savedInstanceState) {
  6. View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
  7. Button button = view.findViewById(R.id.your_button_id);
  8. button.setOnClickListener(new View.OnClickListener() {
  9. @Override
  10. public void onClick(View v) {
  11. mListener.yourMethod(); // 这里调用与活动通信的方法
  12. }
  13. });
  14. return view;
  15. }
  16. }
英文:

So if I create a new project and throw a calanderView into the xml file for the main activity. Then write some code in the main activity

  1. public class MainActivity extends AppCompatActivity {
  2. private CalendarView mCalendarView;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. mCalendarView = findViewById(R.id.calendarView);
  8. mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
  9. @Override
  10. public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
  11. String date = year + "/" + month + "/" + dayOfMonth;
  12. Log.wtf("wtf","onSelectedDayChange" + date);
  13. }
  14. });
  15. }
  16. }

The setOnDateChangeListener will work as expected and Log.wtf will execute.

If I try to throw a calanderView into a xml file for a fragment. The calendar will appear in the fragment and I can click on dates. But for some reason the setOnDateChangeListener does not work. Here is my code for my fragment. Sorry its the default code so there is kind of a lot. I am new to using fragments so I am not sure whats all important. I implemented the setOnDateChangeListener in the onCreateView method.

  1. public class ThirdFragment extends Fragment {
  2. // TODO: Rename parameter arguments, choose names that match
  3. // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  4. private static final String ARG_PARAM1 = "param1";
  5. private static final String ARG_PARAM2 = "param2";
  6. // TODO: Rename and change types of parameters
  7. private String mParam1;
  8. private String mParam2;
  9. private OnFragmentInteractionListener mListener;
  10. public ThirdFragment() {
  11. // Required empty public constructor
  12. }
  13. // TODO: Rename and change types and number of parameters
  14. public static ThirdFragment newInstance(String param1, String param2) {
  15. ThirdFragment fragment = new ThirdFragment();
  16. Bundle args = new Bundle();
  17. args.putString(ARG_PARAM1, param1);
  18. args.putString(ARG_PARAM2, param2);
  19. fragment.setArguments(args);
  20. return fragment;
  21. }
  22. @Override
  23. public void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. if (getArguments() != null) {
  26. mParam1 = getArguments().getString(ARG_PARAM1);
  27. mParam2 = getArguments().getString(ARG_PARAM2);
  28. }
  29. }
  30. private CalendarView mCalendarView;
  31. @Override
  32. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  33. Bundle savedInstanceState) {
  34. View view = inflater.inflate(R.layout.fragment_third, container, false);
  35. mCalendarView = (CalendarView) view.findViewById(R.id.calendarView);
  36. mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
  37. @Override
  38. public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
  39. String date = year + "/" + month + "/" + dayOfMonth;
  40. Log.wtf("wtf","onSelectedDayChange" + date);
  41. }
  42. });
  43. return inflater.inflate(R.layout.fragment_third, container, false);
  44. }
  45. // TODO: Rename method, update argument and hook method into UI event
  46. public void onButtonPressed(Uri uri) {
  47. if (mListener != null) {
  48. mListener.onFragmentInteraction(uri);
  49. }
  50. }
  51. @Override
  52. public void onAttach(Context context) {
  53. super.onAttach(context);
  54. if (context instanceof OnFragmentInteractionListener) {
  55. mListener = (OnFragmentInteractionListener) context;
  56. } else {
  57. throw new RuntimeException(context.toString()
  58. + " must implement OnFragmentInteractionListener");
  59. }
  60. }
  61. @Override
  62. public void onDetach() {
  63. super.onDetach();
  64. mListener = null;
  65. }
  66. /**
  67. * This interface must be implemented by activities that contain this
  68. * fragment to allow an interaction in this fragment to be communicated
  69. * to the activity and potentially other fragments contained in that
  70. * activity.
  71. * <p>
  72. * See the Android Training lesson <a href=
  73. * "http://developer.android.com/training/basics/fragments/communicating.html"
  74. * >Communicating with Other Fragments</a> for more information.
  75. */
  76. public interface OnFragmentInteractionListener {
  77. // TODO: Update argument type and name
  78. void onFragmentInteraction(Uri uri);
  79. }
  80. }

I honestly have no idea why this does not work because I have other fragments in my project where I have buttons where the button click event works and I implemented those like this:

  1. private OnFragmentInteractionListener mListener;
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4. // Inflate the layout for this fragment
  5. View view = inflater.inflate(R.layout.fragment_login, container, false);
  6. button2 = view.findViewById(R.id.btn_register);
  7. button2.setOnClickListener(new View.OnClickListener() {
  8. @Override
  9. public void onClick(View v) {
  10. // IMPORTANT this interface can send stuff back to the main activity
  11. mListener.register();
  12. }
  13. });
  14. return view;
  15. }

If anyone could give me some insight it would be much appreciated!

答案1

得分: 0

在 Fragments 中,如果你没有使用 binding,那么在 onCreateView 方法中,你只应该填充并返回视图。然后在 onViewCreated 方法中处理你的代码,因为这会确保你获取到了填充的视图并且视图不为空

你还应该查看Fragment 生命周期方法。这样你就会理解 Fragment 是如何与 Activity 一起工作的。

英文:

In Fragments, if you're not using binding then in onCreateView method you should only inflate and return the view.. And do your code stuff in onViewCreated as it makes sure that your get the inflated view and the view is not null.

You should also look for Fragment Lifecycle Methods.
That way you'll understand how Fragments works along with Activity.

答案2

得分: 0

你需要在onCreateView方法中返回与您在其中填充的view对象相同的对象。修改您的onCreateView方法如下:

  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4. View view = inflater.inflate(R.layout.fragment_third, container, false);
  5. mCalendarView = (CalendarView) view.findViewById(R.id.calendarView);
  6. mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
  7. @Override
  8. public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
  9. String date = year + "/" + month + "/" + dayOfMonth;
  10. Log.wtf("wtf","onSelectedDayChange" + date);
  11. }
  12. });
  13. return view; // 而不是返回 inflater.inflate(R.layout.fragment_third, container, false);
  14. }
英文:

You need to return the same view object which you have inflated in onCreateView method. Modify your onCreateView method like this:

  1. @Override
  2. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  3. Bundle savedInstanceState) {
  4. View view = inflater.inflate(R.layout.fragment_third, container, false);
  5. mCalendarView = (CalendarView) view.findViewById(R.id.calendarView);
  6. mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
  7. @Override
  8. public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
  9. String date = year + "/" + month + "/" + dayOfMonth;
  10. Log.wtf("wtf","onSelectedDayChange" + date);
  11. }
  12. });
  13. return view; //instead of inflater.inflate(R.layout.fragment_third, container, false);
  14. }

huangapple
  • 本文由 发表于 2020年10月13日 14:52:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64330046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定