我在片段中有recyclerview。当我单击recyclerview项目时,背景颜色会改变。但是当我参加另一项活动并返回时,按下的项目颜色保持不变。
我给adapter.notifyDataSetChanged()
打过电话,但什么都没发生。
这是我的RecyclerView.ViewHolder类
class GreetingsView(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imageView: ImageView? = null
var category: TextView? = null
var frameLayout: FrameLayout? = null
var context: Context? = itemView.context
private var listOfGreetings: List<GreetingModel>? = null
init {
imageView = itemView.findViewById(R.id.greetingImage)
category = itemView.findViewById(R.id.greetingCategory)
frameLayout = itemView.findViewById(R.id.card_view)
this.listOfGreetings = listOfGreetings
itemView.setOnClickListener {
val position = adapterPosition
if (position != RecyclerView.NO_POSITION) {
frameLayout?.setBackgroundColor(Color.BLACK)
val intent = Intent(context, GridGreetingListActivity::class.java)
intent.putExtra("position", position)
context?.startActivity(intent)
}else {
frameLayout?.setBackgroundColor(Color.WHITE)
}
}
}
}
这是我的片段
class CategoryFragment : Fragment(), SendItemPositionListener {
var greetingAdapter: GreetingAdapter? = null
var listOfGreetings: ArrayList<GreetingModel>? = arrayListOf()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
listOfGreetings?.apply {
add(0, GreetingModel("Greeting 1", R.drawable.img_1))
add(1,
GreetingModel("Greeting 2", R.drawable.greeting_1)
)
add(2, GreetingModel("Greeting 3", R.drawable.img_3))
add(3, GreetingModel("Greeting 4", R.drawable.img_4))
add(4, GreetingModel("Greeting 5", R.drawable.img_5))
add(5, GreetingModel("Greeting 6", R.drawable.img_6))
add(6, GreetingModel("Greeting 7", R.drawable.img_7))
add(7, GreetingModel("Greeting 8", R.drawable.img_8))
}
val view = inflater.inflate(R.layout.fragment_category, container, false)
//Get the recyclerView from the inflated view.
val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
val manager = LinearLayoutManager(activity)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = manager
greetingAdapter = GreetingAdapter(activity!!, listOfGreetings!!.toList())
recyclerView.adapter = greetingAdapter
greetingAdapter!!.notifyDataSetChanged()
return view
}
我不确定我在正确的地方打电话给notifyDataSetChanged()
。希望你能帮忙!
答案 0 :(得分:0)
我找到了解决问题的方法。需要在setOnClickListener
的{{1}}上调用itemView
,而不是onBindViewHolder
上的ViewHolder
,并在greetingAdapter?.notifyDataSetChanged()
方法上调用onResume()
。
override fun onBindViewHolder(holder: GreetingsView, position: Int) {
holder.category?.text = listOfGreetings?.get(position)?.greetingCategory
holder.cardView?.setBackgroundColor(Color.TRANSPARENT)
Glide
.with(context?.applicationContext!!)
.load(listOfGreetings?.get(position)?.imageId)
.apply(
RequestOptions()
.placeholder(R.drawable.loading)
.fitCenter()
)
.into(holder.imageView!!)
holder.itemView.setOnClickListener {
holder.cardView?.setBackgroundColor(Color.RED)
if (position != RecyclerView.NO_POSITION) {
val intent = Intent(context, GridGreetingListActivity::class.java)
intent.putExtra("position", 1)
context?.startActivity(intent)
}
}
}
class GreetingsView(itemView: View) : RecyclerView.ViewHolder(itemView) {
var imageView: ImageView? = null
var category: TextView? = null
var cardView: FrameLayout? = itemView.findViewById(R.id.card_view)
var context: Context? = itemView.context
private var listOfGreetings: List<GreetingModel>? = null
init {
imageView = itemView.findViewById(R.id.greetingImage)
category = itemView.findViewById(R.id.greetingCategory)
this.listOfGreetings = listOfGreetings
}
}