airbnb / epoxy

Epoxy is an Android library for building complex screens in a RecyclerView
https://goo.gl/eIK82p
Apache License 2.0
8.52k stars 726 forks source link

[Question]Custom ItemDecoration for different model? #377

Closed JasonHezz closed 6 years ago

JasonHezz commented 6 years ago

After searching issues and wiki,I can't find a way to work out.In normal way, I use recyclerview.getViewType to define, but how can I implent it in Epoxy?

elihart commented 6 years ago

you can get the model at the position and use the model class to determine this.

If you give me more details I can try to be more specific.

JasonHezz commented 6 years ago

Thanks it works.Here is a snippet for others who want to custom decoration

 override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView,
      state: RecyclerView.State?) {
    val position = parent.getChildAdapterPosition(view)
    val adapter = parent.adapter
    if (adapter is EpoxyControllerAdapter) {
      val model = adapter.getModelAtPosition(position)
      //if you are using Epoxy's annotations
      when (model::class.java.superclass) {
        PhotoModel::class.java -> outRect.set(5, 5, 5, 5)
        CollectionModel::class.java -> outRect.set(10, 10, 10, 10)
        else -> outRect.setEmpty()
      }
      //if you are not using Epoxy's annotations
      /*when (model::class) {
        PhotoModel::class -> outRect.set(5, 5, 5, 5)
        CollectionModel::class-> outRect.set(10, 10, 10, 10)
        else -> outRect.setEmpty()
      }*/
    } else {
      outRect.setEmpty()
    }
  }
elihart commented 6 years ago

Looks good, glad you got it.

you should be able to do PhotoModel_::class as well instead of going to the super class

matheusfinatti commented 5 years ago

Using this approach I only see the divider for a brief period during the animation, then it's gone. Any idea of what might be happening @elihart ? Or any other way to add dividers to some models, but not all? Thanks