焦点在 EditText 上时 ViewPager2 片段内容消失

Posted

技术标签:

【中文标题】焦点在 EditText 上时 ViewPager2 片段内容消失【英文标题】:ViewPager2 fragment content is disappear when focus on EditText 【发布时间】:2020-11-10 03:14:46 【问题描述】:

当我通过点击关注 EditText 时,viewpager2 的内容会消失,当我开始输入内容时会重新出现,并且当旋转我的手机内容时也会重新出现 您可以在视频here中看到问题

这是我创建 tablayout 和 viewpager2 的课程

class ATab (activity: AppCompatActivity,viewPagerId: Int,tabLayoutId: Int) 
val items = arrayListOf<ItemModel>()
val adapter = Adapter(items , activity)
val viewpager = activity.findViewById<ViewPager2>(viewPagerId)
val tabLayout = activity.findViewById<TabLayout>(tabLayoutId)

fun add(fragment : Fragment ,title : String ="" ,icon : String = "")
    items.add(ItemModel(fragment,title,icon))
    adapter.notifyDataSetChanged()

init 
    viewpager.adapter = adapter
    TabLayoutMediator(tabLayout,viewpager)tab, position ->
        if (!items[position].title.equals(""))
            tab.text = items[position].title
        
        if (!items[position].icon.equals(""))
            val res_id = activity.resources.getIdentifier(items[position].icon,"drawable",activity.packageName)
            tab.icon = activity.getDrawable(res_id)
        

    .attach()


inner class Adapter(val model : List<ItemModel>, context: AppCompatActivity) : FragmentStateAdapter(context)
    override fun getItemCount(): Int 
        return  model.count()
    

    override fun createFragment(position: Int): Fragment = model[position].fragment


inner class ItemModel(var fragment : Fragment ,var title : String ,var icon : String)

这是我的活动

class LoginActivity : AppCompatActivity() 

override fun onCreate(savedInstanceState: Bundle?) 
    setTheme(R.style.AppTheme2)
    super.onCreate(savedInstanceState)
    overridePendingTransition(
        R.anim.fade_in,
        R.anim.fade_out
    )
    requestWindowFeature(Window.FEATURE_NO_TITLE)
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
    setContentView(R.layout.activity_login)

    val atab = ATab(this,R.id.View_splash,R.id.Tab_splash)
    atab.add(SignInFragment(),"ورود")
    atab.add(SignUpFragment(),"ثبت نام")


override fun onResume() 
    super.onResume()
    overridePendingTransition(
        R.anim.fade_in,
        R.anim.fade_out
    )

这是我的布局

<androidx.constraintlayout.widget.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:id="@+id/login"
android:layout_
android:layout_
android:background="@drawable/splash_bachground_gradiant"
tools:context=".Activities.LoginActivity">


<LinearLayout
    android:id="@+id/cons_login"
    android:layout_
    android:layout_
    android:background="@drawable/splash_logo_background"
    android:orientation="vertical"
    android:padding="16dp"
    app:layout_constraintBottom_toTopOf="@+id/guideline5"
    app:layout_constraintEnd_toStartOf="@+id/guideline4"
    app:layout_constraintStart_toStartOf="@+id/guideline3"
    app:layout_constraintTop_toTopOf="@+id/guideline2">


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/Tab_splash"
        android:layout_
        android:layout_
        app:layout_constraintBottom_toTopOf="@+id/View_splash"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/View_splash"
        android:layout_
        android:layout_
        android:minHeight="500px"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/Tab_splash">

    </androidx.viewpager2.widget.ViewPager2>


</LinearLayout>

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline3"
    android:layout_
    android:layout_
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.11" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline4"
    android:layout_
    android:layout_
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.89" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_
    android:layout_
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.18" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline5"
    android:layout_
    android:layout_
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.82" /></androidx.constraintlayout.widget.ConstraintLayout>

