実現したいこと
カレンダーを基にしたアプリを作成しています。
現在は、初期表示として1月のカレンダーが表示されており(12月31日・2月1~10日も一緒に表示)
1月から2月もしくは12月に移動できる仕様です。
該当のソースコード
MonthFragment.java
1~~省略~~ 2public class MonthFragment extends Fragment { 3 View view; 4 Calendar this_month_instance; 5 int this_first_day_of_week; 6 Integer this_last_day_of_week; 7 int preMaxDate; 8 int rest; 9 10 private ArrayList<DateList> dateList; 11 Calendar cl = new GregorianCalendar(); 12 int this_year = cl.get(Calendar.YEAR); 13 int this_month = cl.get(Calendar.MONTH); 14 int this_date = cl.get(Calendar.DATE); 15 int this_maxDate = cl.getActualMaximum(Calendar.DATE); // 今月の最大日数 16 int idx = 0; 17 18 @Override 19 public View onCreateView(LayoutInflater inflater, ViewGroup container, 20 Bundle savedInstanceState) { 21 return inflater.inflate(R.layout.month_fragment, container, false); 22 } 23 24 @Override 25 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 26 super.onStart(); 27 this.view = view; 28 Button post = view.findViewById(R.id.post); 29 post.setOnClickListener(new View.OnClickListener() { 30 @Override 31 public void onClick(View view) { 32 this_month++; 33 viewCalendar(); 34 } 35 }); 36 viewCalendar(); 37 } 38 39 public void viewCalendar() { 40 dataInitialize(); 41 42 TextView yearText = this.view.findViewById(R.id.year); 43 yearText.setText(this_year + "年"); 44 45 TextView monthText = this.view.findViewById(R.id.month); 46 monthText.setText(this_month + 1 + "月"); 47 48 RecyclerView recyclerView = this.view.findViewById(R.id.date_recycler_view_container); 49 recyclerView.setHasFixedSize(true); 50 // ①recycleView に layoutManager をセット 51 recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 7)); 52 // ②Adapter を生成して RecyclerView にセット 53 recyclerView.setAdapter(new Monthly_RecyclerView_Adapter(getActivity(), this.dateList)); 54 } 55 56 // dateList = [DateList, , ...] を作成 57 private void dataInitialize() { 58 this.dateList = new ArrayList<>(); // push する対象 59 // 今月1日のインスタンス 60 this.this_month_instance = new GregorianCalendar(this.this_year, this.this_month, 1); 61 // 今月1日の曜日 62 this.this_first_day_of_week = this_month_instance.get(Calendar.DAY_OF_WEEK); 63 // 今月最終日の曜日 64 this.this_last_day_of_week = new GregorianCalendar(this.this_year, this.this_month, this.this_maxDate).get(Calendar.DAY_OF_WEEK); 65 // 先月の最大日数 66 this.preMaxDate = new GregorianCalendar(this.this_year, this.this_month-1, 1) 67 .getActualMaximum(Calendar.DATE); 68 69 //★① 1日よりも前の日付生成 70 this.this_first_day_of_week = - (this.this_first_day_of_week - 1); // -(今月初日の曜日 - 1) 71 if(this.this_first_day_of_week <= 0) { 72 //「1日の曜日-1」が付け足す先月の日数。その日数の絶対値をマイナスにして、カウントアップし、0未満の場合は繰り返す。 73 while(this.this_first_day_of_week < 0) { 74 this.dateList.add(new DateList(this.this_year, this.this_month - 1, this.preMaxDate+this.this_first_day_of_week+1, false)); 75 this.this_first_day_of_week++; 76 } 77 this.idx++; 78 } 79 //★② その月の日付生成 80 while(this.idx <= this.this_maxDate) { 81 // 当日は当日フラグ = true 82 if(this.idx == this.this_date) { 83 this.dateList.add(new DateList(this.this_year, this.this_month, this.idx, true)); 84 // 当日は当日フラグ = false 85 } else { 86 this.dateList.add(new DateList(this.this_year, this.this_month, this.idx, false)); 87 } 88 this.idx++; 89 } 90 //★③ 来月の日付生成 91 this.rest = 7 - this.this_last_day_of_week; 92 this.idx = 1; 93 // 先月の最終週の日数(rest) + 次の週の日数(7日) 94 while (rest+7 > 0) { 95 this.dateList.add(new DateList(this.this_year, this.this_month + 1, this.idx, false)); 96 this.idx++; 97 this.rest--; 98 } 99 } 100 public class DateList { 101 private int year; // 年 102 private int month;// 月 103 private int date; // 日にち 104 private boolean todayFlg; // 今日かどうかのフラグ 105 DateList(int year,int month, int date, boolean todayFlg) { 106 this.year = year; 107 this.month = month; 108 this.date = date; 109 this.todayFlg = todayFlg; 110 } 111 public int getYear() { 112 return this.year; 113 } 114 public int getMonth() { 115 return this.month; 116 } 117 public int getDate() { 118 return this.date; 119 } 120 } 121}
Monthly_RecyclerView_Adapter.java
1~~省略~~ 2public class Monthly_RecyclerView_Adapter extends RecyclerView.Adapter<Monthly_RecyclerView_Adapter.ViewHolder>{ 3 4 Context content; 5 ArrayList<MonthFragment.DateList> DateList; 6 7 Monthly_RecyclerView_Adapter(Context content, ArrayList<MonthFragment.DateList> DateList) { 8 this.content = content; 9 this.DateList = DateList; 10 } 11 12 @Override 13 public int getItemCount() { 14 return this.DateList.size(); 15 } 16 17 @NonNull 18 @Override 19 public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 20 // content は MainActivity 21 View view = LayoutInflater.from(content).inflate(R.layout.date_viewholder, parent, false); 22 return new ViewHolder(view); 23 } 24 25 @Override 26 public void onBindViewHolder(@NonNull ViewHolder holder, int position) { 27 28 // 各日付の曜日を取得 29 int year = this.DateList.get(position).getYear(); 30 int month = this.DateList.get(position).getMonth(); 31 int date = this.DateList.get(position).getDate(); 32 33 Calendar cl = new GregorianCalendar(year, month, date); 34 int dayOfWeekNum = cl.get(Calendar.DAY_OF_WEEK); 35 36 String[] weekDays = {"", "(日)", "(月)", "(火)", "(水)", "(木)", "(金)", "(土)"}; 37 String dayOfWeek = weekDays[dayOfWeekNum]; 38 39 // 日付 + 曜日を出力 40 holder.dateBox.setText(date + dayOfWeek); 41 42 holder.dateBox.setClickable(true); 43 holder.dateBox.setOnClickListener(new View.OnClickListener() { 44 @Override 45 public void onClick(View view) { 46 TextView textView = (TextView)view; 47 FragmentActivity fragmentActivity = (FragmentActivity)view.getContext(); 48 DateFragment dateFragment = new DateFragment(); 49 DailyCalendarFragment dailyCalendarFragment = new DailyCalendarFragment(); 50 51 // 日付画面フラグメントにパラメータを付与する(〇月・〇日) 52 Bundle bundle = new Bundle(); 53 TextView monthView = fragmentActivity.findViewById(R.id.month); 54 bundle.putString("month", monthView.getText().toString()); 55 bundle.putString("date", textView.getText().toString()); 56 dateFragment.setArguments(bundle); 57 58 // 日付画面フラグメントを起動する 59 fragmentActivity.getSupportFragmentManager() 60 .beginTransaction() 61 .replace(R.id.month_fragment_container, dateFragment) 62 .addToBackStack("date") 63 .commit(); 64 65 fragmentActivity.getSupportFragmentManager() 66 .beginTransaction() 67 .add(R.id.fragmentContainerView, dailyCalendarFragment) 68 .addToBackStack("dailyCalender") 69 .commit(); 70 } 71 }); 72 } 73 74 public static class ViewHolder extends RecyclerView.ViewHolder { 75 TextView dateBox; 76 77 ViewHolder(@NonNull View itemView) { 78 super(itemView); 79 dateBox = itemView.findViewById(R.id.every_date); 80 } 81 } 82}
MainActivity.java
1package com.example.RecordTime; 2~~省略~~ 3public class MainActivity extends FragmentActivity { 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_main); 8 9 getSupportFragmentManager().beginTransaction() 10 .setReorderingAllowed(true) 11 .add(R.id.month_fragment_container, new MonthFragment()) 12 .commit(); 13 } 14}
回答1件
あなたの回答
tips
プレビュー