How to create custom Adapter with for 3 recycleViews - kotlin

Hello All,
i want to ask if i can add to my 3 recycle Views each recycle view hase interface to optimise my code i tried to add only 1 adapter for the 3 recycle View, as you can see my code below but i find my self stuck with this adapter, any 1 have idea how add custom adapter to adapt 3 recycle View? Thanx.
class CustomAdapter(private val contexte: Context) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val context: Context = contexte
inner class FolderViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
inner class PagesViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
inner class CorpusViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
if (viewType == VIEW_TYPE_CORPUS)
return CorpusViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.corpus_item_layout, parent, false)
)
if (viewType == VIEW_TYPE_FOLDER)
return FolderViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.folder_item_layout, parent, false)
)
return PagesViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.page_item_layout, parent, false
)
)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
TODO("Not yet implemented")
}
override fun getItemCount(): Int {
return 20
}
companion object {
internal val VIEW_TYPE_CORPUS = 1
internal val VIEW_TYPE_FOLDER = 2
internal val VIEW_TYPE_PAGES = 2
}

Instead of doing this, I suggest you to use base class that takes layout id and initialize your common adapter with that.
open class AdapterItem(val layoutId: Int)
data class Corpus(val id: Int): AdapterItem(id)
then init your adapter like
CustomAdapter<AdapterItem>(...)
in your adapter, override getView
#Override
fun getView(position: Int, convertView: View, parent: ViewGroup): View {
val item = list[position]
return if(converView != null){
convertView
} else {
LayoutInflater.from(parent.context).inflate(item.layoutId, parent, false)
}
}

Related

Use kotlin enum to avoid else statement in when expressions

I would like to use the enum class to create exhaustive lists for when statements.
I have created my enum class but in onCreateViewHolder() I am still getting error message "A 'return' expression required in a function with a block body ('{...}')". The error goes away when i add else statement. What's strange is that onBindViewHolder works fine even though it implements the same enum elements.
How can I implement the enum here to avoid using else block in onCreateViewHolder()?
enum class ViewHolderType(val ID: Int) {
FOOTER(0),
ITEM(1)
}
override fun getItemViewType(position: Int): Int {
return if (position == currentList.size) {
ViewHolderType.FOOTER.ID
} else {
ViewHolderType.ITEM.ID
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): MultiViewViewHolder {
when (viewType) {
ViewHolderType.FOOTER.ID -> {
val view = LayoutInflater
.from(parent.context)
.inflate(R.layout.recycler_view_fragment_plus_button_new, parent, false)
return ViewHolder2(view)
}
ViewHolderType.ITEM.ID -> {
val view = LayoutInflater
.from(parent.context)
.inflate(R.layout.recycler_view_fragment, parent, false)
return ViewHolder1(view)
}
}
}
override fun onBindViewHolder(holder: MultiViewViewHolder, position: Int) {
when (getItemViewType(position)) {
ViewHolderType.ITEM.ID -> {
val item = getItem(position)
holder.onBindViewHolderItem(position, item)
}
ViewHolderType.FOOTER.ID -> {
holder.onBindViewHolderFooter()
}
}
}
Do it like this so the subject of when is an enum rather than an Int:
enum class ViewHolderType {
FOOTER,
ITEM
}
override fun getItemViewType(position: Int): Int {
return when (position == currentList.size) {
true -> ViewHolderType.FOOTER
false -> ViewHolderType.ITEM
}.ordinal
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): MultiViewViewHolder {
return when (ViewHolderType.values()[viewType]) {
ViewHolderType.FOOTER -> {
val view = LayoutInflater
.from(parent.context)
.inflate(R.layout.recycler_view_fragment_plus_button_new, parent, false)
ViewHolder2(view)
}
ViewHolderType.ITEM -> {
val view = LayoutInflater
.from(parent.context)
.inflate(R.layout.recycler_view_fragment, parent, false)
ViewHolder1(view)
}
}
}
override fun onBindViewHolder(holder: MultiViewViewHolder, position: Int) {
when (ViewHolderType.values()[getItemViewType(position)]) {
ViewHolderType.ITEM -> {
val item = getItem(position)
holder.onBindViewHolderItem(position, item)
}
ViewHolderType.FOOTER -> {
holder.onBindViewHolderFooter()
}
}
}
And you don’t need the ID property in your enum class. Often it is recommended not to use the ordinals of enums like this because it prevents changes from being made to the number and order of enum elements without breaking code elsewhere. But this reason doesn’t matter here since it is only used internally to this class, so it’s perfectly fine.

I've tried to make a RecyclerView with an Adapter but I cannot see the layout?

I've tried to build a recyclerview with an adapter class and cardview layout however even though the code runs I cannot see the list with cardview (I just get a blank recyclerview)?
SpeakerAdapter.kt
class SpeakerAdapter (val speakers: ArrayList<String>): RecyclerView.Adapter<SpeakerAdapter.ViewHolder>() {
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val speakerName: TextView = itemView.findViewById(R.id.name)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.speakerName.text = speakers[position]
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val View = LayoutInflater.from(parent.context).inflate(R.layout.speaker_list, parent, false)
return ViewHolder(View)
}
override fun getItemCount() = speakers.size
}
Speaker.kt
class Speaker : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.activity_speaker)
val speakers : ArrayList<String> = ArrayList()
for (i in 1..100){
speakers.add("Speaker 1 #$i")
recyclerView_speaker.layoutManager = LinearLayoutManager(this)
recyclerView_speaker.adapter = SpeakerAdapter(speakers)
}
}}
It's a bit hard to debug without the xml. But here goes nothing:
I think it'd be worth a shot modifying your view holder class. The reference for the text view is being set when you init the view holder and the layout may not be inflated yet. Try finding the reference when you bind the data to the view to avoid this issue.
Try this:
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(name: String?) {
itemView. name.text = name
}
}
and then modify your adapter to call this method
class SpeakerAdapter (val speakers: ArrayList<String>): RecyclerView.Adapter<SpeakerAdapter.ViewHolder>() {
...
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(speakers[position])
}
}
PS.: it'd be a good idea to call your view holder something else. ViewHolder is a name that is already used by the android SDK. Ex.: SpeakerViewholder

