pull/32/head
parent
eda339e599
commit
bce9c17fad
@ -0,0 +1,91 @@ |
||||
package io.legado.app.ui.main.explore |
||||
|
||||
import android.content.Context |
||||
import android.graphics.drawable.GradientDrawable |
||||
import android.view.LayoutInflater |
||||
import android.widget.GridLayout |
||||
import io.legado.app.R |
||||
import io.legado.app.base.adapter.ItemViewHolder |
||||
import io.legado.app.base.adapter.SimpleRecyclerAdapter |
||||
import io.legado.app.data.entities.BookSource |
||||
import io.legado.app.help.coroutine.Coroutine |
||||
import io.legado.app.lib.theme.ColorUtils |
||||
import io.legado.app.lib.theme.accentColor |
||||
import io.legado.app.utils.gone |
||||
import io.legado.app.utils.visible |
||||
import kotlinx.android.synthetic.main.item_find_book.view.* |
||||
import kotlinx.android.synthetic.main.item_text.view.* |
||||
import kotlinx.coroutines.CoroutineScope |
||||
import org.jetbrains.anko.sdk27.listeners.onClick |
||||
|
||||
|
||||
class FindBookAdapter(context: Context, private val scope: CoroutineScope, val callBack: CallBack) : |
||||
SimpleRecyclerAdapter<BookSource>(context, R.layout.item_find_book) { |
||||
|
||||
var exIndex = 0 |
||||
|
||||
override fun convert(holder: ItemViewHolder, item: BookSource, payloads: MutableList<Any>) { |
||||
with(holder.itemView) { |
||||
val bgShape: GradientDrawable? = tv_name.background as? GradientDrawable |
||||
bgShape?.setStroke(2, ColorUtils.getRandomColor()) |
||||
tv_name.text = item.bookSourceName |
||||
ll_title.onClick { |
||||
val oldEx = exIndex |
||||
if (exIndex == holder.layoutPosition) { |
||||
exIndex = -1 |
||||
} else { |
||||
exIndex = holder.layoutPosition |
||||
notifyItemChanged(holder.layoutPosition) |
||||
} |
||||
notifyItemChanged(oldEx) |
||||
callBack.scrollTo(holder.layoutPosition) |
||||
} |
||||
if (exIndex == holder.layoutPosition) { |
||||
rotate_loading.loadingColor = context.accentColor |
||||
rotate_loading.show() |
||||
Coroutine.async(scope) { |
||||
item.getExploreRule().getExploreKinds(item.bookSourceUrl) |
||||
}.onSuccess { |
||||
it?.let { |
||||
gl_child.visible() |
||||
var rowNum = 0 |
||||
var columnNum = 0 |
||||
gl_child.removeAllViews() |
||||
it.map { kind -> |
||||
val tv = LayoutInflater.from(context) |
||||
.inflate(R.layout.item_text, gl_child, false) |
||||
tv.text_view.text = kind.title |
||||
tv.text_view.onClick { |
||||
callBack.openExplore( |
||||
item.bookSourceUrl, |
||||
kind.title, |
||||
kind.url |
||||
) |
||||
} |
||||
val rowSpecs = GridLayout.spec(rowNum, 1.0f) |
||||
val colSpecs = GridLayout.spec(columnNum, 1.0f) |
||||
val params = GridLayout.LayoutParams(rowSpecs, colSpecs) |
||||
gl_child.addView(tv, params) |
||||
if (columnNum < 2) { |
||||
columnNum++ |
||||
} else { |
||||
columnNum = 0 |
||||
rowNum++ |
||||
} |
||||
} |
||||
} |
||||
}.onFinally { |
||||
rotate_loading.hide() |
||||
} |
||||
} else { |
||||
rotate_loading.hide() |
||||
gl_child.gone() |
||||
} |
||||
} |
||||
} |
||||
|
||||
interface CallBack { |
||||
fun scrollTo(pos: Int) |
||||
fun openExplore(sourceUrl: String, title: String, exploreUrl: String) |
||||
} |
||||
} |
@ -1,114 +0,0 @@ |
||||
package io.legado.app.ui.main.findbook |
||||
|
||||
import android.graphics.drawable.GradientDrawable |
||||
import android.view.LayoutInflater |
||||
import android.view.View |
||||
import android.view.ViewGroup |
||||
import android.widget.GridLayout |
||||
import androidx.paging.PagedListAdapter |
||||
import androidx.recyclerview.widget.DiffUtil |
||||
import androidx.recyclerview.widget.RecyclerView |
||||
import io.legado.app.R |
||||
import io.legado.app.data.entities.BookSource |
||||
import io.legado.app.help.coroutine.Coroutine |
||||
import io.legado.app.lib.theme.ColorUtils |
||||
import io.legado.app.utils.gone |
||||
import io.legado.app.utils.visible |
||||
import kotlinx.android.synthetic.main.item_find_book.view.* |
||||
import kotlinx.android.synthetic.main.item_text.view.* |
||||
import kotlinx.coroutines.CoroutineScope |
||||
import org.jetbrains.anko.sdk27.listeners.onClick |
||||
|
||||
|
||||
class FindBookAdapter(private val scope: CoroutineScope, val callBack: CallBack) : |
||||
PagedListAdapter<BookSource, FindBookAdapter.MyViewHolder>(DIFF_CALLBACK) { |
||||
|
||||
var exIndex = 0 |
||||
|
||||
companion object { |
||||
var DIFF_CALLBACK = object : DiffUtil.ItemCallback<BookSource>() { |
||||
override fun areItemsTheSame(oldItem: BookSource, newItem: BookSource): Boolean = |
||||
oldItem.bookSourceUrl == newItem.bookSourceUrl |
||||
|
||||
override fun areContentsTheSame(oldItem: BookSource, newItem: BookSource): Boolean = |
||||
oldItem.bookSourceUrl == newItem.bookSourceUrl |
||||
&& oldItem.bookSourceName == newItem.bookSourceName |
||||
} |
||||
} |
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder { |
||||
return MyViewHolder( |
||||
LayoutInflater.from(parent.context) |
||||
.inflate(R.layout.item_find_book, parent, false) |
||||
) |
||||
} |
||||
|
||||
override fun onBindViewHolder(holder: MyViewHolder, position: Int): Unit = |
||||
with(holder.itemView) { |
||||
currentList?.get(position)?.let { bookSource -> |
||||
val bgShape: GradientDrawable? = tv_name.background as? GradientDrawable |
||||
bgShape?.setStroke(2, ColorUtils.getRandomColor()) |
||||
tv_name.text = bookSource.bookSourceName |
||||
ll_title.onClick { |
||||
val oldEx = exIndex |
||||
if (exIndex == position) { |
||||
exIndex = -1 |
||||
} else { |
||||
exIndex = position |
||||
notifyItemChanged(position) |
||||
} |
||||
notifyItemChanged(oldEx) |
||||
callBack.scrollTo(position) |
||||
} |
||||
if (exIndex == position) { |
||||
rotate_loading.show() |
||||
Coroutine.async(scope) { |
||||
bookSource.getExploreRule().getExploreKinds(bookSource.bookSourceUrl) |
||||
}.onSuccess { |
||||
it?.let { |
||||
gl_child.visible() |
||||
var rowNum = 0 |
||||
var columnNum = 0 |
||||
gl_child.removeAllViews() |
||||
it.map { kind -> |
||||
val tv = LayoutInflater.from(context) |
||||
.inflate(R.layout.item_text, gl_child, false) |
||||
tv.text_view.text = kind.title |
||||
tv.text_view.onClick { |
||||
callBack.openExplore( |
||||
bookSource.bookSourceUrl, |
||||
kind.title, |
||||
kind.url |
||||
) |
||||
} |
||||
val rowSpecs = GridLayout.spec(rowNum, 1.0f) |
||||
val colSpecs = GridLayout.spec(columnNum, 1.0f) |
||||
val params = GridLayout.LayoutParams(rowSpecs, colSpecs) |
||||
gl_child.addView(tv, params) |
||||
if (columnNum < 2) { |
||||
columnNum++ |
||||
} else { |
||||
columnNum = 0 |
||||
rowNum++ |
||||
} |
||||
} |
||||
} |
||||
}.onFinally { |
||||
rotate_loading.hide() |
||||
} |
||||
} else { |
||||
rotate_loading.hide() |
||||
gl_child.gone() |
||||
} |
||||
} |
||||
} |
||||
|
||||
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) |
||||
|
||||
interface CallBack { |
||||
|
||||
fun scrollTo(pos: Int) |
||||
|
||||
fun openExplore(sourceUrl: String, title: String, exploreUrl: String) |
||||
} |
||||
} |
Loading…
Reference in new issue