即使可点击属性为真,FrameLayout onClick 事件也不会触发

时间:2020-12-20 18:44:52

标签: android kotlin onclick android-framelayout

我正在尝试显示一个加载视图,它包含一个 ProgressBar。当我单击 FrameLayout 时,不会触发 FrameLayout 的 onClickListener 方法。我可以单击 FrameLayout 后面的任何内容。在许多主题中,可点击属性是推荐的,但它对我不起作用。我错过了什么吗?由于篇幅原因,我裁剪了主要代码部分,如下所示。

我尝试了什么?

  • 在 XML 上将 clickable 属性设置为 true,在 FrameLayout 上将其设置为 setOnClickListener。

loading_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/flLoadingView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    **android:clickable="true"**
    android:alpha="0.4"
    android:animateLayoutChanges="true"
    android:background="@color/loadingView"
    android:visibility="gone">

    <ProgressBar
        android:id="@+id/pbCalculate"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:indeterminate="true"
        **android:clickable="false"**
        android:minWidth="250dp"
        android:minHeight="25dp" />
</FrameLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    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:id="@+id/colMain"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/clSuperMainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/layoutBackground">

        **<include layout="@layout/layout_loading"/>**

        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId=""
            ads:layout_constraintBottom_toBottomOf="parent"
            ads:layout_constraintLeft_toLeftOf="parent"
            ads:layout_constraintRight_toRightOf="parent"/>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/clFormView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@+id/adView"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true">

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/clSubMainLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    tools:context=".MainActivity">

                    <!-- Guide Lines -->

                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/tilMainAmount"
                        style="@style/TextInputLayoutStyle"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:hint="@string/main_amount"
                        app:errorEnabled="true"
                        app:layout_constraintLeft_toRightOf="@+id/glStart"
                        app:layout_constraintRight_toLeftOf="@+id/glEnd"
                        app:layout_constraintTop_toBottomOf="@+id/glHorizontal">

                        <com.google.android.material.textfield.TextInputEditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:inputType="numberDecimal"
                            android:maxLength="12"/>

                    </com.google.android.material.textfield.TextInputLayout>

                    <!-- Other text input layouts. -->

                    <com.google.android.material.button.MaterialButton
                        android:id="@+id/mbtnCalculate"
                        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginTop="8dp"
                        android:text="@string/calculate"
                        android:textColor="@color/buttonStroke"
                        app:cornerRadius="4dp"
                        app:layout_constraintEnd_toStartOf="@+id/glEnd"
                        app:layout_constraintStart_toStartOf="@+id/glVertical"
                        app:layout_constraintTop_toBottomOf="@+id/llPeriod"
                        app:rippleColor="@color/buttonStroke"
                        app:strokeColor="@color/buttonStroke" />

                </androidx.constraintlayout.widget.ConstraintLayout>
            </ScrollView>
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
class MainActivity : AppCompatActivity() {

    private lateinit var tilMainAmount: TextInputLayout
    private lateinit var mbtnCalculate: MaterialButton

    private lateinit var coordinatorLayout: CoordinatorLayout
    private lateinit var clSuperLayout: ConstraintLayout
    private lateinit var clFormView: ConstraintLayout
    private lateinit var flLoadingView: FrameLayout

    private var inAnimation = AlphaAnimation(0f, 1f)
    private var outAnimation = AlphaAnimation(1f, 0f)

    private val uiScope = CoroutineScope(Dispatchers.Main)
    private var isValid = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
       
        coordinatorLayout = findViewById(R.id.colMain)

        tilMainAmount = findViewById(R.id.tilMainAmount)
        
        clSuperLayout = findViewById(R.id.clSuperMainLayout)
        clFormView = findViewById(R.id.clFormView)
        flLoadingView = findViewById(R.id.flLoadingView)

        inAnimation.duration = 200
        outAnimation.duration = 200

        mbtnCalculate.setOnClickListener {
            uiScope.launch {
                flLoadingView.animation = inAnimation
                flLoadingView.visibility = View.VISIBLE
                isValid = checkInputs()
                if (isValid) {
                    calculate()
                }
                //flLoadingView.animation = outAnimation
                //flLoadingView.visibility = View.GONE
            }
        }

        flLoadingView.setOnClickListener {
            **println("Loading frame layout clicked.")** //I checked in debug mode, this line is not triggering.
        }

    }

    private fun checkInputs(): Boolean {
        ...
    }

    private suspend fun calculate() {
        ...
    }

}

0 个答案:

没有答案