为什么此布局需要2-7秒才能在用户界面中呈现?

时间:2019-02-12 18:56:25

标签: android performance android-layout kotlin kotlinx.coroutines

所以我有一个带有RecyclerView的布局,它需要2到7秒的时间来加载7个项目并将它们显示在列表中。

我使用Java示例方法在CPU上运行了Android Studio探查器,看来大部分等待时间都发生在图形绘制和视图测量中。来自FrameDisplayEventReceiver的60%inRun(),并由第一个onMeasure()调用嵌套在该调用下53%。

如果我错了,请纠正我,但看来速度缓慢的原因是XML布局设计不良。

任何人都可以显示我在此布局中所做的使它运行缓慢以及如何提高效率的事情吗?

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/id_1"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_constraintTop_toTopOf="parent"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
        <ProgressBar
            android:id="@+id/id_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminateTint="#795548"
            android:indeterminateTintMode="src_in"
            android:layout_gravity="end"
            />
    </androidx.appcompat.widget.Toolbar>

    <LinearLayout
        android:id="@+id/id_2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintTop_toBottomOf="@id/id_1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:ignore="UseCompoundDrawables">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:srcCompat="@drawable/image_1"
            android:contentDescription="@string/description" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:text="@string/text_1" />
    </LinearLayout>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/id_4"
        android:name="com.company.MyFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/white"

        app:layoutManager="LinearLayoutManager"
        app:layout_constraintTop_toBottomOf="@id/id_1"
        tools:context="MyFragment"
        tools:listitem="@layout/fragment_routeitem"
        android:visibility="gone"/>


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/id_5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:background="?attr/colorPrimary"
        android:tint="@color/white"
        app:backgroundTint="?attr/colorPrimary"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:rippleColor="?attr/colorPrimary"
        app:srcCompat="@drawable/ic_add_white_24dp" />


</androidx.constraintlayout.widget.ConstraintLayout>

这是请求代码(Kotlin)

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
    val view = inflater.inflate(R.layout.fragment_routeitem_list, container, false)
    val toolbar: Toolbar = view.findViewById(R.id.toolbar)
    toolbar.title = resources.getString(R.string.my_route_title)
    progressBar = view.findViewById(R.id.my_routes_loading)
    emptyStateView = view.findViewById(R.id.empty_state)
    recyclerView = view.findViewById(R.id.my_routs_list)
    dataAdapter = MyRouteItemRecyclerViewAdapter(mutableListOf(), this)
    recyclerView.layoutManager = LinearLayoutManager(context)
    recyclerView.adapter = dataAdapter
    startLoadData()

    val newRouteFab: FloatingActionButton = view.findViewById(R.id.new_route_fab)
    newRouteFab.setOnClickListener {
        createNewRoute()
    }


    return view
}



fun startLoadData() {
    launch{
        val routeList: List<Route> = withContext(Dispatchers.IO){
            Application.getDaoSession()
                    .routeDao
                    .queryBuilder()
                    .where(RouteDao.Properties.Id.isNotNull)
                    .orderDesc(RouteDao.Properties.VisibleModified)
                    .build().list()
        }
        setData(routeList.toMutableList())
        progressBar.visibility = View.GONE
    }
}

0 个答案:

没有答案