我正在使用回收视图来显示用户的最新输入。当我单击编辑文本时,旧输入向上移动并且键盘显示在应用程序中。但是当我输入下一个条目时,我必须最小化键盘才能看到新条目。它没有向上移动 - 这是更新回收视图时的预期行为。
阶段 1)
第 2 阶段)点击编辑文本以创建新条目
阶段 3) 输入新文本
阶段 4) 键盘隐藏/最小化
布局从最后开始堆叠......我可以更改什么才能使其工作?
AndroidManifest 设置
<activity
android:name=".Contacts.ui.ContactDetailsHome"
android:configChanges="orientation"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize" />
片段代码
class ContactRelationship : Fragment() {
companion object {
@JvmStatic
fun start(context: Context, contact: Contact?, isEdit: Boolean) {
val starter = Intent(context, ContactRelationship::class.java)
.putExtra("contact", contact as Serializable)
.putExtra("Edit", isEdit)
context.startActivity(starter)
}
}
private val _updatesLiveData = MutableLiveData<ArrayList<ContUPDt>>()
private val updatesLiveData: LiveData<ArrayList<ContUPDt>> = _updatesLiveData
var firebaseUser: FirebaseUser? = null //the used id of the user using the app
var userLookupKey: String = ""
private lateinit var contact: Contact
var mupdateList: List<ContUPDt>? = null
var contUpdateAdapter: ContUpdateAdapter? = null
var reference: DatabaseReference? = null
lateinit var rvContRelUpdate: RecyclerView
private lateinit var progressBar: ProgressDialog
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for this fragment
val view: View =
inflater.inflate(R.layout.fragment_view_contact_relationship, container, false)
progressBar = ProgressDialog(activity)
progressBar.setCancelable(false)
firebaseUser = FirebaseAuth.getInstance().currentUser
rvContRelUpdate = view.findViewById(R.id.rvContRelUpdate)
rvContRelUpdate.setHasFixedSize(true)
val linearLayoutManager = LinearLayoutManager(activity)
linearLayoutManager.stackFromEnd = true
rvContRelUpdate.layoutManager = linearLayoutManager
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
contact = (activity?.intent?.getSerializableExtra("contact") as Contact)
//assigning it to fields to be displayed
userLookupKey = contact.lookupKey
val contId = contact.id
//get updates
retrieveUpdates(userLookupKey)
//Implementing Send Update
send_update_btn.setOnClickListener {
val updateTxt = text_messageact.text.toString()
if (updateTxt == "") {
Toast.makeText(activity, "The update is empty.", Toast.LENGTH_LONG)
.show()
} else {
sendMessageToUser(userLookupKey, contId, updateTxt)
}
text_messageact.setText("")
}
private fun sendMessageToUser(userLookupKey: String, contId: String, updateMsg: String) {
val currentCalendar = Calendar.getInstance()
//creating object in DB
val db = activity?.let { AppDatabase.getDatabase(it) }
val ev = ContUPDt(
0,
userLookupKey,
contId,
currentCalendar.timeInMillis,
updateMsg,
null,
"Update"
)
db!!.ContUpdateDao().addcontUpdate(ev)
}
private fun retrieveUpdates(userLookupKey: String) {
val mupdateList = ArrayList<ContUPDt>()
val db = AppDatabase.getDatabase(requireActivity())
db.ContUpdateDao().getcontUpdate(userLookupKey).observe(viewLifecycleOwner, Observer {
val contUpdt = it
mupdateList.clear()
for (i in contUpdt) {
mupdateList.add(i)
}
})
contUpdateAdapter = ContUpdateAdapter(requireActivity(), mupdateList)
rvContRelUpdate.adapter = contUpdateAdapter
}
}
适配器代码
class ContUpdateAdapter(
mContext: Context,
mupdateList: List<ContUPDt>
) : RecyclerView.Adapter<ContUpdateAdapter.ViewHolder?>() {
private val mContext: Context
private val mupdateList: List<ContUPDt>
init {
this.mContext = mContext
this.mupdateList = mupdateList
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var updateDTTime: TextView? = null
var updateMessage: TextView? = null
var updateType: TextView? = null
init {
updateDTTime = itemView.findViewById(R.id.updateDTTime)
updateMessage = itemView.findViewById(R.id.updateMessage)
updateType = itemView.findViewById(R.id.updateType)
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
position: Int
): ViewHolder {
return ViewHolder (
LayoutInflater.from(parent.context)
.inflate(R.layout.contactupdate, parent, false)
)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val contUpdte: ContUPDt = mupdateList[position]
val formatter = SimpleDateFormat(" E, HH:MM a, dd MMM yyyy", Locale.getDefault())
val updatetime = formatter.format(contUpdte.timestamp)
holder.updateDTTime!!.text = updatetime
holder.updateMessage!!.text = contUpdte.update
holder.updateType!!.text = contUpdte.actType
}
override fun getItemCount(): Int {
return mupdateList.size
}
}
答案 0 :(得分:0)
添加新项目后只需将 RecyclerView 滚动到最后位置:
rvContRelUpdate.scrollToPosition(contUpdateAdapter.itemCount - 1)