片段/ Kotin中的数据绑定

时间:2019-12-27 08:58:01

标签: android android-fragments android-databinding

fragmentOne.kt



import android.icu.lang.UCharacter.GraphemeClusterBreak.T
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.navigation.fragment.findNavController

import androidx.databinding.DataBindingUtil
import androidx.navigation.Navigation
import com.example.twoscreens.databinding.FragmentOneBinding

/**
* A simple [Fragment] subclass.
*/
class fragmentOne : Fragment() {


   override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
       // Inflate the layout for this fragment

        val binding=DataBindingUtil.inflate<fragmentOneBinding>(layoutInflater,R.layout.fragment_one,container,false)
       binding.next_screen.setOnClickListener { view: View? ->
           if (view != null) {
               Navigation.findNavController(view).navigate(R.id.action_fragment_one2_to_fragment_two2)
           }
       }

       return binding.root

   }


}

我希望您可以在方括号中看到FragmentOneBinding,因为它显示错误为 unresolved reference (未解决的引用),而xml名称不是fragment_one.xml

fragment_one.xml

<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:id="@+id/fragment_one"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragmentOne">

    <!-- TODO: Update blank fragment layout -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/next_screen"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:gravity="bottom|center_horizontal|center_vertical|fill_vertical"
        android:text="Next Screen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

请指导我并告知错误 另外,如果您想查看其他文件在评论中说

1 个答案:

答案 0 :(得分:0)

在xml中:

<layout>
    <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:id="@+id/fragment_one"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragmentOne">

        <!-- TODO: Update blank fragment layout -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <Button
                android:id="@+id/next_screen"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="16dp"
                android:gravity="bottom|center_horizontal|center_vertical|fill_vertical"
                android:text="Next Screen"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

在fragmentOne类中:

 val binding = DataBindingUtil.inflate<FragmentOneBinding>(layoutInflater,R.layout.fragment_one,container,false)

您可以重建android进行检查