我正在我的应用程序中实现聊天,当键盘打开时,回收器视图以有线方式运行,卡片自行扩展
我试图禁用回收,改变我构建布局的方式但没有任何作用,我需要理解为什么会发生这种情况以及如何解决它。
视图的XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical"
tools:context="io.thed.cuju.ChatDetailsFragment">
<LinearLayout
android:id="@+id/linearLayout22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@+id/chat_image"
android:layout_width="32dp"
android:layout_height="32dp"
tools:src="@drawable/avatar" />
<TextView
android:id="@+id/name_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:textSize="16sp"
tools:text="Bradley Anderson" />
</LinearLayout>
<include
android:id="@+id/say_hi"
layout="@layout/say_hi_chat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<android.support.constraint.ConstraintLayout
android:id="@+id/chat_list_and_input_section"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/chat_details_recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_constraintBottom_toTopOf="@+id/linearLayout5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/space2"
app:reverseLayout="true"
tools:listitem="@layout/recived_chat_box_item" />
<TextView
android:id="@+id/space2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/Gray"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ProgressBar
android:id="@+id/progressBar12"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@+id/linearLayout5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="@+id/to_be_sent_chat"
style="@style/cujuEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:ems="10"
android:hint="Type a message"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/chat_extra"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/send_chat"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/to_be_sent_chat"
app:layout_constraintTop_toTopOf="@+id/to_be_sent_chat" />
<ImageView
android:id="@+id/chat_extra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/add_media_circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
适配器
package io.thed.cuju.adapters
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.support.v4.content.ContextCompat
import android.support.v7.widget.RecyclerView
import android.text.format.DateUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.google.gson.Gson
import com.pddstudio.preferences.encrypted.EncryptedPreferences
import com.squareup.picasso.Picasso
import io.thed.cuju.R
import io.thed.cuju.beans.MessageBean
import io.thed.cuju.beans.UserBean
import io.thed.cuju.constants.*
import io.thed.cuju.customsViews.ZoomImageFragment
import java.text.SimpleDateFormat
import java.util.*
class ChatDetailsAdapter// Provide a suitable constructor (depends on the kind of dataset)
(private val chatList: List<MessageBean>, internal var context: Context) : RecyclerView.Adapter<ChatDetailsAdapter.ViewHolder>() {
internal var token: String? = null
internal var user: UserBean? = null
val SENT_CHAT = 1
val RECIVED_CHAT = 2
override fun getItemViewType(position: Int): Int {
val current: MessageBean = chatList[position]
val encryptedPreferences = EncryptedPreferences.Builder(context).withEncryptionPassword(PASSWORD).build()
var token: String = encryptedPreferences.getString(USER_PREF_TOKEN_KEY, NO_TOKEN)
tokenForImage = "?x-access-token=" + token
user = Gson().fromJson(encryptedPreferences!!.getString(USER_PREF_KEY, ""), UserBean::class.java)
if (user!!._id != current.from) {
return RECIVED_CHAT
} else {
return SENT_CHAT
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChatDetailsAdapter.ViewHolder {
val v: View?
if (viewType == SENT_CHAT) {
v = LayoutInflater.from(parent.context).inflate(R.layout.send_chat_box_item, parent, false)
} else {
v = LayoutInflater.from(parent.context).inflate(R.layout.recived_chat_box_item, parent, false)
}
return ViewHolder(v)
}
// set the view's size, margins, paddings and layout parameters
lateinit var tokenForImage: String
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val current = chatList[position]
holder.setIsRecyclable(false)
if (current.data.text != null) {
holder.chat_text.text = current.data.text
}
if (current.data.type == "picture") {
val url = BASE_URL + current.data.media!!.thumbnail + tokenForImage
Picasso.with(context).load(Uri.parse(url)).fit().placeholder(context.resources.getDrawable(R.drawable.placeholder)).into(holder.chat_image)
holder.chat_image.visibility = View.VISIBLE
holder.chat_play.visibility = View.GONE
holder.chat_image.setOnClickListener {
ZoomImageFragment.newInstance(url).show((context as Activity).fragmentManager, "")
}
} else if (current.data.type == "video") {
val url = BASE_URL + current.data.media!!.thumbnail + tokenForImage
Picasso.with(context).load(Uri.parse(url)).fit().placeholder(context.resources.getDrawable(R.drawable.placeholder)).into(holder.chat_image)
holder.chat_image.visibility = View.VISIBLE
holder.chat_play.visibility = View.VISIBLE
holder.chat_image.setOnClickListener {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(BASE_URL + current.data.media.link + tokenForImage))
ContextCompat.startActivity(context, intent, null)
}
} else {
holder.chat_image.visibility = View.GONE
holder.chat_play.visibility = View.GONE
}
val formatter = SimpleDateFormat(DATE_FORMAT)
formatter.timeZone = TimeZone.getTimeZone("GMT")
try {
val dateToBeParsed = DateUtils.getRelativeTimeSpanString(formatter.parse(current.created_at).time, System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS)
holder.chat_time.text = dateToBeParsed
} catch (e: Exception) {
println(e.message)
holder.chat_time.text = current.created_at
}
if (holder.itemViewType == SENT_CHAT) {
if (current.failed) {
holder.chat_status!!.setImageResource(R.drawable.message_seen)
} else if (current.seen) {
holder.chat_status!!.setImageResource(R.drawable.message_seen)
} else if (current.received) {
holder.chat_status!!.setImageResource(R.drawable.message_recived_not_seen)
} else if (current.sent) {
holder.chat_status!!.setImageResource(R.drawable.message_sent_to_server)
}
}
}
override fun getItemCount(): Int {
return chatList.size
}
class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val chat_image: ImageView = v.findViewById(R.id.chat_image)
val chat_play: ImageView = v.findViewById(R.id.chat_play)
val chat_text: TextView = v.findViewById(R.id.chat_text)
val chat_time: TextView = v.findViewById(R.id.chat_time)
val chat_status: ImageView? = v.findViewById(R.id.chat_status)
}
}
reviced_chat_box_item.xml(抱歉打字错误)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<android.support.v7.widget.CardView
android:id="@+id/cardView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="12dp"
app:cardBackgroundColor="@color/recivedChat"
app:cardCornerRadius="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/chat_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chat_text"
tools:srcCompat="@drawable/content_image" />
<ImageView
android:id="@+id/chat_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/chat_image"
app:layout_constraintEnd_toEndOf="@+id/chat_image"
app:layout_constraintStart_toStartOf="@+id/chat_image"
app:layout_constraintTop_toTopOf="@+id/chat_image"
app:srcCompat="@drawable/play" />
<TextView
android:id="@+id/chat_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:layout_marginEnd="2dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="2dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="Hello buddy, gonna go for a training today?"
android:textColor="@color/chat_color"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/chat_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_marginTop="4dp"
android:text="12 mins ago"
android:textColor="@color/time_ago"
android:textSize="13sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView2" />
</android.support.constraint.ConstraintLayout>
send_chat_box_item.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp">
<android.support.v7.widget.CardView
android:id="@+id/cardView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:layout_marginStart="8dp"
app:cardCornerRadius="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp">
<ImageView
android:id="@+id/chat_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chat_text"
tools:srcCompat="@drawable/content_image" />
<ImageView
android:id="@+id/chat_play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/chat_image"
app:layout_constraintEnd_toEndOf="@+id/chat_image"
app:layout_constraintStart_toStartOf="@+id/chat_image"
app:layout_constraintTop_toTopOf="@+id/chat_image"
app:srcCompat="@drawable/play" />
<TextView
android:id="@+id/chat_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="16dp"
android:layout_marginStart="2dp"
android:layout_marginTop="8dp"
android:text="Hello buddy, gonna go for a training today?"
android:textColor="@color/chat_color"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
<TextView
android:id="@+id/chat_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="4dp"
android:text="12 mins ago"
android:textColor="@color/time_ago"
android:textSize="13sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cardView2" />
<ImageView
android:id="@+id/chat_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="@+id/chat_time"
app:layout_constraintEnd_toStartOf="@+id/chat_time"
app:layout_constraintTop_toTopOf="@+id/chat_time"
app:srcCompat="@drawable/message_seen" />
</android.support.constraint.ConstraintLayout>