gpt4 book ai didi

android - 改变 recyclerview 项目的可见性会改变其他项目的可见性

转载 作者:行者123 更新时间:2023-11-29 23:25:19 25 4
gpt4 key购买 nike

我有一个带适配器的 recyclerView...recyclerview 有带有类似按钮的项目。当我点击(喜欢)按钮时,(不像)应该变得不可见,反之亦然..一切正常但是当我点击(喜欢)一个项目时,其他项目中的(不同)也变得不可见,就像我点击一样其他 View 中的相同按钮我已经尝试过这个答案,但没有用 Android: Changing visibility of a view in recyclerView这是适配器代码:

    package com.example.user.myapplication;

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.opengl.Visibility
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.*
import android.widget.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
import kotlinx.android.synthetic.main.item.view.*
import kotlinx.android.synthetic.main.itemm.view.*
import java.io.BufferedReader
import java.io.File
import java.io.FileReader

/**
* Created by Ahmed on 10/14/2018.
*/

class ItemmAdapter(private val mItems: Array<File>,private val activity:Activity) : RecyclerView.Adapter<ItemmAdapter.ItemViewHolder>() {

lateinit var parent:ViewGroup

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.itemm, parent, false)
this.parent=parent
return ItemViewHolder(view)
}

override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {

val item = mItems[position]
holder.itemView.layoutParams=RecyclerView.LayoutParams(parent.context.resources.displayMetrics.widthPixels/2,RecyclerView.LayoutParams.MATCH_PARENT)



//holder.itemView.findViewById<ImageView>(R.id.image).setImageResource(R.drawable.icons8_cycling_104)
//holder.itemView.findViewById<TextView>(R.id.description).text="abcdefghijklmnopqrstuvwxyzضصثقفغعهخحجدطكمنتالبيسشذئءؤرلاىة"
//holder.itemView.findViewById<TextView>(R.id.price).text="10000$"
holder.like.setOnClickListener{
holder.like.visibility=View.GONE
holder.unlike.visibility=View.VISIBLE
}
holder.unlike.setOnClickListener{
holder.unlike.visibility=View.GONE
holder.like.visibility=View.VISIBLE
}
holder.shopping.setOnClickListener{
holder.shopping.visibility=View.GONE
holder.notshopping.visibility=View.VISIBLE
}
holder.notshopping.setOnClickListener{
holder.notshopping.visibility=View.GONE
holder.shopping.visibility=View.VISIBLE
}
}

override fun getItemCount(): Int {
return mItems.size
}

inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val like=itemView.findViewById<ImageView>(R.id.like)
val unlike=itemView.findViewById<ImageView>(R.id.unlike)
val shopping=itemView.findViewById<ImageView>(R.id.shopping)
val notshopping=itemView.findViewById<ImageView>(R.id.notShopping)


}

}

请帮帮我

最佳答案

仅在每个项目中使用绑定(bind)来设置项目内容并且将监听器从绑定(bind)方法移动到持有者类:

inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var liked = false
var shopped = false
val like=itemView.findViewById<ImageView>(R.id.like)
val unlike=itemView.findViewById<ImageView>(R.id.unlike)
val shopping=itemView.findViewById<ImageView>(R.id.shopping)
val notshopping=itemView.findViewById<ImageView>(R.id.notShopping)

init{

like?.setOnClickListener{
liked = true
like.visibility=View.GONE
unlike.visibility=View.VISIBLE
}
unlike?.setOnClickListener{
liked = false
unlike.visibility=View.GONE
like.visibility=View.VISIBLE
}
shopping?.setOnClickListener{
shopped = true
shopping.visibility=View.GONE
notshopping.visibility=View.VISIBLE
}
notshopping?.setOnClickListener{
shopped = false
notshopping.visibility=View.GONE
shopping.visibility=View.VISIBLE
}
}

fun bind(){// call this from onBind method of adapter
if(liked) like?.performClick() else unlike?.performClick()
if(shopped) shopping?.performClick() else notshopping?.performClick()
}

}

关于android - 改变 recyclerview 项目的可见性会改变其他项目的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53694594/

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