Consider this snipped:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.test, container, false)
class MyViewHolder(val view: View): RecyclerView.ViewHolder(view) {
init {
view.setOnClickListener {
Log.d("hey", "there")
}
}
}
view.findViewById<RecyclerView>(R.id.files).adapter = object: RecyclerView.Adapter<MyViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val item = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
return MyViewHolder(item)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.view.findViewById<TextView>(R.id.text).text = files[position].name
}
override fun getItemCount() = files.size
}
return view
}
onCreateView creates a local variable view. The nested class MyViewHolder also declares a variable called view. What was unexpected is that the variable view accessed inside the init block of MyViewHolder (where the OnClickListener is set) is not the one declared in MyViewHolder, but the outer one. Why?
I would expect the innermost variable declaration would be used.
The class is not declared as an inner class. Outside variables should not be accesible.
What am I missing?
This is not the case of a nested class, but of a function that returns a class.
If you try to define your class as inner, you'll actually get an error message:
Modifier 'inner' is not applicable to 'local class'
I'll simplify this example a bit, so the Android part won't interfere:
// This is what you're doing
fun a(): Any {
val a = "a"
class B(val a: String = "b") {
init {
println(a)
}
}
return B()
}
// This is what you think you're doing
class A(val a: String = "a") {
class B(val a: String = "b") {
init {
println(a)
}
}
}
fun main() {
// This refers to function called a
val func = a()
// This refers to a nested class called B
val nestedClass = A.B()
}
If you actually want to refer to the local class properties, use this
Related
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'm having trouble binding my ViewHolder, and I've got two warning that I believe are related. I am trying to use Hilt to create a clickable ViewHolder, so in my SessionAdapter I am using an inner class to bind my SessionViewHolder to my RecyclerView.
First, I am struggling to understand what to return for the inner class SessionViewHolder fun bind(session: Session) { ...}. Android Studio is telling me function "bind" is never used, but I thought I used it in my onBindViewHolder?
Secondly, in my override onBindViewHolder I don't understand how I should use val session?
#AndroidEntryPoint
class SessionFragment : Fragment() {
var adapter: SessionAdapter = SessionAdapter()
private val sessionAdapter = SessionListAdapter(this::onSessionClicked)
private fun onSessionClicked(session: Session): Session {
return(session)
}
class SessionAdapter {
fun setOnClickListener() {
return(addSessionToItinerary())
}
private fun addSessionToItinerary() {
return addSessionToItinerary()
}
}
class SessionListAdapter(
private val onSessionCLicked: (Session) -> Unit,
) : ListAdapter<Session, SessionListAdapter.SessionViewHolder>(SessionItemCallback) {
inner class SessionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind() {
val textView = itemView.findViewById<TextView>(0)
fun bind(session: Session) {
textView.text = session.title
itemView.setOnClickListener {
onSessionCLicked(session)
return#setOnClickListener
}
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SessionViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val itemView = layoutInflater.inflate(R.layout.fragment_session_list, parent, false)
return SessionViewHolder(itemView)
}
override fun onBindViewHolder(holder: SessionViewHolder, position: Int) {
val session = getItem(position)
holder.bind()
}
}
Thank you in advance for you help. I have gotten myself confused with the recurrence of bind and session throughout my adapter.
See here in this code, you have defined two bind functions, but one is nested inside the other so it is unusable:
fun bind() {
val textView = itemView.findViewById<TextView>(0)
fun bind(session: Session) {
textView.text = session.title
itemView.setOnClickListener {
onSessionCLicked(session)
return#setOnClickListener
}
}
}
The outer bind() function is the one you are calling, and it doesn't make sense to bind nothing. This function gets a reference to a TextView, and it creates a function that is never used.
Another problem is that you are passing 0 to findViewById. There is never going to be a view with an ID of 0. You need to pass R.id.whateverYourTextViewIsNamedInYourXml.
Side note, return#onClickListener is unnecessary. If a function doesn't return anything, putting a return statement on the last line doesn't do anything.
To make it work, you should replace the above code with something like this, but replace the name of the text view with whatever ID you assigned it in your XML:
fun bind(session: Session) {
val textView = itemView.findViewById<TextView>(R.id.myTextView)
textView.text = session.title
itemView.setOnClickListener {
onSessionCLicked(session)
}
}
and then pass the session to this function when you call it.
Side note, your SessionAdapter class doesn't make any sense at all, but you're not using it for anything anyway. I would delete that.
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)
}
}
I have read the following syntax. I have no idea why scope resolution operator is used in it.
class XyzFragment : Fragment() {
lateinit var adapter: ChatAdapter
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
if (!::adapter.isInitialized) { <-- This one
adapter = ChatAdapter(this, arrayListOf())
}
}
}
I want to know what is :: in if (!::adapter.isInitialized) { statement.
:: is a short form for this:: in Kotlin.
:: is a operator to creates a member reference or a class reference. For example,
class Test {
fun foo() {
}
fun foo2(value: Int) {
}
fun bar() {
val fooFunction = ::foo
fooFunction.invoke() // equals to this.foo()
val foo2Function = ::foo2
foo2Function.invoke(1) // equals to this.foo2(1)
val fooFunction2 = Test::foo
val testObject = Test()
fooFunction2.invoke(this) // equals to this.foo()
fooFunction2.invoke(testObject) // equals to testObject.foo()
}
}
This is mainly used in reflection and passing function.
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'
}