Я новичок в android. Я пытаюсь разработать приложение для чтения электронных книг. Я реализовал вложенные виды рециркуляции, чтобы получить желаемый дизайн, который выглядит следующим образом:
Я хочу реализовать ClickListeners для этого дизайна, так что если заголовок раздел (например: «Категория 1») или кнопка со стрелкой перед ним, после чего он отправляет пользователя в действие, показывающее книги этой категории. А если щелкнуть элемент представления «Переработка дочерних элементов», обложку книги, ее название или что-либо в этом макете карты, то пользователь отправляется в действие, показывающее описание этой книги и другие параметры, такие как чтение, добавление в библиотеку и т. Д. c. Как мне этого достичь?
Мой код:
Книга:
public class Book {
private int uniqueId;
private String bookTitle;
private String bookAuthor;
// private String bookCoverImageURL;
private int readPercentage;
private int views;
private int bookCoverImage;
private String bookSummary;
private String publishedDate;
private String readingTime;
public Book(int bookCoverImage, String bookTitle, String readingTime, int views) {
this.bookCoverImage = bookCoverImage;
this.bookTitle = bookTitle;
this.readingTime =readingTime;
this.views= views;
}
public String getBookTitle() {
return bookTitle;
}
public String getBookAuthor() {
return bookAuthor;
}
// public String getBookCoverImageURL() {
//// return bookCoverImageURL;
//// }
public int getReadPercentage() {
return readPercentage;
}
public int getBookCoverImage() {
return bookCoverImage;
}
public int getUniqueId() {
return uniqueId;
}
public String getBookSummary() {
return bookSummary;
}
public String getPublishedDate() {
return publishedDate;
}
public String getReadingTime() {
return readingTime;
}
public int getViews() {
return views;
}
public void setViews(int views) {
this.views = views;
}
public void setUniqueId(int uniqueId) {
this.uniqueId = uniqueId;
}
public void setBookTitle(String bookTitle) {
this.bookTitle = bookTitle;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public void setReadPercentage(int readPercentage) {
this.readPercentage = readPercentage;
}
public void setBookCoverImage(int bookCoverImage) {
this.bookCoverImage = bookCoverImage;
}
public void setBookSummary(String bookSummary) {
this.bookSummary = bookSummary;
}
public void setPublishedDate(String publishedDate) {
this.publishedDate = publishedDate;
}
public void setReadingTime(String readingTime) {
this.readingTime = readingTime;
}
}
Категория Раздел:
public class CategorySection {
private String categoryTitle;
private ArrayList<Book> mBookList = new ArrayList<>();
public String getCategoryTitle() {
return categoryTitle;
}
public void setCategoryTitle(String categoryTitle) {
this.categoryTitle = categoryTitle;
}
public ArrayList<Book> getmBookList() {
return mBookList;
}
public void setmBookList(ArrayList<Book> mBookList) {
this.mBookList = mBookList;
}
Домашняя страницаКатегорияКнигиКарта:
public class HomeCategorySectionBooksAdapter extends RecyclerView.Adapter<HomeCategorySectionBooksAdapter.BookViewHolder> {
private ArrayList<Book> mBookList = new ArrayList<>();
public HomeCategorySectionBooksAdapter(ArrayList<Book> mBookList) {
this.mBookList = mBookList;
}
@NonNull
@Override
public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_category_section_book_card, parent, false);
return new BookViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull BookViewHolder holder, int position) {
Book currentBook = mBookList.get(position);
String title = currentBook.getBookTitle();
String readTime = currentBook.getReadingTime();
int nofviews = currentBook.getViews();
holder.bookCoverImage.setImageResource(currentBook.getBookCoverImage());
holder.bookTitle.setText(title);
holder.readingTime.setText(readTime);
holder.views.setText(nofviews+" views");
}
@Override
public int getItemCount() {
return mBookList.size();
}
public class BookViewHolder extends RecyclerView.ViewHolder {
public ImageView bookCoverImage;
public TextView bookTitle;
public TextView readingTime;
public TextView views;
public BookViewHolder(@NonNull View itemView) {
super(itemView);
bookCoverImage = itemView.findViewById(R.id.section_card_book_image_view);
bookTitle = itemView.findViewById(R.id.section_card_book_title);
readingTime = itemView.findViewById(R.id.section_card_book_reading_time);
views = itemView.findViewById(R.id.section_card_book_views);
}
}
}
HomeCategorySectionAdapter:
public class HomeCategorySectionsAdapter extends RecyclerView.Adapter<HomeCategorySectionsAdapter.CategorySectionViewHolder> {
private Context mContext;
private ArrayList<CategorySection> mSectionList = new ArrayList<>();
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
public HomeCategorySectionsAdapter(ArrayList<CategorySection> mSectionList) {
this.mSectionList = mSectionList;
}
@NonNull
@Override
public CategorySectionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.home_category_section_card, parent, false);
return new CategorySectionViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull CategorySectionViewHolder holder, int position) {
CategorySection currentSection = mSectionList.get(position);
String sectionTitle = currentSection.getCategoryTitle();
holder.categoryTitle.setText(sectionTitle);
LinearLayoutManager layoutManager = new LinearLayoutManager(holder.booksRecyclerView.getContext(),LinearLayoutManager.HORIZONTAL,false);
layoutManager.setInitialPrefetchItemCount(currentSection.getmBookList().size());
HomeCategorySectionBooksAdapter booksAdapter = new HomeCategorySectionBooksAdapter(mSectionList.get(position).getmBookList());
holder.booksRecyclerView.setLayoutManager(layoutManager);
holder.booksRecyclerView.setAdapter(booksAdapter);
holder.booksRecyclerView.setRecycledViewPool(viewPool);
}
@Override
public int getItemCount() {
return mSectionList.size();
}
public class CategorySectionViewHolder extends RecyclerView.ViewHolder {
public TextView categoryTitle;
public RecyclerView booksRecyclerView;
public CategorySectionViewHolder(@NonNull View itemView) {
super(itemView);
categoryTitle = itemView.findViewById(R.id.home_category_section_title);
booksRecyclerView = itemView.findViewById(R.id.home_category_section_books_recyclerview);
}
}
}
HomeFragment:
public class HomeFragment extends Fragment {
private RecyclerView recyclerView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = view.findViewById(R.id.home_sections_recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL,false);
HomeCategorySectionsAdapter sectionsAdapter = new HomeCategorySectionsAdapter(buildSectionList());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(sectionsAdapter);
return view;
}
private ArrayList<CategorySection> buildSectionList() {
ArrayList<CategorySection> sectionList = new ArrayList<>();
for(int i=0;i<6;i++){
CategorySection categorySection = new CategorySection();
categorySection.setCategoryTitle("Category "+(i+1));
categorySection.setmBookList(buildBookList());
sectionList.add(categorySection);
}
return sectionList;
}
private ArrayList<Book> buildBookList() {
ArrayList<Book> bookList = new ArrayList<>();
for (int i=0;i<6;i++){
Book book = new Book(R.drawable.the_poppy_wife,"Title "+(i+1),(10+1)+" minutes",i*10+2*i);
bookList.add(book);
}
return bookList;
}
}