RecyclerView onClick in kotlin

I'm new android learner, I'm trying to make a RecyclerView contains of List of (Stories Title, and Stories images).
When you click on an item in the RecyclerView it should open a new activity called ChildrenStoriesPreview contains of ScrollView which have ImageView to put the Story Image in it and TextView to put the Text of the Story in it.
the problem is that I don't know how to set ocItemClickListener to know which item is clicked and how the new activity will contain information depending on this item? Could you please help me?
here is my Main.kt
class MainChildrenStories : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_children_stories)
var childrenStoriesArraylist = ArrayList<ChildrenStoriesRecyclerView>()
childrenStoriesArraylist.add(ChildrenStoriesRecyclerView("Story1", R.drawable.pic1))
childrenStoriesArraylist.add(ChildrenStoriesRecyclerView("Story2", R.drawable.pic2))
childrenStoriesArraylist.add(ChildrenStoriesRecyclerView("Story3", R.drawable.pic3))
children_stories_recyclerview.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)
val childrenStoriesAdapter = ChildrenStoriesAdapter(childrenStoriesArraylist)
children_stories_recyclerview.adapter = childrenStoriesAdapter
childrenStoriesAdapter.setOnItemClickListener(object : ChildrenStoriesAdapter.ClickListener {
override fun onClick(pos: Int, aView: View) {
//The App Crash here
if (pos == 0){
my_text_view.text = "Story number 1"
my_imageview.setImageResource(R.drawable.pic1)
}else if (pos == 1){
my_text_view.text = "Story number 2"
my_imageview.setImageResource(R.drawable.pic2)
}
val intent = Intent(this#MainChildrenStories, ChildrenStoryPreview::class.java)
startActivity(intent)
}
})
}
}
MyRecyclerView Class
data class ChildrenStoriesRecyclerView(var mStoryName: String, var mStoryImage: Int)
My RecyclerView Adapter Class
class ChildrenStoriesAdapter(var myArrayList: ArrayList<ChildrenStoriesRecyclerView>) :
RecyclerView.Adapter<ChildrenStoriesAdapter.ViewHolder>() {
lateinit var mClickListener: ClickListener
fun setOnItemClickListener(aClickListener: ClickListener) {
mClickListener = aClickListener
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
val v = LayoutInflater.from(p0.context).inflate(R.layout.children_stories_list, p0, false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return myArrayList.size
}
override fun onBindViewHolder(p0: ViewHolder, p1: Int) {
var infList = myArrayList[p1]
p0.storyName.text = infList.mStoryName
p0.storyImage.setImageResource(infList.mStoryImage)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
override fun onClick(v: View) {
mClickListener.onClick(adapterPosition, v)
}
val storyName = itemView.findViewById(R.id.txtStoryName) as TextView
val storyImage = itemView.findViewById(R.id.imageViewChildrenStories) as ImageView
init {
itemView.setOnClickListener(this)
}
}
interface ClickListener {
fun onClick(pos: Int, aView: View)
}
}
My new Activity To show Details of the Story
class ChildrenStoryPreview : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_children_story_preview)
}
}
Pass the Event Listener to Adapter constructor also to viewholder to catch view holder (items) clicks.
class ChildrenStoriesAdapter(var myArrayList: ArrayList<ChildrenStoriesRecyclerView>
var clickListener:MyClickListener?) :
RecyclerView.Adapter<ChildrenStoriesAdapter.ViewHolder>() {
...
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
val v = LayoutInflater.from(p0.context).inflate(R.layout.children_stories_list, p0, false)
return ViewHolder(v, clickListener)
}
...
inner class ViewHolder(itemView: View, clickListener:MyClickListener?) :
RecyclerView.ViewHolder(itemView) {
itemView.setOnClickListener { clickListener?.myClickedFun(...) }
...
class ChildrenStoryPreview : AppCompatActivity(), MyClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_children_story_preview)
}
override fun myClickedFun(...) {
...
}
}
Later init adapter like
..
val childrenStoriesAdapter = ChildrenStoriesAdapter(childrenStoriesArraylist, this)

