【问题标题】:using Groupie RecyclerView I've been trying to show users from Firebase database, but I can't use the TextView in override fun bind使用 Groupie RecyclerView 我一直在尝试从 Firebase 数据库中显示用户,但我不能在覆盖有趣的绑定中使用 TextView
【发布时间】:2022-08-20 03:08:35
【问题描述】:

这很可能是视图绑定问题,但我无法弄清楚,我尝试在单独的类中制作适配器,但仍然存在同样的问题

这是应该显示包含 RecyclerView 和适配器的应用程序用户的活动,我正在使用 GroupieAdapter 作为适配器:

class NewMessageActivity : AppCompatActivity() {
private lateinit var binding: ActivityNewMessageBinding
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityNewMessageBinding.inflate(LayoutInflater.from(this))
    setContentView(binding.root)
    val topAppBar = binding.topAppBar
    val rvNewChats = binding.rvNewChats

        rvNewChats.apply {
            setHasFixedSize(true)
            layoutManager = LinearLayoutManager(context)
        }

    topAppBar.setNavigationOnClickListener {
        finish()
    }
    fetchUsers()
}

private fun fetchUsers(){
    val ref = FirebaseDatabase.getInstance().getReference(\"/users\")
    ref.addListenerForSingleValueEvent(object: ValueEventListener{
        override fun onDataChange(snapshot: DataSnapshot) {
            val rvNewChats = binding.rvNewChats
            val adapter = GroupAdapter<GroupieViewHolder>()

            snapshot.children.forEach{
                Log.d(\"New Message\", it.toString())

                val user = it.getValue(User::class.java)
                if(user != null){
                    adapter.add(UserItem(user))
                }
            }
            rvNewChats.adapter = adapter
        }

        override fun onCancelled(error: DatabaseError) {
        }
    })
  }
  }

这是我的 UserItem 类,我一直在尝试从用户行布局中 ViewBind 一个 TextView,所以我可以相应地更改它的名称,(viewHolder.itemView.tvUserName.text = user.username) 我应该是能够访问其文本并更改它,但它无法识别它(tvUserName)

class UserItem(private val user: User): Item<GroupieViewHolder>() {
lateinit var binding: NewMessageUserBinding
val tvUserName = binding.tvUserNameNc

override fun getLayout() = R.layout.new_message_user

override fun bind(viewHolder: GroupieViewHolder, position: Int) {
    viewHolder.itemView.tvUserName.text = user.username
   }
}

这是用户行布局(new_message_user.xml),它包含一个 ImageView 和我一直试图在绑定乐趣中访问的 TextView

<de.hdodenhof.circleimageview.CircleImageView
    android:id=\"@+id/user_profile_photo\"
    android:layout_width=\"50dp\"
    android:layout_height=\"50dp\"
    android:layout_margin=\"15dp\"
    android:src=\"@color/black\"
    android:cropToPadding=\"true\"
    app:layout_constraintBottom_toBottomOf=\"parent\"
    app:layout_constraintStart_toStartOf=\"parent\"
    app:layout_constraintTop_toTopOf=\"parent\"/>

<TextView
    android:id=\"@+id/tv_user_name_nc\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:layout_margin=\"15dp\"
    android:text=\"@string/user_name_nc\"
    android:textSize=\"16sp\"
    android:textColor=\"@color/black\"
    android:fontFamily=\"@font/dm_mono\"
    app:layout_constraintBottom_toBottomOf=\"parent\"
    app:layout_constraintStart_toEndOf=\"@+id/user_profile_photo\"
    app:layout_constraintTop_toTopOf=\"parent\" />

  </androidx.constraintlayout.widget.ConstraintLayout>

这是我的用户类

 class User(val uid: String ,val phoneNumber: String ,val username: String,  val 
 profileImageUrl: String){
 constructor() : this(\"\", \"\", \"\", \"\")
 }

知道我做错了什么吗?

  • \"但它不认识它\" - 你是什么意思?您在 logcat 中看到任何错误吗?
  • tvUserName.text 这个,是红色的!我无法使用它
  • viewHolder.tvUserName.text = user.username 有效吗?
  • 不,它没有。
  • 这很可能是视图绑定问题,但我无法弄清楚,我尝试在单独的类中制作适配器,但仍然存在同样的问题

标签: kotlin android-recyclerview android-viewbinding groupie


【解决方案1】:

实际上,如果您阅读 groupie adapter 的文档,您会找到答案 https://github.com/lisawray/groupie/tree/master/example-viewbinding

  1. 添加此依赖项
    • 实施“com.github.lisawray.groupie:groupie:$groupie_version”
    • 实施“com.github.lisawray.groupie:groupie-viewbinding:$groupie_version”
    1. 让你的适配器变成这样
      class YourAdapter(val item: ItemCategory) : BindableItem<ItemLayoutBinding?>() {
      
          override fun initializeViewBinding(view: View): ItemLayoutBinding =
              ItemLayoutBinding.bind(view)
      
          override fun getLayout(): Int {
              return R.layout.item_layout
          }
      
          override fun bind(viewBinding: ItemLayoutBinding, position: Int) {
              viewBinding.apply {
                  itemText.text = "your text"
              }
          }
      }

    你的抓取用户

    private lateinit var groupAdapter: GroupieAdapter
    
    private fun fetchUsers(){
        groupAdapter = GroupieAdapter()
    
        val ref = FirebaseDatabase.getInstance().getReference("/users")
        ref.addListenerForSingleValueEvent(object: ValueEventListener{
            override fun onDataChange(snapshot: DataSnapshot) {
                val rvNewChats = binding.rvNewChats
    
                snapshot.children.forEach{
                    Log.d("New Message", it.toString())
    
                    val user = it.getValue(User::class.java)
                    if(user != null){
                        groupAdapter.add(UserItem(user))
                    }
                }
                rvNewChats.adapter = groupAdapter
            }
    
            override fun onCancelled(error: DatabaseError) {
            }
        })
      }
    

【讨论】:

    猜你喜欢
    • 2018-08-08
    • 1970-01-01
    • 2020-10-15
    • 2021-12-19
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多