温馨提示:本文翻译自stackoverflow.com,查看原文请点击:kotlin - How can I use my custom image view with DataBinding in Android?
android android-custom-view android-databinding android-jetpack kotlin

kotlin - 如何在Android的DataBinding中使用自定义图像视图?

发布于 2020-04-20 14:51:34

因此,我检查了Google的此代码实验室,RatioImageView在Android中创建自定义视图它只是扩展ImageView并OnMeasure()根据设备的视口的宽高比覆盖该方法。相同的代码是:

class RatioImageView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
): ImageView(context, attrs, defStyleAttr) {

    private val VP_HEIGHT = Resources.getSystem().displayMetrics.heightPixels
    private val VP_WIDTH = Resources.getSystem().displayMetrics.widthPixels
    private val HWR: Float = VP_HEIGHT.toFloat()/VP_WIDTH.toFloat() //height to width ratio


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),(MeasureSpec.getSize(widthMeasureSpec)*HWR).toInt())
    }
}

然后我在ListItem视图中将其用作:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp">

        <com.example.myapp.RatioImageView
            android:id="@+id/target"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:contentDescription="image holder"
            android:scaleType="centerCrop"
            android:background="#757575"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintDimensionRatio="9:16" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

然后在Adapter中,我使用DataBinding如下内容更新视图:

override fun onBindViewHolder(holder: HomeItemHolder, position: Int) {
        holder.binding.apply {

              //loadImage() is an extension function added to ImageView class
              target.loadImage(UiUtil.getCustomUrl(photos[position].urls.fullImage, height, width))
       
            root.setOnClickListener {
                handleClick(position)
            }
        }
}

在构建过程中,它显示以下错误:

Cannot access class 'RatioImageView'. Check your module classpath for missing or conflicting dependencies

即对于target.loadImage(...)OnBindViewHolder()中编写的行

另外,如果我不将DataBinding用于根布局,那么它可以正常工作。

所以问题是需要添加RatioImageView什么?为了使其与DataBinding一起使用,请考虑到这里我不需要将XML布局与View类关联。

这是 onCreateViewHolder()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeItemHolder {
        return HomeItemHolder(ItemPhotoListLayoutBinding.inflate(LayoutInflater.from(parent.context)))
    }

下面是ViewHolder该类:

inner class HomeItemHolder (val binding: ItemPhotoListLayoutBinding):
        RecyclerView.ViewHolder(binding.root) {
    }

另外,如果我的代码在此处缺少某些内容,这里是回购:https : //github.com/prafullmishra/CustomImageView

查看更多

提问者
Lincoln
被浏览
5
Lisa Wray 2020-02-22 09:20

我在您发布在Github上的代码示例中发现了问题。实际上,从您发布的摘录中很难解决这个问题,但是该错误正确地表明了类名的问题。

您的XML引用了该类 com.example.bindingcustomimageview.CustomImage.RatioImageView一无所知,这RatioView看起来像是CustomImage包中的一个公共内部类com.example.bindingcustomimageview

但事实并非如此。它是RatioImageView包中的类com.example.bindingcustomimageview.CustomImage(其中“ CustomImage”是大写的包名称)。您可以通过将其拖到RatioImageView与MainActivity相同的程序包中来快速修复它,然后一切都会正常运行。

为避免将来出现此类混乱,请勿使用大写字母命名您的软件包。