pull/32/head
GKF 6 years ago
parent 3101a90393
commit 304c099151
  1. 34
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfAdapter.kt
  2. 2
      app/src/main/java/io/legado/app/ui/main/bookshelf/BookshelfFragment.kt
  3. 7
      app/src/main/res/layout/item_bookshelf_list_add.xml

@ -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 kotlinx.android.synthetic.main.item_relace_rule.view.tv_name
import java.io.File import java.io.File
class BookshelfAdapter : PagedListAdapter<Book, BookshelfAdapter.MyViewHolder>(DIFF_CALLBACK) { class BookshelfAdapter : PagedListAdapter<Book, RecyclerView.ViewHolder>(DIFF_CALLBACK) {
companion object { companion object {
@JvmField @JvmField
@ -33,15 +33,42 @@ class BookshelfAdapter : PagedListAdapter<Book, BookshelfAdapter.MyViewHolder>(D
var callBack: CallBack? = null 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)) return MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_bookshelf_list, parent, false))
} }
override fun onBindViewHolder(holder: MyViewHolder, position: Int) { override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder) {
is MyViewHolder -> {
currentList?.get(position)?.let { currentList?.get(position)?.let {
holder.bind(it, callBack) 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() }
}
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) { class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
@ -76,5 +103,6 @@ class BookshelfAdapter : PagedListAdapter<Book, BookshelfAdapter.MyViewHolder>(D
interface CallBack { interface CallBack {
fun open(book: Book) fun open(book: Book)
fun search()
} }
} }

@ -7,6 +7,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList import androidx.paging.PagedList
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
@ -42,6 +43,7 @@ class BookshelfFragment : BaseFragment(R.layout.fragment_bookshelf) {
bookGroupAdapter = BookGroupAdapter() bookGroupAdapter = BookGroupAdapter()
rv_book_group.adapter = bookGroupAdapter rv_book_group.adapter = bookGroupAdapter
rv_bookshelf.layoutManager = LinearLayoutManager(context) rv_bookshelf.layoutManager = LinearLayoutManager(context)
rv_bookshelf.addItemDecoration(DividerItemDecoration(rv_bookshelf.context, LinearLayoutManager.VERTICAL))
bookshelfAdapter = BookshelfAdapter() bookshelfAdapter = BookshelfAdapter()
rv_bookshelf.adapter = bookshelfAdapter rv_bookshelf.adapter = bookshelfAdapter
} }

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:text="@string/add"
android:textSize="24sp" />
Loading…
Cancel
Save