MyBookShelfある程度完成しました
1 parent 74e9713 commit 7d7bb1d6d3f23ac41dccd2f4ad31ce7880c93e37
k-sakoda authored on 25 Jun
Showing 2 changed files
View
21
app/src/main/java/com/example/citrusclient/views/MyBookshelfFragment.java
String token = citrus.getToken();
String accountId = citrus.getAccountId();
 
bookList = new ArrayList<>();
bookList.add(new Book("a", 1, "a", true, "0"));
bookList.add(new Book("a", 1, "a", true, "#ff0000"));
 
RecyclerView recyclerView = view.findViewById(R.id.my_books_list);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(view.getContext(), 2);
//new LinearLayoutManager(view.getContext());
recyclerView.setLayoutManager(layoutManager);
RecyclerView.Adapter bookAdapter = new MyBookshelfAdapter(bookList);
MyBookshelfAdapter bookAdapter = new MyBookshelfAdapter(bookList);
recyclerView.setAdapter(bookAdapter);
 
FloatingActionButton addButton = view.findViewById(R.id.book_add_button);
addButton.setOnClickListener(v -> {
booksViewModel.getBookLiveData().observe(getViewLifecycleOwner(), new Observer<HashMap<Integer, Book>>() {
@Override
public void onChanged(HashMap<Integer, Book> integerBookHashMap) {
bookList = new ArrayList<>(integerBookHashMap.values());
bookAdapter.notifyDataSetChanged();
bookAdapter.setBooks(bookList);
}
});
 
booksViewModel.loadBooks("bird", "xyz");
MyBookshelfAdapter(List<Book> book){
this.bookList = book;
}
 
public void setBooks(List<Book> books){
bookList = books;
notifyDataSetChanged();
}
 
@NonNull
@Override
public MyBookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_book, parent, false);
@Override
public void onBindViewHolder(@NonNull MyBookViewHolder holder, int position) {
Book bookData = this.bookList.get(position);
holder.bookButton.setText(bookData.getTitle());
holder.bookButton.setBackgroundColor(Color.RED);
int red = Integer.parseInt(bookData.getColor().substring(1, 3), 16);
int green = Integer.parseInt(bookData.getColor().substring(3, 5), 16);
int blue = Integer.parseInt(bookData.getColor().substring(5, 7), 16);
holder.bookButton.setBackgroundColor(Color.rgb(red, green, blue));
holder.bookButton.setTextColor(Color.rgb(255 - red, 255 - green, 255 - blue));
}
 
@Override
public int getItemCount() {
View
1
■■■■
app/src/main/res/layout/a_book.xml
android:id="@+id/book_button"
android:layout_width="198dp"
android:layout_height="224dp"
android:text="Button"
android:textColor="#000000"
app:cornerRadius="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"