通过Glide

时间:2018-03-20 10:07:53

标签: android kotlin android-glide bottom-sheet

我希望动态更改ImageView中的BottomSheet内容。问题是,当我使用Glide将图片加载到我的ImageView时,它会触发自己的requestLayout(),结果 - BottomSheet消失了。如果我评论Glide加载代码,那么everithing工作正常。我的意思是TextView更改文字而BottomSheet不会消失。

我应该如何处理图像加载?

这是我的代码

bottom_sheet_item.xml

<android.support.constraint.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="@dimen/_160sdp"
    style="@style/MinElevationStyle"
    android:background="@color/colorHint"
    android:layout_gravity="bottom"
    app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:contentDescription="@null"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/imageViewItem"
        android:scaleType="fitXY"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:contentDescription="@null"
        tools:text="some text"
        android:gravity="center"
        style="@style/TextSmall"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/textView"
        android:textColor="@android:color/white"/>

</android.support.constraint.ConstraintLayout>

fragment.xml之

<android.support.design.widget.CoordinatorLayout


  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.trader.marinentrader.ui.search.searchresults.map.MapWithItemsFragment">

    <fragment
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/map"
        tools:context=".ui.search.searchresults.SearchResultsActivity"
        android:name="com.google.android.gms.maps.SupportMapFragment" />

    <include
        android:id="@+id/bottomSheetItem"
        layout="@layout/bottom_sheet_item" />

</android.support.design.widget.CoordinatorLayout>

和exec代码

 private fun bindItem(item: ItemEntity){
    val imagePath = item.media?.firstOrNull()?.path
    if(imagePath == null){
        imageViewItem.clearGlide()
    }else{
        imageViewItem.load(imagePath)
    }
    textView.text = item.title
}

通过kotlin扩展加载图片

fun ImageView.load(any: Any?, placeholder: Int = 0){
GlideApp
        .with(this)
        .load(any)
        .centerCrop()
        .placeholder(placeholder)
        .fallback(placeholder)
        .transition(DrawableTransitionOptions.withCrossFade())
        .into(this)
}

0 个答案:

没有答案