Created
June 15, 2020 04:11
-
-
Save ekoo/2fb4842f94e190199d15a9593673c901 to your computer and use it in GitHub Desktop.
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
class StateAdapter(private val retry: () -> Unit) : LoadStateAdapter<StateAdapter.LoadStateViewHolder>() { | |
override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) { | |
holder.bind(loadState) | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadStateViewHolder { | |
return LoadStateViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_load_state, parent, false), retry) | |
} | |
inner class LoadStateViewHolder(view: View, retry: () -> Unit): RecyclerView.ViewHolder(view){ | |
init { | |
itemView.state_retry_button.setOnClickListener { | |
retry.invoke() | |
} | |
} | |
fun bind(loadState: LoadState){ | |
itemView.apply { | |
state_progressBar.visibleWhen(loadState is LoadState.Loading) | |
state_error_msg_textView.visibleWhen(loadState is LoadState.Error) | |
state_retry_button.visibleWhen(loadState is LoadState.Error) | |
} | |
if (loadState is LoadState.Error){ | |
itemView.state_error_msg_textView.text = loadState.error.localizedMessage | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment