我无法弄清楚如何在单击RecyclerView的项目时更改alpha的值。我想把alpha的值从1改成0.5
下面是相同的适配器类
package GiftClass
import LeaderboardClass.LeaderboardAdapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.gearsrun.www.R
import kotlinx.android.synthetic.main.item_gift.view.*
class GiftAdapter(private val giftList: List<Gift>) : RecyclerView.Adapter<GiftAdapter.GiftViewHolder>() {
private lateinit var mlistener : onItemClickListener
interface onItemClickListener{
fun onItemClick(position: Int)
}
fun setOnItemClickListener(listener: onItemClickListener){
mlistener = listener
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GiftViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_gift,parent,false)
return GiftViewHolder(itemView,mlistener)
}
override fun onBindViewHolder(holder: GiftViewHolder, position: Int) {
val currentItem = giftList[position]
holder.item_img.setImageResource(currentItem.imageResource)
holder.item_price.text = currentItem.price
holder.item_soldNum.text = currentItem.sold_num
holder.item_medal.text = currentItem.medal_num
}
override fun getItemCount() = giftList.size
class GiftViewHolder(itemView: View,listener:onItemClickListener) : RecyclerView.ViewHolder(itemView){
val item_img:ImageView = itemView.item_img
val item_price : TextView = itemView.price
val item_soldNum : TextView = itemView.sold_num
val item_medal : TextView = itemView.medal_num
init {
itemView.setOnClickListener {
listener.onItemClick(absoluteAdapterPosition)
itemView.alpha = 0.5f
}
}
}
}
添加的参考/建议是赞赏
在onClickListener中,您可以获得属性itemView。Alpha和调整Alpha值根据您的需要