在 Android 中实现视图绑定的问题

Issue with implementing View Binding in Android

我正在尝试将视图绑定到我的 activity 之一。问题是我无法访问视图,它显示未解决的引用。

现在当我使用 tools:viewBindingIgnore="true" 然后视图没有问题,我可以访问它们,但是我得到 activity class.

的未解析符号

activity_single_movie.xml

<?xml version="1.0" encoding="utf-8"?>

<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:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/text_error"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="Connection gone!!"
        android:textColor="@color/white"
        android:visibility="gone" />

    <ScrollView
        app:layout_constraintTop_toTopOf= "parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/movie_poster"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:adjustViewBounds="true"
                android:layout_gravity="center"
                android:scaleType="fitCenter"
                android:src="@drawable/placeholder_image"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_margin="8dp" >

                <TextView
                    android:id="@+id/movie_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="movie"
                    android:textStyle="bold"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/movie_tagline"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="sub title"
                    android:textStyle="bold"
                    android:textSize="14sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="movie info"
                    android:textStyle="bold"
                    android:textSize="14sp" />


                //Layout for Release Date
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="Release Date: "
                        android:textSize="12sp"
                        style="bold" />

                    <TextView
                        android:id="@+id/movie_release_date"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="2019"
                        android:textSize="12sp"
                        style="bold" />

                </LinearLayout>

                //Layout for Rating
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="Rating: "
                        android:textSize="12sp"
                        style="bold" />

                    <TextView
                        android:id="@+id/movie_rating"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="5/10"
                        android:textSize="12sp"
                        style="bold" />

                </LinearLayout>

                //Layout for Runtime
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="Runtime: "
                        android:textSize="12sp"
                        style="bold" />

                    <TextView
                        android:id="@+id/movie_runtime"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="2hrs 13mins"
                        android:textSize="12sp"
                        style="bold" />

                </LinearLayout>

                //Layout for Budget

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="Budget: "
                        android:textSize="12sp"
                        style="bold" />

                    <TextView
                        android:id="@+id/movie_budget"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="0Million"
                        android:textSize="12sp"
                        style="bold" />

                </LinearLayout>

                //Layout for revenue
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="Budget: "
                        android:textSize="12sp"
                        style="bold" />

                    <TextView
                        android:id="@+id/movie_revenue"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="2dp"
                        android:text="0Million"
                        android:textSize="12sp"
                        style="bold" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

在SingleMovie.kt

package com.example.movieapp.ui.single_movie_details

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.example.movieapp.R
import com.example.movieapp.data.api.POSTER_BASE_URL
import com.example.movieapp.data.api.TheMovieDBClient
import com.example.movieapp.data.api.TheMovieDBInterface
import com.example.movieapp.data.repository.NetworkState
import com.example.movieapp.data.vo.MovieDetails
import com.example.movieapp.databinding.ActivitySingleMovieBinding
import java.text.NumberFormat
import java.util.*

class SingleMovie : AppCompatActivity() {

private lateinit var viewModel: SingleMovieViewModel
private lateinit var movieRepository: MovieDetailsRepository
private lateinit var binding: ActivitySingleMovieBinding



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    binding = ActivitySingleMovieBinding.inflate(layoutInflater)
    val view = binding.root
    setContentView(view)

    val movieId: Int = intent.getIntExtra("id", 1)

    val apiService: TheMovieDBInterface = TheMovieDBClient.getClient()
    movieRepository = MovieDetailsRepository(apiService)

    viewModel = getViewModel(movieId)
    viewModel.movieDetails.observe(this, Observer {
        bindUi(it)
    })

    viewModel.movieDetailsNetworkState.observe(this, Observer {

        binding.progress_bar.setVisibility(if (it == NetworkState.LOADING) View.VISIBLE else View.GONE)
        binding.text_error.setVisibility( if(it == NetworkState.ERROR) View.VISIBLE else View.GONE)
    })
}

fun bindUi(it: MovieDetails){

    binding.movie_title.setText(it.title)
    binding.movie_tagline.setText(it.tagline)
    binding.movie_release_date.setText(it.releaseDate)
    binding.movie_rating.setText(it.voteAverage.toString())
    binding.movie_runtime.setText(it.runtime.toString() + "minutes")

    val formatCurrency: NumberFormat = NumberFormat.getCurrencyInstance(Locale.US)
    binding.movie_budget.setText(formatCurrency.format(it.budget))
    binding.movie_revenue.setText(formatCurrency.format(it.revenue))

    //for poster of movie
    val moviePosterURL: String = POSTER_BASE_URL + it.posterPath
    Glide.with(this)
        .load(moviePosterURL)
        .into(binding.movie_poster)

}


private fun getViewModel(movieId: Int): SingleMovieViewModel {

    return ViewModelProviders.of(this, object: ViewModelProvider.Factory {

        override fun <T : ViewModel?> create(modelClass: Class<T>): T {
            return SingleMovieViewModel(movieRepository, movieId) as T
        }

    }) [SingleMovieViewModel::class.java]

}
}

使用 tools:viewBindingIgnore="true"

时出错
Unresolved reference: ActivitySingleMovieBinding

不使用tools:viewBindingIgnore="true"

的错误
Unresolved reference: progress_bar

所有视图都相似

View Binding 将下划线转换为驼峰式,正确的语法是 binding.progressBar

<ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:visibility="gone"/>
binding.progressBar.setVisibility(if (it == NetworkState.LOADING) View.VISIBLE else View.GONE)