android - 屏幕旋转时导致跳帧的 fragment

标签 android android-fragments

我有一个 fragment ,它显示超过 33 个 TextView 和一个折线图。屏幕旋转非常慢,Android Studio 会这样说:“我/编舞者:跳过 49 帧!应用程序可能在其主线程上做了太多工作。”丢帧的数量随着每次新的旋转而增加。如果数据显示在 Activity 本身中(即不在 fragment 中),则不会发生这种情况。即使我注释掉处理数据的每一行代码并只留下 fragment 的最小骨架,问题仍然存在。 我不知道我做错了什么,但由于即使没有处理数据,问题仍然存在,我想这是布局问题?

Activity :

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ButterKnife.bind(this);

FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    SensorDataFragment dataFragment = new SensorDataFragment();
    fragmentTransaction.add(R.id.fragment_container, dataFragment).commit();

fragment :

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater
            .inflate(R.layout.sensor_data_fragment, container, false);
    return rootView;
}

Activity 布局:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll"/>

</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:id="@+id/device_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/appbar_layout"
    android:visibility="gone"
    tools:visibility="visible"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>

</androidx.core.widget.NestedScrollView>

<RelativeLayout
    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=".BlinkyActivity"
    tools:ignore="ContentDescription">

    <LinearLayout
        android:id="@+id/progress_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:visibility="gone"
        tools:visibility="visible"
        android:padding="@dimen/activity_horizontal_margin">

        <TextView
            android:id="@+id/connection_state"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="16sp"
            tools:text="@string/state_connecting"/>

        <ProgressBar
            android:id="@+id/progress_bar"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:indeterminate="true"/>
    </LinearLayout>

    <include
        android:id="@+id/not_supported"
        layout="@layout/info_not_supported"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone"
        tools:visibility="visible"/>

</RelativeLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

fragment 布局:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.cardview.widget.CardView
        android:id="@+id/spect_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:background="@android:color/white"
        app:cardElevation="1dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/spect_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/spect_tool_bar"
                android:layout_width="0dp"
                android:layout_height="?actionBarSize"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:logo="@drawable/outline_opacity_24"
                app:title="@string/title_spectrum"
                app:titleMarginStart="@dimen/toolbar_title_margin" />

            <TextView
                android:id="@+id/spect_summary"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:paddingStart="@dimen/activity_horizontal_margin"
                android:paddingEnd="@dimen/activity_horizontal_margin"
                android:text="@string/spectrum_summary"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spect_tool_bar" />

            <com.github.mikephil.charting.charts.LineChart
                android:id="@+id/spect_chart"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingTop="@dimen/activity_horizontal_margin"
                android:paddingBottom="0dp"
                app:layout_constraintTop_toBottomOf="@+id/spect_summary" />

            <ImageView
                android:id="@+id/menu_numberformat"
                android:contentDescription="@string/menu_number_format_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingTop="@dimen/activity_horizontal_margin"
                android:paddingEnd="@dimen/activity_horizontal_margin"
                android:paddingStart="@dimen/activity_horizontal_margin"
                android:onClick="showNumberFormatPopup"
                app:srcCompat="@drawable/outline_more_horiz_24"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/spect_chart" />

            <TextView
                android:id="@+id/sensor_label_1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_horizontal_margin"
                android:paddingBottom="5dp"
                android:text="@string/sensor_label_1"
                app:layout_constraintLeft_toRightOf="@+id/f1_label"
                app:layout_constraintTop_toBottomOf="@+id/spect_chart" />

            <TextView
                android:id="@+id/sensor_label_2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_horizontal_margin"
                android:paddingBottom="0dp"
                android:text="@string/sensor_label_2"
                app:layout_constraintLeft_toRightOf="@+id/sensor_label_1"
                app:layout_constraintTop_toBottomOf="@+id/spect_chart" />

            <TextView
                android:id="@+id/sensor_label_3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_horizontal_margin"
                android:paddingBottom="0dp"
                android:text="@string/sensor_label_3"
                app:layout_constraintLeft_toRightOf="@+id/sensor_label_2"
                app:layout_constraintTop_toBottomOf="@+id/spect_chart" />

            <TextView
                android:id="@+id/f1_label"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:paddingEnd="32dp"
                android:paddingStart="@dimen/activity_horizontal_margin"
                android:paddingBottom="0dp"
                android:text="@string/f1_data_label"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/sensor_label_1" />

            <TextView
                android:id="@+id/s1f1_data"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingBottom="0dp"
                android:text="@string/spectrum_unknown"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="@+id/sensor_label_1"
                app:layout_constraintEnd_toEndOf="@+id/sensor_label_1"
                app:layout_constraintLeft_toLeftOf="@+id/sensor_label_1"
                app:layout_constraintTop_toBottomOf="@+id/sensor_label_1" />
// a lot of more text views
</androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

会不会是布局造成的?我的意思是,如果没有处理任何数据,还有什么可能导致问题?非常感谢您的帮助!

最佳答案

编舞者只能检测跳帧。它无法说明为什么会发生这种情况。

但我认为问题的发生是因为在您的 fragment 中处理了大量数据

最好的方法是在与主 UI 线程分开的线程中进行所有处理,以访问您的数据然后绘制它

还有这种重复 View 的行为,例如具有相同属性的“TextView”,这对用户 RecyclerView 来说并不是一个好主意,并将您的数据列表传递给回收器适配器

关于android - 屏幕旋转时导致跳帧的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54178946/

相关文章:

安卓 SQLite : Index -1 requested with a size of 1

android logcat "android.permission.LOCATION_HARDWARE "安全异常

android - 在 recyclerview fragment 中实现上下文操作模式的问题

android - 加载第一个列表项时在第二个 fragment 中加载项目

android - 如何使用 ORMLite 更新列

android - 不同版本的自定义组件

android - 滑动tabs的时候,fragment的oncreateView没有被调用,怎么每次都调用oncreateView

android - 从android中的 fragment 更改当前选项卡

java - android map api 无法从 Fragment 转换为 MapFragment

android - 自定义适配器不适用于 fragment 中的 RecyclerView