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 8c23744..cd900b8 100644 --- a/app/src/main/java/com/example/citrusclient/views/HomeFragment.java +++ b/app/src/main/java/com/example/citrusclient/views/HomeFragment.java @@ -98,6 +98,8 @@ month = citrus.getCurMonth(); day = citrus.getCurDay(); + modo = "todoSchedule"; + todosViewModel = new ViewModelProvider(this).get(TodosViewModel.class); scheduleViewModel = new ViewModelProvider(this).get(ScheduleViewModel.class); booksViewModel = new ViewModelProvider(this).get(BooksViewModel.class); @@ -126,6 +128,7 @@ int month; int day; + String modo; public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); @@ -139,29 +142,96 @@ todoList = new ArrayList<>(); scheduleList = new ArrayList<>(); + int todoWidth = 140; + int scheduleWidth = 170; + int modoButtonWidth = 180; + int wholeButtonWidth = 370; + 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(todoWidth)); + //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(scheduleWidth)); + + //ボタン処理 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); + View verticalLine = view.findViewById(R.id.vertical_line); // IDを指定して取得 + + //上のTodoButtonを押したときTodoだけを表示 + modoTodoButton.setOnClickListener(v -> { + ViewGroup.LayoutParams todoButtonParams = modoTodoButton.getLayoutParams(); + ViewGroup.LayoutParams todoRecyclerViewLayoutParams = todoRecyclerView.getLayoutParams(); + if(modo == "todoSchedule") { + modoScheduleButton.setVisibility(View.GONE); + scheduleRecyclerView.setVisibility(View.GONE); + todoButtonParams.width = dpToPx(wholeButtonWidth); // 幅を全体に + modoTodoButton.setLayoutParams(todoButtonParams); + todoRecyclerViewLayoutParams.width = dpToPx(wholeButtonWidth); + todoRecyclerView.setLayoutParams(todoRecyclerViewLayoutParams); + todoAdapter.setItemWidth(dpToPx(330)); + verticalLine.setVisibility(View.GONE); + modo = "todo"; + } else if(modo == "todo"){ + todoButtonParams.width = dpToPx(modoButtonWidth); // 幅を全体に + modoTodoButton.setLayoutParams(todoButtonParams); + todoRecyclerViewLayoutParams.width = dpToPx(modoButtonWidth); + todoRecyclerView.setLayoutParams(todoRecyclerViewLayoutParams); + modoScheduleButton.setVisibility(View.VISIBLE); + scheduleRecyclerView.setVisibility(View.VISIBLE); + todoAdapter.setItemWidth(dpToPx(todoWidth)); + verticalLine.setVisibility(View.VISIBLE); + modo = "todoSchedule"; + } + }); + + //上のScheduleButtonを押したときScheduleだけを表示 + modoScheduleButton.setOnClickListener(v -> { + ViewGroup.LayoutParams scheuleButtonParams = modoScheduleButton.getLayoutParams(); + ViewGroup.LayoutParams scheduleRecyclerViewLayoutParams = scheduleRecyclerView.getLayoutParams(); + if(modo == "todoSchedule") { + modoTodoButton.setVisibility(View.GONE); + todoRecyclerView.setVisibility(View.GONE); + scheuleButtonParams.width = dpToPx(wholeButtonWidth); // 幅を全体に + modoScheduleButton.setLayoutParams(scheuleButtonParams); + scheduleRecyclerViewLayoutParams.width = dpToPx(wholeButtonWidth); + scheduleRecyclerView.setLayoutParams(scheduleRecyclerViewLayoutParams); + scheduleAdapter.setItemWidth(dpToPx(360)); + verticalLine.setVisibility(View.GONE); + modo = "schedule"; + } else if(modo == "schedule"){ + scheuleButtonParams.width = dpToPx(modoButtonWidth); // 幅を全体に + modoScheduleButton.setLayoutParams(scheuleButtonParams); + scheduleRecyclerViewLayoutParams.width = dpToPx(modoButtonWidth); + scheduleRecyclerView.setLayoutParams(scheduleRecyclerViewLayoutParams); + modoTodoButton.setVisibility(View.VISIBLE); + todoRecyclerView.setVisibility(View.VISIBLE); + scheduleAdapter.setItemWidth(dpToPx(scheduleWidth)); + verticalLine.setVisibility(View.VISIBLE); + modo = "todoSchedule"; + } + }); + FloatingActionButton todoAddButton = view.findViewById(R.id.todo_add_button); todoAddButton.setOnClickListener(v -> { ((MainActivity) getActivity()).showFragment(new CreateTodoFragment(year, month, day)); @@ -172,7 +242,7 @@ ((MainActivity) getActivity()).showFragment(new CreateScheduleFragment(year, month, day)); }); - ImageButton prevBotton = view.findViewById(R.id.prev_day_botton); + ImageButton prevBotton = view.findViewById(R.id.prev_day_button); prevBotton.setOnClickListener(v -> { curDate = curDate.minusDays(1); year = curDate.getYear(); @@ -184,7 +254,7 @@ updateTodoSchedule(integerBookHashMap); }); - ImageButton nextBotton = view.findViewById(R.id.next_day_botton); + ImageButton nextBotton = view.findViewById(R.id.next_day_button); nextBotton.setOnClickListener(v -> { curDate = curDate.plusDays(1); year = curDate.getYear(); @@ -196,10 +266,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 +339,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 +358,7 @@ private HashMap idBookHashMap; private TodosViewModel todosViewModel; private Context context; + private int itemWidth; // アイテムの幅を保持 MyTodoshelfAdapter(List todos, HashMap idBookHashMap, TodosViewModel todosViewModel, Context context) { @@ -325,6 +398,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 +450,11 @@ }); } + public void setItemWidth(int width) { + this.itemWidth = width; + notifyDataSetChanged(); // 幅が変更されたことを通知 + } + static class MyTodoViewHolder extends RecyclerView.ViewHolder { Button todoButton; @@ -390,6 +473,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 +508,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 +600,10 @@ notifyDataSetChanged(); // 必要に応じてアダプターをリフレッシュ } + public void setItemWidth(int width) { + this.itemWidth = width; + notifyDataSetChanged(); // 幅が変更されたことを通知 + } @Override public int getItemCount() { return scheduleList.size();