繁体   English   中英

如何优化一个复杂的活动有 80 多个视图

[英]How to optimize a complex activity has more than 80 views

概述:我正在尝试创建此活动,其中有X 个主题(在本例中为 12 个)每个主题都有自己的CardView ,每张卡片都有三个TextEdit ,它们计算该主题的平均值并将其显示在该卡片的TextView中,最后单击按钮后,它会显示另一个活动中所有主题的平均值。

问题:所以我对每张卡进行了硬编码,如您所见...(只有两张卡的代码和图像),我收到警告说此活动有超过 80 个视图,这对性能不利...

问题1 :是否有任何解决方案可以优化活动而不使每张卡在单独的活动中?

问题 2 :是否可以在 ListView 元素中创建一个 TextEdit 并分别获取每个 TextEdit 值(用于计算目的)?

提前致谢

界面截图

     fun conculateM1 (ctr1 : Float , ctr2 :Float , synt : Float ) : String{
            val resi = BigDecimal(((ctr1+ctr2+(synt*2))/4).toDouble()).setScale(2, RoundingMode.HALF_EVEN)
            return resi.toString()
        }

        fun doIt (targetX : TextInputEditText ,nf1 : TextInputLayout , nf2 : TextInputLayout , nfx : TextInputLayout ,
                  ng1 :TextInputEditText , ng2 :TextInputEditText , ngx :TextInputEditText , nxr : TextView){

            targetX.addTextChangedListener(object : TextWatcher {

                override fun afterTextChanged(s: Editable) {}
                override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}

                override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

                    if (ng1.text.toString() != "" && ng2.text.toString() != "" && ngx.text.toString() != ""){
                        nxr.text = conculateM1( ng1.text.toString().toFloat() , ng2.text.toString().toFloat() , ngx.text.toString().toFloat())

                        if (ng1.text.toString().length > 5 || ng1.text.toString().toFloat() > 20){
                            nf1.error = "_"
                        }else if(ng1.text.toString().length <= 5 && ng1.text.toString().toFloat() <= 20){
                            nf1.error = null
                        }

                        if (ng2.text.toString().length > 5 || ng2.text.toString().toFloat() > 20){
                            nf2.error = "_"
                        }else if(ng2.text.toString().length <= 5 && ng2.text.toString().toFloat() <= 20){
                            nf2.error = null
                        }

                        if (ngx.text.toString().length > 5 || ngx.text.toString().toFloat() > 20){
                            nfx.error = "_"
                        }else if(ngx.text.toString().length <= 5 && ngx.text.toString().toFloat() <= 20){
                            nfx.error = null
                        }

                    }
                }
            })

        }

        val n1ctr1 = findViewById<TextInputLayout>(R.id.n1_ctr1)
        val n1ctr2 = findViewById<TextInputLayout>(R.id.n1_ctr2)
        val n1synt = findViewById<TextInputLayout>(R.id.n1_synt)

        val n1c1 = findViewById<TextInputEditText>(R.id.n1c1)
        val n1c2 = findViewById<TextInputEditText>(R.id.n1c2)
        val n1s = findViewById<TextInputEditText>(R.id.n1s)

        val n1res = findViewById<TextView>(R.id.n1_res)

        doIt(n1c1 , n1ctr1 , n1ctr2 , n1synt , n1c1 , n1c2 , n1s , n1res)
        doIt(n1c2 , n1ctr1 , n1ctr2 , n1synt , n1c1 , n1c2 , n1s , n1res)
        doIt(n1s , n1ctr1 , n1ctr2 , n1synt , n1c1 , n1c2 , n1s , n1res)

        val n2ctr1 = findViewById<TextInputLayout>(R.id.n2_ctr1)
        val n2ctr2 = findViewById<TextInputLayout>(R.id.n2_ctr2)
        val n2synt = findViewById<TextInputLayout>(R.id.n2_synt)

        val n2c1 = findViewById<TextInputEditText>(R.id.n2c1)
        val n2c2 = findViewById<TextInputEditText>(R.id.n2c2)
        val n2s = findViewById<TextInputEditText>(R.id.n2s)

        val n2res = findViewById<TextView>(R.id.n2_res)

        doIt(n2c1 , n2ctr1 , n2ctr2 , n2synt , n2c1 , n2c2 , n2s , n2res)
        doIt(n2c2 , n2ctr1 , n2ctr2 , n2synt , n2c1 , n2c2 , n2s , n2res)
        doIt(n2s , n2ctr1 , n2ctr2 , n2synt , n2c1 , n2c2 , n2s , n2res)
    <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="@drawable/calcbg"
    tools:context=".CalculationActivity">

    <ScrollView
        android:layout_width="409dp"
        android:layout_height="match_parent"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginBottom="1dp"
    
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0">

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

            <androidx.cardview.widget.CardView
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="15dp">


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

                    <TextView
                        android:id="@+id/matiere1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_marginBottom="5dp"

                        android:gravity="center"
                        android:text="Subject 1"
                        android:textAlignment="center"
                        android:textSize="24sp" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

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

                            <com.google.android.material.textfield.TextInputLayout
                                android:id="@+id/n1_ctr1"
                                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                                android:layout_width="150dp"
                                android:layout_height="70dp"
                                android:layout_gravity="center"
                                android:hint="@string/controle_i"
                                app:errorEnabled="true">

                                <com.google.android.material.textfield.TextInputEditText
                                    android:id="@+id/n1c1"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:inputType="numberDecimal" />
                            </com.google.android.material.textfield.TextInputLayout>

                            <com.google.android.material.textfield.TextInputLayout
                                android:id="@+id/n1_ctr2"
                                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                                android:layout_width="150dp"
                                android:layout_height="70dp"
                                android:layout_gravity="center"
                                android:hint="@string/controle_ii"
                                app:errorEnabled="true">

                                <com.google.android.material.textfield.TextInputEditText
                                    android:id="@+id/n1c2"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    
                                    android:inputType="numberDecimal" />
                            </com.google.android.material.textfield.TextInputLayout>

                            <com.google.android.material.textfield.TextInputLayout
                                android:id="@+id/n1_synt"
                                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                                android:layout_width="150dp"
                                android:layout_height="70dp"
                                android:layout_gravity="center"
                                android:hint="@string/synthese"
                                app:errorEnabled="true">

                                <com.google.android.material.textfield.TextInputEditText
                                    android:id="@+id/n1s"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:inputType="numberDecimal" />
                            </com.google.android.material.textfield.TextInputLayout>

                        </LinearLayout>

                    </LinearLayout>

                    <TextView
                        android:id="@+id/n1_res"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="10dp"
                        android:gravity="center"
                        android:text="@string/_00_00"
                        android:textAlignment="center"
                        android:textSize="24sp" />

                </LinearLayout>

            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="15dp">


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

                    <TextView
                        android:id="@+id/matiere2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="5dp"
                        android:layout_marginBottom="5dp"

                        android:gravity="center"
                        android:text="Subject 2"
                        android:textAlignment="center"
                        android:textSize="24sp" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal">

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

                            <com.google.android.material.textfield.TextInputLayout
                                android:id="@+id/n2_ctr1"
                                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                                android:layout_width="150dp"
                                android:layout_height="match_parent"
                                android:layout_gravity="center"
                                android:layout_marginTop="5dp"
                                android:hint="@string/controle_i">

                                <com.google.android.material.textfield.TextInputEditText
                                    android:id="@+id/n2c1"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    
                                    android:inputType="numberDecimal" />
                            </com.google.android.material.textfield.TextInputLayout>

                            <com.google.android.material.textfield.TextInputLayout
                                android:id="@+id/n2_ctr2"
                                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                                android:layout_width="150dp"
                                android:layout_height="match_parent"
                                android:layout_gravity="center"
                                android:layout_marginTop="5dp"
                                android:hint="@string/controle_ii">

                                <com.google.android.material.textfield.TextInputEditText
                                    android:id="@+id/n2c2"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    
                                    android:inputType="numberDecimal" />
                            </com.google.android.material.textfield.TextInputLayout>

                            <com.google.android.material.textfield.TextInputLayout
                                android:id="@+id/n2_synt"
                                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                                android:layout_width="150dp"
                                android:layout_height="match_parent"
                                android:layout_gravity="center"
                                android:layout_marginTop="5dp"
                                android:hint="@string/synthese">

                                <com.google.android.material.textfield.TextInputEditText
                                    android:id="@+id/n2s"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    
                                    android:inputType="numberDecimal" />
                            </com.google.android.material.textfield.TextInputLayout>

                        </LinearLayout>

                    </LinearLayout>

                    <TextView
                        android:id="@+id/n2_res"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:layout_marginBottom="10dp"
                        
                        android:gravity="center"
                        android:text="@string/_00_00"
                        android:textAlignment="center"
                        android:textSize="24sp" />

                </LinearLayout>

            </androidx.cardview.widget.CardView>
            
            

            <Button
                android:id="@+id/button"
                android:layout_width="350dp"
                android:layout_gravity="center"
                android:layout_height="wrap_content"
                app:cornerRadius="90dp"
                android:text="@string/ehseb" />

        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

无需手动创建 80 个卡片视图,只需使用回收器视图,这将对优化有很大帮助,还可以让您访问适配器中的每个 textView。 您还可以通过使用和实现分页库来优化回收器视图呈现时间

暂无
暂无

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

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