Подгрузка данных RecyclerView | Kotlin
Помогите решить проблему, ибо у меня мозг не работает. Читал пост, но что-то ничего не получается.
Есть Adapter
class Adapter() : RecyclerView.Adapter<Adapter.ViewHolder>() {
var list: MutableList<Item> = mutableListOf<Item>()
var loader: Boolean = false
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
return ViewHolder(inflater, parent)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val i: Item = list[position]
holder.bind(i)
}
override fun getItemCount(): Int = list.size
fun set(c: MutableList<Item>) {
this.list.clear()
this.list.addAll(c)
notifyDataSetChanged()
}
class ViewHolder(inflater: LayoutInflater, parent: ViewGroup) : RecyclerView.ViewHolder(inflater.inflate(R.layout.b_y_m_a_l_r_item, parent, false)) {
private var image : ImageView? = null
private var meta : TextView? = null
private var title : TextView? = null
private var genres : TextView? = null
private var rating : TextView? = null
private var ch_counts : TextView? = null
init {
image = itemView.findViewById<ImageView>(R.id.b_y_m_a_l_r_i_image)
meta = itemView.findViewById<TextView>(R.id.b_y_m_a_l_r_i_meta)
title = itemView.findViewById<TextView>(R.id.b_y_m_a_l_r_i_title)
genres = itemView.findViewById<TextView>(R.id.b_y_m_a_l_r_i_genres)
rating = itemView.findViewById<TextView>(R.id.b_y_m_a_l_r_i_rating)
ch_counts = itemView.findViewById<TextView>(R.id.b_y_m_a_l_r_i_chCount)
}
fun bind(data: Item) {
meta?.text = data.meta
title?.text = data.title
genres?.text = data.genres
rating?.text = data.rating
ch_counts?.text = data.ch_counts
if(data.image != ""){
Picasso.get()
.load(data.image)
.placeholder(R.mipmap.image_placeholder)
.error(R.mipmap.no_internet)
.fit()
.networkPolicy(NetworkPolicy.OFFLINE)
.into(image, object: com.squareup.picasso.Callback {
override fun onSuccess() {
}
override fun onError(e_p: Exception?) {
//Try again online if cache failed
Picasso.get()
.load(data.image)
.error(R.mipmap.no_internet)
.into(image, object: com.squareup.picasso.Callback {
override fun onSuccess() {
}
override fun onError(e: Exception?) {
Log.v("Picasso", "Could not fetch image");
}
});
}
});
}
}
}
override fun onViewAttachedToWindow(holder: ViewHolder) {
super.onViewAttachedToWindow(holder)
val layoutPosition = holder.layoutPosition
loader = layoutPosition == itemCount-1
}
}
А во фрагменте я вызываю это:
GlobalScope.launch {
val page = 1;
vModel?.setPage(page)
vModel?.setUrl("https://#####?Book_page=")
vModel?.init()
val elements = vModel?.filterElements()
GlobalScope.launch(Dispatchers.Main) {
if (elements != null) {
dataAdapter!!.set(elements)
}
}
}
Вот ViewModel фрагмента:
private var URL : String? = null
private var doc: Document? = null
private var page = 0
fun init(){
try {
doc = Jsoup.connect(URL + page).get()
} catch (e: IOException) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
fun setPage(p : Int) { page = p }
fun setUrl(u : String){
URL = u
}
@RequiresApi(Build.VERSION_CODES.N)
fun filterElements() : MutableList<Item>{
val list = mutableListOf<Item>()
return list;
}
@Suppress("UNREACHABLE_CODE")
@RequiresApi(Build.VERSION_CODES.N)
fun filterTitle(data: String, symbol: Char) : String{
val index = data.indexOf(symbol)
if(index == - 1) {
return data
} else {
val result = ""
val elements = data.split(symbol)
for (el: String in elements){
val cyrillic: Boolean = el.chars()
.mapToObj(Character.UnicodeBlock::of)
.anyMatch { b -> b == Character.UnicodeBlock.CYRILLIC }
if (cyrillic) return el.trim();
}
return data
}
return data
}
Так как мне сделать подгрузку контента? У меня срабатывает onViewAttachedToWindow и loader меняется на true, но как мне подгрузить контент?