gpt4 book ai didi

android - 使用 Glide 在 Recyclerview 中不显示图像

转载 作者:搜寻专家 更新时间:2023-11-01 09:32:24 27 4
gpt4 key购买 nike

我有一个使用 recyclerview 的帖子列表。列表很棒,但是当我尝试在 recyclerview 中添加图像时出现问题。我正在使用 Glide 添加图像。我已经在 Navigation Drawer 中使用 glide 成功添加了图像,但是当我在 Recyclerview 中尝试时,它只显示我获取的默认图像。

这是我的 Kotlin

class InformationListActivity : MainActivity() {
private val glide: RequestManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_information_list)

val toolbar = findViewById<View>(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)
supportActionBar!!.setDisplayShowHomeEnabled(true)

val user = FirebaseAuth.getInstance().currentUser
val uName = user?.displayName
val uEmail = user?.email
val uPhotoUrl = user?.photoUrl
user?.uid

val navigationView = findViewById<View>(R.id.nav_view) as NavigationView
navigationView.setNavigationItemSelectedListener(this)
val headerView = navigationView.getHeaderView(0)

val navName = headerView.findViewById<TextView>(R.id.navName)
navName.text = uName
val navEmail = headerView.findViewById<TextView>(R.id.navEmail)
navEmail.text = uEmail
val navImage = headerView.findViewById<ImageView>(R.id.navImage)
Glide.with(this).load(uPhotoUrl).into(navImage)

window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

val signOut = findViewById<View>(R.id.btnSignOut) as LinearLayout
signOut.setOnClickListener {
FirebaseAuth.getInstance().signOut()
val goToLogin = Intent(this@InformationListActivity, LoginActivity::class.java)
startActivity(goToLogin)
}


val recyclerView = findViewById<View>(R.id.recycler_view) as RecyclerView
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(this)
val myref = FirebaseDatabase.getInstance().reference.child("/posts")
val recyclerAdapter = object : FirebaseRecyclerAdapter<Post, InformationListActivity.PostViewHolder>(
Post::class.java,
R.layout.information_model,
InformationListActivity.PostViewHolder::class.java,
myref
) {
override fun populateViewHolder(viewHolder: InformationListActivity.PostViewHolder, model: Post, position: Int) {
val data_empty = findViewById<View>(R.id.data_empty) as TextView
data_empty.visibility = View.GONE
viewHolder.run {
glide?.let { setmPostImageUrl(it,model.getmPostImageUrl()) }
setmPostTitle(model.getmPostTitle())
setmPostDescription(model.getmPostDescription())
viewHolder.setItemClickListener(object : ItemClickListener {
override fun onItemClick(pos: Int) {
//OPEN DETAIL ACTIVITY
openInformationDetail(model.getmPostImageUrl(), model.getmPostTitle(), model.getmPostDescription())
}
})
}
}
}
recyclerView.adapter = recyclerAdapter
}

private fun openInformationDetail(vararg details: String) {
val i = Intent(this, InformationDetailActivity::class.java)
i.putExtra("POSTIMAGEURL_KEY", details[0])
i.putExtra("POSTTITLE_KEY", details[1])
i.putExtra("POSTDESCRIPTION_KEY", details[2])
startActivity(i)
}

class PostViewHolder(mView: View) : RecyclerView.ViewHolder(mView), View.OnClickListener {
private var modelImgPost: ImageView = mView.findViewById(R.id.imgPost)
private var modelPostTitle: TextView = mView.findViewById(R.id.modelPostTitle)
private var modelPostDescription: TextView = mView.findViewById(R.id.modelPostDescription)
private var btnMore: Button = mView.findViewById(R.id.btnMore)
private lateinit var itemClickListener: ItemClickListener

init {
btnMore.setOnClickListener(this)
}

fun setItemClickListener(itemClickListener: ItemClickListener) {
this.itemClickListener = itemClickListener
}

override fun onClick(view: View) {
this.itemClickListener.onItemClick(this.layoutPosition)
}

fun setmPostImageUrl(glide: RequestManager, mPostImageUrl: String) {
glide.load(Uri.parse(mPostImageUrl)).into(modelImgPost)
}

fun setmPostTitle(mPostTitle: String) {
modelPostTitle.text = mPostTitle
}

fun setmPostDescription(mPostDescription: String) {
modelPostDescription.text = mPostDescription
}
}

最佳答案

if (glide != null) {
setmPostImageUrl(glide, model.getmPostImageUrl())
}

Glide 是null 因为你永远不会分配/实例化它。

替换这一行:

private val glide: RequestManager? = null

与:

private var glide: RequestManager? = null

并在 onCreate() 中执行:

glide = Glide.with(this)

关于android - 使用 Glide 在 Recyclerview 中不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46013255/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com