오늘 배운 것
- TODO프로젝트 이용해서 TODO App만들기(7)
바텀바 메뉴 완성하기
- 할 일 목록들을 recyclerView를 이용해 만들기
- recyclerView를 관리하기위한 Adapter가 필요함
- (시간관계상 LocalDate 변환오류, null 에러 해결 못 함)
내 할일 프래그먼트 만들기
- 할 일 데이터들을 아이디를 이용해 서버에서 가져오고 생성한 Adapter 클래스에 보냄
package com.example.todo.fragment.todo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.todo.R;
import com.example.todo.domain.Todo;
import com.example.todo.domain.TodoListResult;
import com.example.todo.domain.User;
import com.example.todo.retrofit.RetrofitClient;
import com.example.todo.retrofit.TodoService;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class TodoMeFragment extends Fragment {
public static final String TAG = "TodoMeFragment";
private TextView tvTodoMeMsg;
private TodoMeAdapter adapter;
private String loginId;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_todo_me,container, false );
Bundle bundle = getArguments();
loginId = bundle.getString("loginId");
tvTodoMeMsg = view.findViewById(R.id.tvTodoMeMsg);
RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
adapter = new TodoMeAdapter();
LinearLayoutManager linearLayoutManager
= new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(adapter);
requestGetMyTodos(loginId);
return view;
}
private void requestGetMyTodos(String loginId) {
TodoService todoService = RetrofitClient.getTodoService();
Call<TodoListResult> call = todoService.getTodoByUserName("me",loginId);
call.enqueue(new Callback<TodoListResult>() {
@Override
public void onResponse(Call<TodoListResult> call, Response<TodoListResult> response) {
if (!response.isSuccessful()) {
Log.d(TAG, "onResponse : " + response.message());
return;
}
TodoListResult todoListResult = response.body();
setTodosAndShow(todoListResult);
}
@Override
public void onFailure(Call<TodoListResult> call, Throwable t) {
Log.d(TAG, "onFailure : " + t.getMessage());
}
});
}
private void setTodosAndShow(TodoListResult todoListResult) {
if (!todoListResult.isHasResult()) {
tvTodoMeMsg.setVisibility(View.VISIBLE);
return;
}
tvTodoMeMsg.setVisibility(View.GONE);
User user = todoListResult.getUser();
List<Todo> todoList = user.getTodolist();
if (todoList == null || todoList.size() == 0) {
return;
}
adapter.setItems(todoList);
adapter.notifyDataSetChanged();
}
}
Adapter 클래스
- Adapter에서 recyclerView를 관리함
- recyclerView는 화면에 표현되는 객체의 갯수를 자동으로 체크하고 일정 숫자의 객체를 이용하여 데이터를 출력 함으로 써 자원소비량을 낮춤
- 순서정보를 가지고 있기 때문에 각각의 item에 접근하는 것도 가능함
package com.example.todo.fragment.todo;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.todo.R;
import com.example.todo.domain.Todo;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
public class TodoMeAdapter extends RecyclerView.Adapter<TodoMeAdapter.ViewHolder>{
public static final String TAG = "TodoMeAdapter";
private List<Todo> items = new ArrayList<>();
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
LayoutInflater layoutInflater = LayoutInflater.from(context);
View itemView = layoutInflater.inflate(R.layout.todo_item, viewGroup, false);
ViewHolder viewHolder = new ViewHolder(itemView);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int position) {
Todo item = items.get(position);
viewHolder.setItem(item);
}
@Override
public int getItemCount() {
return items.size();
}
public void addItem(Todo item) {
items.add(item);
}
public Todo getItem(int position) {
return items.get(position);
}
public void setItems(List<Todo> items) {
this.items = items;
}
public void setItem(int position, Todo item) {
items.set(position, item);
}
class ViewHolder extends RecyclerView.ViewHolder {
private ImageView img1;
private TextView tvTitle, tvtvTagetDate, tvtvDone;
public ViewHolder(@NonNull View itemView) {
super(itemView);
img1 = itemView.findViewById(R.id.img1);
tvTitle = itemView.findViewById(R.id.tvTitle);
tvtvTagetDate = itemView.findViewById(R.id.tvTagetDate);
tvtvDone = itemView.findViewById(R.id.tvDone);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
Todo todo = items.get(position);
Toast.makeText(view.getContext(), "선택 아이템 : " + todo.toString(), Toast.LENGTH_SHORT).show();
}
});
}
public void setItem(Todo item) {
if (item == null) {
return;
}
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy년 MM월 dd일");
LocalDate localDate = item.getTargetDate();
String strDate = dateTimeFormatter.format(localDate);
Log.d(TAG,localDate.toString());
Log.d(TAG,strDate);
tvTitle.setText(item.getTitle());
tvtvTagetDate.setText(strDate);
tvtvDone.setText(item.isStatus() ? "완료" : "진행중");
}
}
}
RetorfitClient 클래스
- 할 일 데이터들을 받아올 Service인터페이스를 만들어 Retrofit을 준비함
public static TodoService getTodoService() {
TodoService todoService = getInstance().create(TodoService.class);
return todoService;
}
MainActivity에서 Bottom메뉴
- Bottom 메뉴 선택 시 Bundle 객체에 아이디 정보를 담아 프래그먼트 전환 시 정보를 같이 넘김
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navi);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()) {
case R.id.bottom_menu_mytodo:
Log.d(TAG, "내 할일 메뉴 선택됨");
Bundle bundle = new Bundle();
bundle.putString("loginId", loginId);
onFragmentChanged(TODO_ME, bundle); break;
case R.id.bottom_menu_alltodo: Log.d(TAG, "모든 할일 메뉴 선택됨"); break;
case R.id.bottom_menu_chart: Log.d(TAG, "차트 메뉴 선택됨"); break;
}
return false;
}