我有一个使用recyclerview的帖子列表。列表很棒,但是当我尝试在recyclerview上添加图像时会出现问题。我正在使用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
}
}
答案 0 :(得分:2)
if (glide != null) {
setmPostImageUrl(glide, model.getmPostImageUrl())
}
Glide null 因为您从不分配/实例化它。
替换此行:
private val glide: RequestManager? = null
with:
private var glide: RequestManager? = null
并在onCreate()
中执行:
glide = Glide.with(this)