diff --git a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java index 1dd8e26..feb32ee 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -126,6 +126,7 @@ int month; int day; + String modo = "todoSchedule"; public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -139,29 +140,85 @@ todoList = new ArrayList<>(); scheduleList = new ArrayList<>(); + Button curDateButton = view.findViewById(R.id.date_button); curDateButton.setText(year + "年" + month + "月" + day + "日"); - + //TodoAdapter設定 RecyclerView todoRecyclerView = view.findViewById(R.id.my_todos_list); todoRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext()); todoRecyclerView.setLayoutManager(layoutManager); MyTodoshelfAdapter todoAdapter = new MyTodoshelfAdapter(todoList, integerBookHashMap, todosViewModel, getActivity()); todoRecyclerView.setAdapter(todoAdapter); - + todoAdapter.setItemWidth(dpToPx(130)); + //ScheduleAdapter設定 RecyclerView scheduleRecyclerView = view.findViewById(R.id.my_schedule_list); scheduleRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager scheduleLayoutManager = new LinearLayoutManager(view.getContext()); scheduleRecyclerView.setLayoutManager(scheduleLayoutManager); MyScheduleshelfAdapter scheduleAdapter = new MyScheduleshelfAdapter(scheduleList, integerBookHashMap, curDate, getActivity()); scheduleRecyclerView.setAdapter(scheduleAdapter); + scheduleAdapter.setItemWidth(dpToPx(160)); + + //ボタン処理 Button dateButton = view.findViewById(R.id.date_button); dateButton.setOnClickListener(v -> { ((MainActivity) getActivity()).showFragment(new CalendarFragment(year, month)); }); + Button modoTodoButton = view.findViewById(R.id.modo_todo_button); + Button modoScheduleButton = view.findViewById(R.id.modo_schedule_button); + + modoTodoButton.setOnClickListener(v -> { + ViewGroup.LayoutParams params = modoTodoButton.getLayoutParams(); + ViewGroup.LayoutParams todoparams = todoRecyclerView.getLayoutParams(); + if(modo == "todoSchedule") { + modoScheduleButton.setVisibility(View.GONE); + scheduleRecyclerView.setVisibility(View.GONE); + params.width = dpToPx(370); // 幅を全体に + modoTodoButton.setLayoutParams(params); + todoparams.width = dpToPx(370); + todoRecyclerView.setLayoutParams(todoparams); + todoAdapter.setItemWidth(dpToPx(330)); + modo = "todo"; + } else if(modo == "todo"){ + params.width = dpToPx(180); // 幅を全体に + modoTodoButton.setLayoutParams(params); + todoparams.width = dpToPx(180); + todoRecyclerView.setLayoutParams(todoparams); + modoScheduleButton.setVisibility(View.VISIBLE); + scheduleRecyclerView.setVisibility(View.VISIBLE); + todoAdapter.setItemWidth(dpToPx(130)); + modo = "todoSchedule"; + } + }); + + modoScheduleButton.setOnClickListener(v -> { + ViewGroup.LayoutParams params = modoScheduleButton.getLayoutParams(); + ViewGroup.LayoutParams scheduleRecyclerViewLayoutParams = scheduleRecyclerView.getLayoutParams(); + if(modo == "todoSchedule") { + modoTodoButton.setVisibility(View.GONE); + todoRecyclerView.setVisibility(View.GONE); + params.width = dpToPx(370); // 幅を全体に + modoScheduleButton.setLayoutParams(params); + scheduleRecyclerViewLayoutParams.width = dpToPx(370); + scheduleRecyclerView.setLayoutParams(scheduleRecyclerViewLayoutParams); + scheduleAdapter.setItemWidth(dpToPx(360)); + modo = "schedule"; + } else if(modo == "schedule"){ + params.width = dpToPx(180); // 幅を全体に + modoScheduleButton.setLayoutParams(params); + scheduleRecyclerViewLayoutParams.width = dpToPx(180); + scheduleRecyclerView.setLayoutParams(scheduleRecyclerViewLayoutParams); + modoTodoButton.setVisibility(View.VISIBLE); + todoRecyclerView.setVisibility(View.VISIBLE); + scheduleAdapter.setItemWidth(dpToPx(160)); + modo = "todoSchedule"; + } + }); + FloatingActionButton todoAddButton = view.findViewById(R.id.todo_add_button); todoAddButton.setOnClickListener(v -> { ((MainActivity) getActivity()).showFragment(new CreateTodoFragment(year, month, day)); @@ -196,10 +253,9 @@ updateTodoSchedule(integerBookHashMap); }); + booksViewModel.loadBooks(accountId, token);//ViewModel呼び出し - booksViewModel.loadBooks(accountId, token); - System.out.println(token); - + //ViewModel.observe(onChanged) booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(HashMap idBookHashMap) { @@ -270,7 +326,10 @@ scheduleAdapter.setSchedules(scheduleList, integerBookHashMap); } - + // dp を px に変換するためのメソッド + private int dpToPx(int dp) { + return Math.round(dp * getResources().getDisplayMetrics().density); + } @Override public void onStop() { @@ -286,6 +345,7 @@ private HashMap idBookHashMap; private TodosViewModel todosViewModel; private Context context; + private int itemWidth; // アイテムの幅を保持 MyTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Context context) { @@ -325,6 +385,11 @@ String token = citrus.getToken(); String accountId = citrus.getAccountId(); + // アイテムの幅を設定 + ViewGroup.LayoutParams params = holder.todoButton.getLayoutParams(); + params.width = itemWidth; // 動的に設定した幅を使用 + holder.todoButton.setLayoutParams(params); + Todo todoData = this.todoList.get(position); holder.todoButton.setText(todoData.getTitle()); holder.todoCheckBox.setChecked(todoData.getCheck()); @@ -372,6 +437,11 @@ }); } + public void setItemWidth(int width) { + this.itemWidth = width; + notifyDataSetChanged(); // 幅が変更されたことを通知 + } + static class MyTodoViewHolder extends RecyclerView.ViewHolder { Button todoButton; @@ -390,6 +460,7 @@ private HashMap idBookHashMap; private Context context; private LocalDate curDate; + private int itemWidth; // アイテムの幅を保持 MyScheduleshelfAdapter(List schedules, HashMap idBookHashMap, LocalDate curDate, Context context) { this.scheduleList = schedules; @@ -424,6 +495,11 @@ public void onBindViewHolder(@NonNull MyScheduleViewHolder holder, int position) { Activity activity = (Activity) context; + // アイテムの幅を設定 + ViewGroup.LayoutParams params = holder.scheduleButton.getLayoutParams(); + params.width = itemWidth; // 動的に設定した幅を使用 + holder.scheduleButton.setLayoutParams(params); + int red = 169; int green = 169; int blue = 169; @@ -511,6 +587,10 @@ notifyDataSetChanged(); // 必要に応じてアダプターをリフレッシュ } + public void setItemWidth(int width) { + this.itemWidth = width; + notifyDataSetChanged(); // 幅が変更されたことを通知 + } @Override public int getItemCount() { return scheduleList.size();