最后这是我的片段布局

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/f2"
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_
android:layout_>

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/cons_login"
    android:layout_
    android:layout_
    android:background="?attr/colorWhite"
    android:padding="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">


    <EditText
        android:id="@+id/edt_sign_up_username"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:background="@drawable/splash_edt_background"
        android:ems="10"
        android:hint="نام کاربری"
        android:inputType="textPersonName"
        android:padding="8dp"
        android:textColor="?attr/colorBlack"
        android:textColorHighlight="?attr/colorPrimaryDark"
        android:textColorHint="?attr/colorHint"
        android:textColorLink="?attr/colorPrimary"
        android:textCursorDrawable="?attr/colorPrimary"
        app:layout_constraintBottom_toTopOf="@+id/edt_sign_up_fullname"
        app:layout_constraintEnd_toEndOf="@+id/edt_sign_up_fullname"
        app:layout_constraintStart_toStartOf="@+id/edt_sign_up_fullname"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/edt_sign_up_fullname"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:background="@drawable/splash_edt_background"
        android:ems="10"
        android:hint="نام و نام خانوادگی"
        android:inputType="textPersonName"
        android:padding="8dp"
        android:textColor="?attr/colorBlack"
        android:textColorHighlight="?attr/colorPrimaryDark"
        android:textColorHint="?attr/colorHint"
        android:textColorLink="?attr/colorPrimary"
        android:textCursorDrawable="?attr/colorPrimary"
        app:layout_constraintBottom_toTopOf="@+id/edt_sign_up_email"
        app:layout_constraintEnd_toEndOf="@+id/edt_sign_up_email"
        app:layout_constraintStart_toStartOf="@+id/edt_sign_up_email"
        app:layout_constraintTop_toBottomOf="@+id/edt_sign_up_username" />

    <EditText
        android:id="@+id/edt_sign_up_email"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:background="@drawable/splash_edt_background"
        android:ems="10"
        android:hint="پست الکترونیک"
        android:inputType="textEmailAddress"
        android:padding="8dp"
        android:textColor="?attr/colorBlack"
        android:textColorHighlight="?attr/colorPrimaryDark"
        android:textColorHint="?attr/colorHint"
        android:textColorLink="?attr/colorPrimary"
        android:textCursorDrawable="?attr/colorPrimary"
        app:layout_constraintBottom_toTopOf="@+id/edt_sign_up_password"
        app:layout_constraintEnd_toEndOf="@+id/edt_sign_up_password"
        app:layout_constraintStart_toStartOf="@+id/edt_sign_up_password"
        app:layout_constraintTop_toBottomOf="@+id/edt_sign_up_fullname" />

    <EditText
        android:id="@+id/edt_sign_up_password"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:background="@drawable/splash_edt_background"
        android:ems="10"
        android:gravity="right"
        android:hint="رمز عبور"
        android:inputType="textPassword"
        android:padding="8dp"
        android:textColor="?attr/colorBlack"
        android:textColorHighlight="?attr/colorPrimaryDark"
        android:textColorHint="?attr/colorHint"
        android:textColorLink="?attr/colorPrimary"
        android:textCursorDrawable="?attr/colorPrimary"
        app:layout_constraintBottom_toTopOf="@+id/edt_sign_up_password_repeat"
        app:layout_constraintEnd_toEndOf="@+id/edt_sign_up_password_repeat"
        app:layout_constraintStart_toStartOf="@+id/edt_sign_up_password_repeat"
        app:layout_constraintTop_toBottomOf="@+id/edt_sign_up_email" />

    <EditText
        android:id="@+id/edt_sign_up_password_repeat"
        android:layout_
        android:layout_
        android:layout_marginTop="8dp"
        android:background="@drawable/splash_edt_background"
        android:ems="10"
        android:gravity="right"
        android:hint="تکرار رمز عبور"
        android:inputType="textPassword"
        android:padding="8dp"
        android:textColor="?attr/colorBlack"
        android:textColorHighlight="?attr/colorPrimaryDark"
        android:textColorHint="?attr/colorHint"
        android:textColorLink="?attr/colorPrimary"
        android:textCursorDrawable="?attr/colorPrimary"
        app:layout_constraintBottom_toTopOf="@+id/btn_sign_up_ok"
        app:layout_constraintEnd_toEndOf="@+id/btn_sign_up_ok"
        app:layout_constraintStart_toStartOf="@+id/btn_sign_up_ok"
        app:layout_constraintTop_toBottomOf="@+id/edt_sign_up_password" />

    <Button
        android:id="@+id/btn_sign_up_ok"
        android:layout_
        android:layout_

        android:layout_marginTop="8dp"
        android:background="@drawable/btn_style"
        android:padding="8dp"
        android:text="ثبت نام"
        android:textColor="?attr/colorTextrev"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/edt_sign_up_password_repeat" />

    <ProgressBar
        android:id="@+id/prg_login"
        style="?android:attr/progressBarStyle"
        android:layout_
        android:layout_
        android:progressDrawable="@drawable/splash_bachground_gradiant"
        android:visibility="gone"
        app:layout_constraintBottom_toBottomOf="@+id/btn_sign_up_ok"
        app:layout_constraintEnd_toEndOf="@+id/btn_sign_up_ok"
        app:layout_constraintStart_toStartOf="@+id/btn_sign_up_ok"
        app:layout_constraintTop_toTopOf="@+id/btn_sign_up_ok" />

</androidx.constraintlayout.widget.ConstraintLayout></androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

【参考方案1】:

我发现了错误 在我的片段布局中,我无缘无故地将 ConstraintLayout 用于另一个 所以删除后,错误修复。

【讨论】:

以上是关于焦点在 EditText 上时 ViewPager2 片段内容消失的主要内容,如果未能解决你的问题,请参考以下文章

切换viewpager时关闭键盘

从选项卡式片段导航时,如何避免聚焦最后一个 EditText?

Android上点击EditText将不会显示软键盘

片段中的Tablayout + viewpager,需要清除viewpager中的edittext数据

当 ViewPager 显示 Fragment 时 EditText 自动打开软键盘

android EditText焦点问题