0

我正在尝试从一项活动导航到另一项活动。第二个活动有一个具有三个片段的浏览器。其中两个片段有很多视图,其中一个片段视图较少。我尝试在 viewpager 中只保留一个具有很多视图的片段。加载需要一些时间。我尝试了另一个有很多观点的。加载也需要时间。但是当我保留视图较少的片段时,花费的时间更少。如果我一次全部加载,则再次加载需要大量时间。我首先在两个片段上都有很多视图层次结构,但我最终使用表格布局将它们全部删除。活动呈现缓慢。片段 1:

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TableRow style="@style/TableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                </TableRow>

                <TableRow style="@style/TableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/white" />

                </TableRow>

                <TableRow style="@style/TableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1.5"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                </TableRow>


                <TableRow style="@style/TableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1.5" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/white">

                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/white" />

                </TableRow>

                <TableRow
                    android:id="@+id/ll_nfo1"
                    style="@style/TableRowStyle"
                    android:visibility="visible">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow android:id="@+id/ll_nfo2"
                    style="@style/TableRowStyle"
                    android:visibility="visible">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                </TableRow>

                <TableRow style="@style/BidOfferTableRowStyle">

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"/>

                    <com.intmilli.imobile.Views.CustomTextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1" />

                </TableRow>

            </TableLayout>


        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:id="@+id/ll_bottom"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dimen_40">

        <com.intmilli.imobile.Views.CustomTextView
            android:id="@+id/tv_h_first"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"/>

        <View
            android:id="@+id/view_line3_left"
            android:layout_height="match_parent"
            android:layout_toRightOf="@id/tv_h_first"
            android:background="@color/mainbg_lightblue" />

        <com.intmilli.imobile.Views.CustomTextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

片段 3:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true">

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="4dp"
            android:paddingBottom="4dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <Spinner
                android:id="@+id/day_spinner"
                android:layout_width="0dp"
                android:layout_height="34dp" />

            <Spinner
                android:id="@+id/limit_spinner"
                android:layout_width="0dp"
                android:layout_height="34dp" />

            <Spinner
                android:id="@+id/buy_spinner"
                android:layout_width="0dp"
                android:layout_height="34dp" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageButton
                android:id="@+id/sub_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3" />

            <EditText
                android:id="@+id/et_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"/>

            <ImageButton
                android:id="@+id/add_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3"/>

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1" />

            <ImageButton
                android:id="@+id/sub_price"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3" />

            <EditText
                android:id="@+id/et_pricevalue"
                android:layout_width="0dp"
                android:layout_height="36dp" />

            <ImageButton
                android:id="@+id/add_price"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageButton
                android:id="@+id/sub_dis_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3"/>

            <EditText
                android:id="@+id/et_dis_qty"
                android:layout_width="0dp"
                android:layout_height="36dp" />

            <ImageButton
                android:id="@+id/add_dis_qty"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3"/>


        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp">

            <com.intmilli.imobile.Views.CustomTextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>

            <ImageButton
                android:id="@+id/sub_tri_price"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3"/>

            <EditText
                android:id="@+id/et_triggerPrice"
                android:layout_width="0dp"
                android:layout_height="36dp" />

            <ImageButton
                android:id="@+id/add_tri_price"
                android:layout_width="0dp"
                android:layout_height="36dp"
                android:layout_weight="0.3" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="6dp">

            <CheckBox
                android:id="@+id/ch_stopLoss"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <CheckBox
                android:id="@+id/ch_IOC"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

            <com.intmilli.imobile.Views.CustomTextView
                android:id="@+id/tv_placeOrderBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </TableRow>

    </TableLayout>

</ScrollView>

正如我所说的片段 1 和 3 需要很多时间。有人可以帮忙解决这个问题吗谢谢!

4

1 回答 1

2
  • 权重不是很好用。我看到你在布局中使用了太多 android:layout_weight。这会导致加载布局缓慢。因为它需要根据设备屏幕调整视图。

    所以不要使用太多的权重并使用RelativeLayoutConstraintLayout。

  • 不要在内部onCreate()onCreateView().

  • 做所有findViewById()onViewCreated()片段。另请阅读片段生命周期以更好地理解。

  • 如果您正在调用APIonViewCreated(). 它会先创建视图,然后设置从 API 接收的数据。

  • 请注意,您没有在 UI 或主线程中执行任何长操作或逻辑。这应该在Worker Thread中完成,例如AsyncTask或 new Thread。

于 2018-11-14T07:50:26.090 回答