首页 > 解决方案 > 如何防止editText与recyclerView重叠?

问题描述

问题是软键盘出现的时候,和recyclerView重叠。但我希望我的软键盘只需将回收器推到顶部,在键盘上方,就像windowSoftInputMode="adjustPan"一样。但是 adjustPan 不是选项,因为我需要我的工具栏。
我已经尝试过adjustResize,但没有运气。

主要活动布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
>

<include
        layout="@layout/toolbar_layout"
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
/>

<androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/recyc"
        app:layout_constraintBottom_toTopOf="@+id/message_edit_text"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_layout"/>


<com.google.android.material.textfield.TextInputEditText
        android:id="@+id/message_edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textShortMessage"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

适配器项目布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">


<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的MainActivity代码,只有 onCreate 方法

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    recyc.layoutManager = LinearLayoutManager(this)
    val adapter = MyAdapter(
        arrayOf("sd","sd2","sd2","sd2","sd2","s2d","s2d","sd",
            "sd2","s2d","s2d","sd5","s8d","67sd","s76d")
    )
    recyc.adapter = adapter
}

这是我的适配器类

class MyAdapter(private val myDataset: Array<String>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {

class MyViewHolder(val view: View) : 
RecyclerView.ViewHolder(view)

override fun onCreateViewHolder(
    parent: ViewGroup,
    viewType: Int
): MyAdapter.MyViewHolder {
    val textView = LayoutInflater.from(parent.context)
        .inflate(R.layout.container, parent, false)
    return MyViewHolder(textView)
}

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) 
      { holder.itemView.textView.text = myDataset[position] }

    override fun getItemCount() = myDataset.size
}

标签: androidandroid-recyclerviewandroid-view

解决方案


推荐阅读