gpt4 book ai didi

java - Android kotlin/java - ReclerView 在 Holder/xml 中隐藏部件时的奇怪行为

转载 作者:行者123 更新时间:2023-12-01 19:46:22 35 4
gpt4 key购买 nike

我不知道这个问题的正确标题是什么。

我正在做一个聊天应用程序,这是适配器内的部分:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val msg = messages[position]

if (msg.who == "you"){
holder.messagemelayout.visibility = View.GONE
holder.messageyou.text = msg.message
}else{
holder.messageyoulayout.visibility = View.GONE
holder.messageme.text = msg.message
}
}

因此,当me发送消息时,“you”的布局(messageyoulayout)就会被隐藏,反之亦然

当我添加这样的新消息时不会:

        var count = 1
bbb.setOnClickListener {
messageslist.add(Chat("hey " + count.toString(), "me"))
adapter.notifyItemInserted(adapter.itemCount - 1)

count++
}

结果是这样的:

enter image description here

当我不隐藏任何布局时,布局内未得到任何更新的文本仍然会被随机的旧内容填充:

enter image description here

我希望这个问题是可以理解的。

我该如何解决这个问题?就像完全删除没有更新或其他内容的布局一样。

提前致谢

编辑:

整个适配器:

class ChatAdapter(val context: Context, private val messages: MutableList<Chat>) : RecyclerView.Adapter<ChatAdapter.ViewHolder>(){


override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val msg = messages[position]

if (msg.who == "you"){
holder.messageyou.text = msg.message

holder.messageme.text = ""

holder.messageme.setBackgroundResource(0)
holder.messageyou.setBackgroundResource(R.drawable.round_corners_lightgray_color)

}else{
holder.messageme.text = msg.message

holder.messageyou.text = ""

holder.messageme.setBackgroundResource(R.drawable.round_corners_accent_color)
holder.messageyou.setBackgroundResource(0)
}
}

override fun getItemCount() = messages.size

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

class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!){

val messageyou = itemView!!.messageyou!!
val messageme = itemView!!.messageme!!
val messageyoulayout = itemView!!.messageyoulayout!!
val messagemelayout = itemView!!.messagemelayout!!

}

}

聊天.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/tools"
android:id="@+id/malsehn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
app:cardBackgroundColor="@android:color/transparent"
app:cardElevation="0dp">


<LinearLayout
android:id="@+id/messagemelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4">

<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3">

<TextView
android:id="@+id/messageme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_corners_accent_color"
android:paddingLeft="8dp"
android:paddingTop="6dp"
android:paddingRight="8dp"
android:paddingBottom="6dp"
android:text="TextView"
android:textColor="@android:color/white"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/messageyoulayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4">

<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3">

<TextView
android:id="@+id/messageyou"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_corners_lightgray_color"
android:paddingLeft="8dp"
android:paddingTop="6dp"
android:paddingRight="8dp"
android:paddingBottom="6dp"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</android.support.constraint.ConstraintLayout>
</LinearLayout>

</android.support.v7.widget.CardView>

最佳答案

当您不了解 ViewHolder 模式的工作原理时,不要称其为“有问题的困惑”。

您应该使用多个 itemViewType,而不是连续翻转 View 可见性(请注意,整个代码都进入您的 RecyclerView.Adapter):

// constants
companion object {
const val TYPE_YOU = 1
const val TYPE_ME = 2
}

/* This method is called to determine what type of ViewHolder should be used to represent item at [position] */
override fun getItemViewType(position: Int) = if(messages[position].who == "you") TYPE_YOU else TYPE_ME

/* [viewType] determines what ViewHolder we should create. */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val layoutId = when(viewType){
TYPE_YOU -> {
// this viewholder will display only messages from "you", inflate only "you" layout
R.layout.chat_item_you
}
TYPE_ME ->{
// this viewholder will display only messages from "me", inflate only "me" layout
R.layout.chat_item_me
}
else -> throw IllegalArgumentException("Unknown viewType: $viewType")
}
val itemView = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
return ViewHolder(itemView)
}

// then you can use your original bind method, because "you" and "me" messages will never re-user others ViewHolder
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val msg = messages[position]

if (msg.who == "you"){
// holder.itemViewtype should always be equal to TYPE_YOU here
}else{
// holder.itemViewtype should always be equal to TYPE_ME here
}

holder.message.text = msg.message
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val txtMessage : TextView = itemView.findViewById(R.id.message)
}

然后将聊天气泡分成两个单独的布局:

chat_item_you.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="6dp"
android:layout_height="wrap_content">

<TextView
android:id="@+id/message"
style="@style/ChatBubbleStyle"
android:background="@drawable/round_corners_lightgray_color"
android:text="TextView"
android:layout_gravity="end" />
</FrameLayout>

类似,但颜色和重力不同:

chat_item_me.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_margin="6dp"
android:layout_height="wrap_content">

<TextView
android:id="@+id/message"
style="@style/ChatBubbleStyle"
android:background="@drawable/round_corners_blue_color"
android:text="TextView"
android:layout_gravity="start" />
</FrameLayout>

styles.xml中使用共享样式来防止冗余代码:

<style name="ChatBubbleStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingTop">6dp</item>
<item name="android:paddingRight">8dp</item>
<item name="android:paddingBottom">6dp</item>
<item name="android:textColor">@android:color/black</item>
<item name="android:textSize">16sp</item>
</style>

关于java - Android kotlin/java - ReclerView 在 Holder/xml 中隐藏部件时的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53176342/

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