package com.example.citrusclient.views; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; import com.example.citrusclient.R; import com.example.citrusclient.models.Schedule; import com.example.citrusclient.viewmodels.ScheduleViewModel; import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.Calendar; import java.util.List; /** * A simple {@link Fragment} subclass. * Use the {@link CalendarFragment#newInstance} factory method to * create an instance of this fragment. */ public class CalendarFragment extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private static final String ARG_PARAM1 = "param1"; private static final String ARG_PARAM2 = "param2"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; private TableLayout tableLayout; private TableRow[] tableRows = new TableRow[6]; public CalendarFragment() { // 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 ScheduleFragment. */ // TODO: Rename and change types and number of parameters public static CalendarFragment newInstance(String param1, String param2) { CalendarFragment fragment = new CalendarFragment(); Bundle args = new Bundle(); args.putString(ARG_PARAM1, param1); args.putString(ARG_PARAM2, param2); fragment.setArguments(args); return fragment; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_PARAM1); mParam2 = getArguments().getString(ARG_PARAM2); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_calendar, container, false); } private List<Schedule> scheduleList; ScheduleViewModel scheduleViewModel; int month; @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); tableLayout = view.findViewById(R.id.calendarlayout); Calendar calendar = Calendar.getInstance(); month = calendar.get(Calendar.MONTH) + 1; int date = calendar.get(Calendar.DATE); int lastdate = calendar.getActualMaximum(Calendar.DATE); int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); Button nextMonth = view.findViewById(R.id.nextMonth); Button prevMonth = view.findViewById(R.id.prevMonth); TextView curMonth = view.findViewById(R.id.month); curMonth.setText("" + month + "月"); nextMonth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { month++; if(month > 12){ month = 1; } curMonth.setText("" + month + "月"); } }); prevMonth.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { month--; if(month < 1) { month = 12; } curMonth.setText("" + month + "月"); } }); for(int i = 0; i < 6; i++) { tableRows[i] = (TableRow) tableLayout.getChildAt(i); for(int j = 0; j < 7; j++) { LinearLayout layout = new LinearLayout(requireContext()); layout.setOrientation(LinearLayout.VERTICAL); TextView textView = new TextView(requireContext()); textView.setText("" + i); RecyclerView recyclerView = new RecyclerView(requireContext()); recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext())); recyclerView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); List<Schedule> schedules = new ArrayList<>(); schedules.add(new Schedule("abc", "3232", "yyyy", 0, 1)); schedules.add(new Schedule("123", "3232", "yyyy", 0, 2)); recyclerView.setAdapter(new MyScheduleAdapter(schedules)); LinearLayout.LayoutParams p = (LinearLayout.LayoutParams) recyclerView.getLayoutParams(); p.weight = 1; layout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT)); TableRow.LayoutParams p2 = (TableRow.LayoutParams) layout.getLayoutParams(); p2.weight = 1; layout.addView(textView); layout.addView(recyclerView); tableRows[i].addView(layout); } } } } class MyScheduleAdapter extends RecyclerView.Adapter<MyScheduleAdapter.MyScheduleViewHolder> { private List<Schedule> scheduleList; MyScheduleAdapter(List<Schedule> schedules) { this.scheduleList = schedules; } public void setSchedules(List<Schedule> schedules) { scheduleList = schedules; notifyDataSetChanged(); } @NonNull @Override public MyScheduleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_calendar_schedule, parent, false); return new MyScheduleViewHolder(view); } @Override public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { Schedule scheduleData = this.scheduleList.get(position); holder.scheduleText.setText(scheduleData.getTitle()); } @Override public int getItemCount() { return scheduleList.size(); } static class MyScheduleViewHolder extends RecyclerView.ViewHolder { TextView scheduleText; public MyScheduleViewHolder(@NonNull View itemView) { super(itemView); scheduleText = itemView.findViewById(R.id.schedule_text); } } }