7

我想用 ViewBinding 创建一个片段,但我的代码不起作用。我阅读了 ViewBinding 文档,但我的片段没有显示。这是我的代码:

片段播放器.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Test fragment"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

layout_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/fragment_player"
    class="com.test.plo.view.PlayerFragment"
    android:name="com.test.plo.view.PlayerFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

播放器片段.java

 public class PlayerFragment extends Fragment {

   private FragmentPlayerBinding fragmentPlayerBinding;

   @Nullable
   @Override
   public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

       fragmentPlayerBinding = FragmentPlayerBinding.inflate(inflater, container, false);
       View view = fragmentPlayerBinding.getRoot();
       return view;
   }

  @Override
  public void onDestroyView() {
      super.onDestroyView();
      fragmentPlayerBinding = null;
  }

}

谁能帮我?

4

2 回答 2

0

首先,在您的应用程序级别中放入以下代码行

    android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 19
        targetSdkVersion 30
        versionCode 1          
        versionName "1.0"       
        multiDexEnabled true
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    testOptions {
        unitTests.returnDefaultValues = true
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
    //Enable View Bindings to true
    buildFeatures {
        viewBinding = true
    }
}

然后通过右键单击“文件”->新建->片段->选择空白片段(或您想要的任何片段)在您的项目中添加新片段。这将创建您的片段类文件以及与之关联的 xml 文件。
我创建了名为“HomeFragment.kt”的空白片段,其 xml 文件的名称为“fragment_home.xml”。

在我的 xml 文件中,我添加了一个 textview 和 Button(您可以在此处定义您的小部件 - 一种设计布局),代码如下所示。

“fragment_home.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:layout_width="match_parent"
    android:layout_height="match_parent">



  <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">


                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/mTv_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Home Fragment"
                    android:textColor="@color/main_black"
                    android:lineSpacingExtra="@dimen/dimen_2dp"
                    android:gravity="center" />


 <com.google.android.material.button.MaterialButton
                    android:id="@+id/mBtn_homeButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Click Here!"
                    android:layout_gravity="center"
                    android:textSize="@dimen/dimen_20sp"
                    android:textAllCaps="false"
                    app:cornerRadius="@dimen/dimen_50dp"
                    android:paddingLeft="@dimen/dimen_50dp"
                    android:paddingRight="@dimen/dimen_50dp"
                    android:paddingTop="12dp"
                    android:paddingBottom="12dp" />

  </androidx.appcompat.widget.LinearLayoutCompat>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

最后在您的片段类中,您可以使用 viewBinding 绑定这些属性。 “家庭片段.kt”

    class HomeFragment : Fragment() {

    private lateinit var homeViewModel: HomeViewModel
    // view binding will create an Class for your xml file, you can use it directly by this "FragmentHomeBinding" as my xml file name is "fragment_home.xml" 
 // create a variable of your xml binding class
    private lateinit var binding : FragmentHomeBinding 

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments!=null) {
            Log.i(tag, "onCreate: ")
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {

        homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
        //val root = inflater.inflate(R.layout.fragment_home, container, false)
        binding = FragmentHomeBinding.inflate(inflater, container, false)

        // observe your text change by using viewModel
        homeViewModel.text.observe(viewLifecycleOwner, Observer {
            //you can use your view/widgets by using the binding object like this --- 
            binding.mTvTitle.text = it
        })

       // button click event -- using viewBinding 
       binding.mBtnHomeButton.setOnClickListener {
            Toast.makeText(context, "Hello..!", Toast.LENGTH_SHORT).show()
        }
       

        return binding.root
    }
}

干杯..!

于 2021-10-20T12:27:32.023 回答
0

你有没有试过把:

android {
    ...
    buildFeatures {
    viewBinding true
    }
}

在您的应用程序 build.gradle 中?

于 2021-10-20T11:42:08.080 回答