簡體   English   中英

包含viewpager的android活動緩慢打開

[英]android activity containing viewpager opening slowly

我正在嘗試從一項活動導航到另一項活動。 第二個活動有一個包含三個片段的viewpager。 其中兩個片段有很多視圖,而其中一個視圖較少。 我試圖在具有很多視圖的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需要花費很多時間。 有人可以幫忙解決此問題,謝謝!

  • 重量 不宜太多使用。 我看到您在布局中使用了過多的 android:layout_weight 這會導致加載布局緩慢。 因為它需要根據設備屏幕調整視圖。

    因此,不要使用過多的權重並使用RelativeLayoutConstraintLayout。

  • 不要在onCreate()onCreateView()編寫任何邏輯,數據獲取,視圖管理。
  • 在Fragment的onViewCreated()中執行所有findViewById() 另請閱讀片段生命周期以獲得更好的理解。
  • 如果要調用API,則在onViewCreated()onViewCreated() 延遲 onViewCreated() 調用它。 它將讓視圖首先創建,然后設置從API接收的數據。
  • 請注意,您不在 UI或主線程中進行任何長時間的操作或邏輯。 這應該在工作線程(AsyncTask或new Thread)中完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM