未在 Fragment 中触发按钮 onclicklistener
Button onclicklistener not fired in Fragment
我正在使用 Kotlin 1.3.61
,kotlin 扩展,androidx.appcompat:appcompat:1.1.0
,androidx.constraintlayout:constraintlayout:1.1.3
,androidx.core:core-ktx:1.1.0
,org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61
并访问片段中我的 xml 中定义的按钮,如下所示:
class MyFragment : Fragment(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i(TAG, "In onCreate()")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.i(TAG, "In onCreateView()")
return inflater.inflate(R.layout.fragment_map_bottom_view, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
whereBtn.setOnClickListener {
Log.i(TAG, "where clicked but first click doesn't register")
}
但是按钮的第一次点击永远不会触发,但第二次和后续点击会触发。
我尝试 @ContainerOptions(CacheImplementation.SPARSE_ARRAY)
获取片段,但没有成功。
这是片段布局文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:backgroundTint="@color/white">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/headerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="16dp"
android:text="@string/navHeaderTitle"
android:textAppearance="@style/ChipTextCaptionFont"
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/whereBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.095"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/whereBtn"
style="@style/DrawerMenuItemsFont"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_below="@+id/headerTitle"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="32dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:drawablePadding="10dp"
android:drawableStart="@drawable/ic_search_24px"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:hint="@string/where_desc"
android:clickable="true"
android:focusable="true"
android:textAppearance="@style/DrawerProfileCaptionFont"
android:textSize="16sp"
android:textColorHint="@color/black"
android:background="@drawable/rect"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.285"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/headerTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
看来whereBtn
是可聚焦的。
没有第一次点击的事件,因为焦点改变已经消耗了事件。
我正在使用 Kotlin 1.3.61
,kotlin 扩展,androidx.appcompat:appcompat:1.1.0
,androidx.constraintlayout:constraintlayout:1.1.3
,androidx.core:core-ktx:1.1.0
,org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61
并访问片段中我的 xml 中定义的按钮,如下所示:
class MyFragment : Fragment(), OnMapReadyCallback {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.i(TAG, "In onCreate()")
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.i(TAG, "In onCreateView()")
return inflater.inflate(R.layout.fragment_map_bottom_view, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
...
whereBtn.setOnClickListener {
Log.i(TAG, "where clicked but first click doesn't register")
}
但是按钮的第一次点击永远不会触发,但第二次和后续点击会触发。
我尝试 @ContainerOptions(CacheImplementation.SPARSE_ARRAY)
获取片段,但没有成功。
这是片段布局文件:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:backgroundTint="@color/white">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/headerTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="16dp"
android:text="@string/navHeaderTitle"
android:textAppearance="@style/ChipTextCaptionFont"
android:textColor="@color/black"
app:layout_constraintBottom_toTopOf="@+id/whereBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.095"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/whereBtn"
style="@style/DrawerMenuItemsFont"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_below="@+id/headerTitle"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="32dp"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:drawablePadding="10dp"
android:drawableStart="@drawable/ic_search_24px"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:hint="@string/where_desc"
android:clickable="true"
android:focusable="true"
android:textAppearance="@style/DrawerProfileCaptionFont"
android:textSize="16sp"
android:textColorHint="@color/black"
android:background="@drawable/rect"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.285"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/headerTitle" />
</androidx.constraintlayout.widget.ConstraintLayout>
看来whereBtn
是可聚焦的。
没有第一次点击的事件,因为焦点改变已经消耗了事件。