新片段打开后,Kotlin FAB仍然可以点击

时间:2019-02-02 22:05:08

标签: android-fragments kotlin floating-action-button

我有一个布局,可以通过fab click打开一个新片段。我遇到的问题是,在打开新片段时,晶圆厂保持可点击状态。它不可见,但是如果我单击应有的位置-它会显示并按照程序执行操作。

这是我的代码: Activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">


    <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:elevation="0dp"
            android:clickable="true" app:srcCompat="@drawable/ic_add_black_24dp"
            android:id="@+id/fabHome" android:layout_marginBottom="15dp"
            app:layout_constraintBottom_toBottomOf="parent" android:layout_marginEnd="15dp"
            app:layout_constraintEnd_toEndOf="parent" android:focusable="true"/>

    <android.support.v7.widget.RecyclerView
            android:layout_width="0dp"
            android:id="@+id/recyclerMain"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="10dp"
            android:layout_marginEnd="10dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginTop="10dp"
            app:layout_constraintTop_toTopOf="parent"
    />

    <TextView
            android:id="@+id/empty_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:visibility="gone"
            android:text="@string/no_lists_created_yet_press_the_icon"/>

    <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
                 android:id="@+id/fragmentHolder"/>

</android.support.constraint.ConstraintLayout>

mainActivity.kt

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.internationalwoolly.shoppinglist.fragments.CreateNewList
import com.internationalwoolly.shoppinglist.recyclerview.RecyclerMainAdapter
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // controls if recycler view is shown
        if (RecyclerMainAdapter().isRecyclerViewEmpty()) {
            empty_view.visibility = View.VISIBLE
            recyclerMain.visibility = View.GONE
        }
        recyclerMain.layoutManager = LinearLayoutManager(this)
        recyclerMain.adapter = RecyclerMainAdapter()
        //ends controls if recycler view is shown

        //controls the fab on the main page
        fabHome.setOnClickListener {
            val fragment = CreateNewList()
            supportFragmentManager.beginTransaction()
                .replace(R.id.fragmentHolder, fragment).addToBackStack("back_profile_fragment").commit()
        }
    }
}

1 个答案:

答案 0 :(得分:0)

通过在片段xml中添加可点击和可聚焦的方式修复了该问题