简体   繁体   English

Android Activity 娱乐前台 ViewPager2 布局测量

[英]Android Activity recreation front ViewPager2 layout measurements

My activity contains ViewPager2 in its layout.我的活动在其布局中包含 ViewPager2。

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="ru.telemonitor.signal.r2d2.multimeter.imsat.Fragments.Dialogs.CalibrateActivity">

    ...

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@+id/calibrate_result"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/calibrate_steps_pager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            />

        <com.google.android.material.tabs.TabLayout
            android:id="@+id/calibrate_steps_tab_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            app:tabBackground="@drawable/tab_indicator_selector"
            app:tabGravity="center"
            app:tabIndicatorHeight="0dp"
            />


    </LinearLayout>
    

</androidx.constraintlayout.widget.ConstraintLayout>

That is how ViewPager2 initializes.这就是 ViewPager2 初始化的方式。 Note that CalibrateProbesFragment has a different height than others.请注意,CalibrateProbesFragment 的高度与其他高度不同。

private inner class CalibrateActivityPagerAdapter(fa: FragmentActivity) : FragmentStateAdapter(fa) {
        override fun getItemCount(): Int = 4

        override fun createFragment(position: Int): Fragment {
            return when (position) {
                0 -> CalibrateProbesFragment()
                1 -> CalibrateVoltageFragment()
                2 -> CalibrateCurrentFragment()
                3 -> CalibrateResistanceFragment()
                else -> Fragment()
            }
        }
    }

When the activity is created by Intent there are right height measurements.当活动由 Intent 创建时,会有正确的高度测量。

在此处输入图片说明 在此处输入图片说明

After that, I try to recreate the activity in its onActivityResult method after calling some other activity by startActivityForResult.之后,我尝试在通过 startActivityForResult 调用其他活动后在其 onActivityResult 方法中重新创建活动。

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        val unmaskedRequestCode = requestCode and 0x0000ffff

        if (unmaskedRequestCode == ImsatActivity.PICK_DEVICE_REQUEST) {
            if (resultCode == RESULT_OK) {
                data?.let { data ->

                    val index = data.getIntExtra(DiscoverActivity.DEVICE_INDEX, -1)
                    if (index < 0) {
                        super.onBackPressed()
                        return
                    }

                    val available = AppController.getInstance().dataProviderService.discoveredDevices
                    if (available.size == 0) {
                        Toast.makeText(this, "Error: try reconnect", Toast.LENGTH_LONG).show()
                        super.onBackPressed()
                        return
                    }

                    if (available.size < index) {
                        super.onBackPressed()
                        return
                    }

                    val multimeterDevice = available[index]
                    val name = data.getStringExtra(DiscoverActivity.DEVICE_NAME)
                    if (name == null || name == "" || name != multimeterDevice.name) {
                        super.onBackPressed()
                        return
                    }

                    AppController.getInstance().dataProviderService.tryConnectDevice(multimeterDevice)

                    viewPager.setCurrentItem(0, false)
                    restart()
                }
            }
        }
        }

    private fun restart () {
       // startActivity(Intent.makeRestartActivityTask(this.intent?.component))
        recreate()
    }

As you can note I call viewPager.setCurrentItem(0, false) before recreate().正如您所注意到的,我在 recreate() 之前调用了 viewPager.setCurrentItem(0, false)。 After activity reset all fragments inside viewpager have the same height as this first fragment(at the index 0).活动重置后,viewpager 中的所有片段与第一个片段具有相同的高度(在索引 0 处)。

在此处输入图片说明 在此处输入图片说明

I use recreate because I want the activity to be at the same point in the back navigation queue but fully recreate its views.我使用重新创建是因为我希望活动位于后退导航队列中的同一点,但完全重新创建其视图。 And mostly because that seems to be less code solution for that.主要是因为这似乎是更少的代码解决方案。 I really don't want to write 100+ lines for custom recreation if it is possible to use standard Activity methods.如果可以使用标准的 Activity 方法,我真的不想为自定义娱乐编写 100 多行。 Maybe there are some workarounds?也许有一些解决方法? Thank you.谢谢你。

I don't know how to solve this.我不知道如何解决这个问题。 I just start a new activity by the Intent.我刚刚通过 Intent 开始了一项新活动。

private fun restart () {
       // startActivity(Intent.makeRestartActivityTask(this.intent?.component))
//        recreate()
        val intent = Intent(this, CalibrateActivity::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_TASK_ON_HOME
        startActivity(intent)
        finish()
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM