我有以下视图来展示品牌项目水平滚动...
当我向上滚动时,onLoadFail()会触发,但如果我评论onLoadFail(),那么每件作品都会如此期待..这里发生了什么!?
使用onLoadFail()
// Setup product image
Glide.with(context)
.load(item.shopImageURL)
.into(holder.imgBrand)
.onLoadFailed(ContextCompat.getDrawable(context, android.R.drawable.ic_dialog_alert))
GIF链接:google drive
没有onLoadFail()
// Setup product image
Glide.with(context)
.load(item.shopImageURL)
.into(holder.imgBrand)
GIF链接:google drive
LoveBrandView.kt
class LoveBrandView : LinearLayout {
constructor(context: Context?) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
private val adapter = LoveBrandAdapter()
init {
// Set layout orientation to vertical
orientation = VERTICAL
setBackgroundColor(Color.parseColor("#262626"))
val inflater = LayoutInflater.from(context)
inflater.inflate(R.layout.view_home_highlight_brand, this, true)
// Attach the adapter to the recyclerview to populate items
rcvLoveBrand.adapter = adapter
// Set layout manager to position the items
rcvLoveBrand.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
// Snap list item after scroll on the gravity start of screen
val snapHelperTop = GravitySnapHelper(Gravity.START)
snapHelperTop.attachToRecyclerView(rcvLoveBrand)
adapter.setListItem(mock())
adapter.notifyDataSetChanged()
}
fun mock(): MutableList<ShopItem> {
val items = mutableListOf<ShopItem>()
items.add(ShopItem(shopImageURL = "https://static.webshopapp.com/shops/208373/files/133254608/500x300x2/calvin-klein.jpg"))
items.add(ShopItem(shopImageURL = "https://lux-store.com/image/catalog/PopularBrands/brand-logo-9.jpg"))
items.add(ShopItem(shopImageURL = "https://logoeps.com/wp-content/uploads/2011/08/uniqlo-logo-vector.jpg"))
items.add(ShopItem(shopImageURL = "https://logoeps.com/wp-content/uploads/2011/08/esprit-logo.jpg"))
items.add(ShopItem(shopImageURL = "https://i.pinimg.com/736x/b5/2c/51/b52c51f556c88216c1795f4ca433fd01--product-logo-fashion-logos.jpg"))
items.add(ShopItem(shopImageURL = "https://www.logodesignteam.com/blog/wp-content/uploads/2017/06/Ralph-Lauren-Logo.png"))
items.add(ShopItem(shopImageURL = "https://i.pinimg.com/736x/95/6f/1e/956f1edc1ec624cc97301e72c6d5b90e--fashion-logos-fashion-typography.jpg"))
items.add(ShopItem(shopImageURL = "https://www.logodesignteam.com/blog/wp-content/uploads/2017/06/Chanel_Logo.png"))
items.add(ShopItem(shopImageURL = "https://www.logodesignteam.com/blog/wp-content/uploads/2017/06/Paul-Smith-Logo.png"))
items.add(ShopItem(shopImageURL = "https://www.logodesignteam.com/blog/wp-content/uploads/2017/06/Louis-Vuitton-Logo.png"))
items.add(ShopItem(shopImageURL = "https://i.pinimg.com/originals/53/29/06/53290622f51318026dd537a2a9e7da94.jpg"))
items.add(ShopItem(shopImageURL = "https://prowly-uploads.s3.amazonaws.com/uploads/PressRoom/563/cover_photo/large_slack-imgs.com.png"))
items.add(ShopItem(shopImageURL = "https://www.logodesignteam.com/blog/wp-content/uploads/2017/07/Intel-Logo.png"))
return items
}
inner class LoveBrandAdapter : RecyclerView.Adapter<LoveBrandAdapter.ViewHolder>() {
private var items = mutableListOf<ShopItem>()
fun setListItem(newItem: MutableList<ShopItem>) {
items = newItem
}
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
val item = items[position]
if (holder != null) {
// Setup product image
Glide.with(context)
.load(item.shopImageURL)
.into(holder.imgBrand)
.onLoadFailed(ContextCompat.getDrawable(context, android.R.drawable.ic_dialog_alert))
}
}
override fun getItemCount() = items.size
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent?.context)
val view = inflater.inflate(R.layout.listitem_home_highlight_brand, parent, false)
return ViewHolder(view)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imgBrand: ImageView = itemView.findViewById(R.id.imgBrand)
}
}
}
XML
<merge 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="wrap_content"
tools:background="#262626"
tools:parentTag="android.widget.LinearLayout"
tools:orientation="vertical"
tools:context=".ui.home.HomeActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Brand We Love"
android:textAllCaps="true"
android:textColor="@color/colorAccent"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="@+id/guideline"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignBaseline="@id/tvTitle"
android:layout_gravity="bottom"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_toEndOf="@+id/tvTitle"
android:layout_toRightOf="@+id/tvTitle" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@+id/guideline"
android:layout_gravity="bottom"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_toEndOf="@+id/tvTitle"
android:layout_toRightOf="@+id/tvTitle"
android:background="@color/colorAccent" />
</RelativeLayout>
<com.abfabmarket.mobile.abfab.ui.view.CustomRecycleView
android:id="@+id/rcvLoveBrand"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginBottom="32dp"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="@layout/listitem_home_highlight_brand" />
</merge>