From 304c0991519ce84a9c10b8af40dcaa05aaa0b318 Mon Sep 17 00:00:00 2001 From: GKF Date: Mon, 10 Jun 2019 12:31:05 +0800 Subject: [PATCH] up --- .../app/ui/main/bookshelf/BookshelfAdapter.kt | 38 ++++++++++++++++--- .../ui/main/bookshelf/BookshelfFragment.kt | 2 + .../res/layout/item_bookshelf_list_add.xml | 7 ++++ 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 app/src/main/res/layout/item_bookshelf_list_add.xml diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt index e2cf5728f..eae2f0aec 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt @@ -16,7 +16,7 @@ import kotlinx.android.synthetic.main.item_bookshelf_list.view.* import kotlinx.android.synthetic.main.item_relace_rule.view.tv_name import java.io.File -class BookshelfAdapter : PagedListAdapter(DIFF_CALLBACK) { +class BookshelfAdapter : PagedListAdapter(DIFF_CALLBACK) { companion object { @JvmField @@ -33,13 +33,40 @@ class BookshelfAdapter : PagedListAdapter(D var callBack: CallBack? = null - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { + override fun getItemViewType(position: Int): Int { + if (position == itemCount - 1) { + return 1 + } + return super.getItemViewType(position) + } + + override fun getItemCount(): Int { + return super.getItemCount() + 1 + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + if (viewType == 1) { + return AddViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_bookshelf_list_add, parent, false)) + } return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_bookshelf_list, parent, false)) } - override fun onBindViewHolder(holder: MyViewHolder, position: Int) { - currentList?.get(position)?.let { - holder.bind(it, callBack) + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + when (holder) { + is MyViewHolder -> { + currentList?.get(position)?.let { + holder.bind(it, callBack) + } + } + is AddViewHolder -> holder.bind(callBack) + } + + } + + class AddViewHolder(view: View) : RecyclerView.ViewHolder(view) { + + fun bind(callBack: CallBack?) = with(itemView) { + setOnClickListener { callBack?.search() } } } @@ -76,5 +103,6 @@ class BookshelfAdapter : PagedListAdapter(D interface CallBack { fun open(book: Book) + fun search() } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt index 8d83af662..49a74f6c0 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.paging.LivePagedListBuilder import androidx.paging.PagedList +import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import io.legado.app.App import io.legado.app.R @@ -42,6 +43,7 @@ class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf) { bookGroupAdapter = BookGroupAdapter() rv_book_group.adapter = bookGroupAdapter rv_bookshelf.layoutManager = LinearLayoutManager(context) + rv_bookshelf.addItemDecoration(DividerItemDecoration(rv_bookshelf.context, LinearLayoutManager.VERTICAL)) bookshelfAdapter = BookshelfAdapter() rv_bookshelf.adapter = bookshelfAdapter } diff --git a/app/src/main/res/layout/item_bookshelf_list_add.xml b/app/src/main/res/layout/item_bookshelf_list_add.xml new file mode 100644 index 000000000..50282f4a8 --- /dev/null +++ b/app/src/main/res/layout/item_bookshelf_list_add.xml @@ -0,0 +1,7 @@ + + \ No newline at end of file