How can I convert this Firebase Recycler Adapter implementation into an Adapter.kt class?

I have written a Kotlin FirebaseRecyclerAdapter that works just fine as part of my MainActivity. However, I would like to have this code in a separate MainAdapter.kt file/class. How can I do this?
var query = FirebaseDatabase.getInstance()
.reference
.child("").child("categories")
.limitToLast(50)
val options = FirebaseRecyclerOptions.Builder<Category>()
.setQuery(query, Category::class.java)
.setLifecycleOwner(this)
.build()
val adapter = object : FirebaseRecyclerAdapter<Category, CategoryHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
return CategoryHolder(LayoutInflater.from(parent.context)
.inflate(R.layout.category_row, parent, false))
}
protected override fun onBindViewHolder(holder: CategoryHolder, position: Int, model: Category) {
holder.bind(model)
}
override fun onDataChanged() {
// Called each time there is a new data snapshot. You may want to use this method
// to hide a loading spinner or check for the "no documents" state and update your UI.
// ...
}
}
class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {
fun bind(category: Category) {
with(category) {
customView.textView_name?.text = category.name
customView.textView_description?.text = category.description
}
}
}
Given your code you could do something like this :
class MainAdapter(lifecycleOwner: LifecycleOwner) : FirebaseRecyclerAdapter<Category, CategoryHolder>(buildOptions(lifecycleOwner)) {
companion object {
private fun buildQuery() = FirebaseDatabase.getInstance()
.reference
.child("").child("categories")
.limitToLast(50)
private fun buildOptions(lifecycleOwner:LifecycleOwner) = FirebaseRecyclerOptions.Builder<Category>()
.setQuery(buildQuery(), Category::class.java)
.setLifecycleOwner(lifecycleOwner)
.build()
}
class CategoryHolder(val customView: View, var category: Category? = null) : RecyclerView.ViewHolder(customView) {
fun bind(category: Category) {
with(category) {
customView.textView_name?.text = category.name
customView.textView_description?.text = category.description
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryHolder {
return CategoryHolder(LayoutInflater.from(parent.context)
.inflate(R.layout.category_row, parent, false))
}
protected override fun onBindViewHolder(holder: CategoryHolder, position: Int, model: Category) {
holder.bind(model)
}
override fun onDataChanged() {
// Called each time there is a new data snapshot. You may want to use this method
// to hide a loading spinner or check for the "no documents" state and update your UI.
// ...
}
}
There are many other ways to handle this problem, this is just an encapsulated version of yours.

Kotlin - RecyclerView.ViewHolder subclass - unable to access an extra property

I have implemented RecyclerView.ViewHolder sub-class as below:
class PersonViewHolder(itemView: View, binding: ViewDataBinding) : RecyclerView.ViewHolder(itemView) { }
Now I am trying to access binding property declared in it like this within subclass of RecyclerView.Adapter:
override fun onBindViewHolder(holder: PersonViewHolder?, position: Int) {
val person = persons[position]
if (holder != null) {
holder.binding.setVariable(BR.person, person) // line with error
holder.binding.executePendingBindings() // line with error
}
}
But compiler is complaining - Unresolved reference: binding
Here is the complete implementation:
class PersonsAdapter(private var persons: Array<Person>) : RecyclerView.Adapter<PersonsAdapter.PersonViewHolder>() {
override fun onBindViewHolder(holder: PersonViewHolder?, position: Int) {
val person = persons[position]
if (holder != null) {
holder.binding.setVariable(BR.person, person)
holder.binding.executePendingBindings()
}
}
override fun getItemCount(): Int {
return persons.size
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): PersonViewHolder {
val itemView = LayoutInflater.from(parent!!.context).inflate(R.layout.list_item_person, parent, false)
return PersonViewHolder(itemView, DataBindingUtil.bind(itemView))
}
class PersonViewHolder(itemView: View, binding: ViewDataBinding) : RecyclerView.ViewHolder(itemView) { }
}
Any ideas if I am missing anything over here? Please suggest.
binding: ViewDataBinding - you're only defining a constructor parameter, it is never saved as a member of the class. Mark it using var or val to have it store the parameter and have it be accessible later on.
check build.gradle file (module level) in your project
in the top android extesion plugin is included or not?
plugin {
id 'kotlin-android-extensions'
}