Last active
January 14, 2019 11:47
-
-
Save rezaiyan/1ce66ed4097664521d72706ad6a3fc8a to your computer and use it in GitHub Desktop.
This is an inner Recyclerview sample with dataBindign
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal inner class MainAdapter(context: Context, var mainContainer: List<MainModel>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() { | |
private val layoutInflater: LayoutInflater = LayoutInflater.from(context) | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { | |
return when (viewType) { | |
//... | |
MainContainer.Type.ACTORS -> { | |
ActorsViewHolder(ItemActorsBinding.inflate(layoutInflater, parent, false)) | |
} | |
else -> { | |
//... | |
} | |
} | |
} | |
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { | |
when (holder) { | |
//... | |
is ActorsViewHolder -> holder.bind(mainContainer[position] as Actors) | |
//... | |
} | |
} | |
override fun getItemViewType(position: Int): Int { | |
return mainContainer[position].viewtype | |
} | |
override fun getItemCount(): Int { | |
return mainContainer.size | |
} | |
} | |
////// | |
class ActorsViewHolder(private val binding: ItemActorsBinding) : RecyclerView.ViewHolder(binding.root) { | |
fun bind(item: Actors) { | |
binding.run { | |
obj = item | |
executePendingBindings() | |
} | |
} | |
} | |
////// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@BindingAdapter("setActorsData") | |
fun setAvatars(recyclerView: RecyclerView, list: ArrayList<Actor>) { | |
val context = recyclerView.context | |
val radius = context.resources.getDimension(R.dimen.list_radius) | |
val peek = context.resources.getDimension(R.dimen.list_peek) | |
val layoutManager = TurnLayoutManager(context, | |
TurnLayoutManager.Gravity.END, | |
TurnLayoutManager.Orientation.HORIZONTAL, | |
radius.toInt(), | |
peek.toInt(), | |
false) | |
recyclerView.layoutManager = layoutManager | |
val adapter = ActorAdapter(context, list) | |
recyclerView.adapter = adapter | |
adapter.notifyDataSetChanged() | |
} | |
//// | |
class ActorAdapter(context: Context, var actors: ArrayList<Actor>) : RecyclerView.Adapter<ActorViewHolder>() { | |
private val layoutInflater: LayoutInflater = LayoutInflater.from(context) | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ActorViewHolder = ActorViewHolder(ItemActorBinding.inflate(layoutInflater, parent, false)) | |
override fun onBindViewHolder(holder: ActorViewHolder, position: Int) = holder.bind(actors[position]) | |
override fun getItemCount() = actors.size | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<androidx.constraintlayout.widget.ConstraintLayout | |
android:id="@+id/avatarActor" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:paddingTop="8dp" | |
tools:background="@color/colorAccent"> | |
<androidx.recyclerview.widget.RecyclerView | |
setActorsData="@{obj.actorList}" // <-- set data to recyclerview | |
android:layout_width="match_parent" | |
android:layout_height="180dp" | |
android:foregroundGravity="center_vertical" | |
android:orientation="horizontal" | |
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" | |
app:layout_constraintTop_toBottomOf="@+id/actorsTitle" | |
tools:listitem="@layout/item_actor" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment