2024-04-25 10:33:59 +08:00
|
|
|
package com.zjgsu.jianshu.Adapter
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.ImageView
|
2024-04-26 19:57:44 +08:00
|
|
|
import android.widget.LinearLayout
|
2024-04-25 10:33:59 +08:00
|
|
|
import android.widget.TextView
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import com.bumptech.glide.Glide
|
2024-04-26 19:57:44 +08:00
|
|
|
import com.zjgsu.jianshu.*
|
2024-04-25 10:33:59 +08:00
|
|
|
|
2024-04-30 09:41:24 +08:00
|
|
|
class BookAdapter2(val bookList: List<Book>,val categoryId:Int) : RecyclerView.Adapter<BookAdapter2.ViewHolder>() {
|
2024-04-25 10:33:59 +08:00
|
|
|
|
|
|
|
inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
|
|
|
val bookList_Image: ImageView = view.findViewById(R.id.book_Image)
|
|
|
|
val bookList_Name: TextView = view.findViewById(R.id.book_Name)
|
|
|
|
val bookList_Introduce: TextView = view.findViewById(R.id.book_Introduce)
|
2024-04-26 19:57:44 +08:00
|
|
|
val outerLinearLayout: LinearLayout =view.findViewById(R.id.bookItem_outerLinearLayout)
|
2024-04-25 10:33:59 +08:00
|
|
|
}
|
|
|
|
|
2024-04-26 19:57:44 +08:00
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BookAdapter2.ViewHolder {
|
|
|
|
val view = LayoutInflater.from(parent.context).inflate(R.layout.book_item, parent, false)
|
2024-04-25 10:33:59 +08:00
|
|
|
return ViewHolder(view)
|
|
|
|
}
|
|
|
|
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
|
|
|
val book = bookList[position]
|
|
|
|
holder.bookList_Name.text = book.name
|
|
|
|
holder.bookList_Introduce.text = book.introduce
|
2024-04-26 19:57:44 +08:00
|
|
|
Glide.with(holder.itemView.context)
|
|
|
|
.load(book.picUrl) // 确保你的Book对象有正确的图片URL
|
|
|
|
.placeholder(R.drawable.pre_load) // 可以设置一个占位图
|
|
|
|
.error(R.drawable.fail_load) // 设置加载失败的图
|
|
|
|
.into(holder.bookList_Image)
|
2024-05-02 10:25:50 +08:00
|
|
|
holder.outerLinearLayout.setOnClickListener {
|
2024-04-27 14:00:50 +08:00
|
|
|
val intent = Intent(holder.itemView.context, BookInformationActivity::class.java)
|
2024-04-25 10:33:59 +08:00
|
|
|
intent.putExtra("Book_name", book.name)
|
2024-05-02 10:25:50 +08:00
|
|
|
val sourceActivity = when (categoryId) {
|
|
|
|
1 -> LiteratureActivity::class.java
|
|
|
|
2 -> ManagementActivity::class.java
|
|
|
|
3 -> PsychologyActivity::class.java
|
|
|
|
4 -> ScienceActivity::class.java
|
|
|
|
5 -> PhilosophyActivity::class.java
|
|
|
|
else -> null
|
|
|
|
}
|
|
|
|
intent.putExtra("Source_Activity", sourceActivity)
|
2024-04-25 10:33:59 +08:00
|
|
|
ContextCompat.startActivity(holder.itemView.context, intent, null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun getItemCount() = bookList.size
|
|
|
|
}
|