Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
fix(xml-inflater): use custom TextureView when inflating XML layout (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky committed Aug 5, 2023
1 parent c5d3852 commit 8414f98
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package com.itsaky.androidide.inflater

import android.content.Context
import android.view.View
import com.android.SdkConstants
import com.itsaky.androidide.inflater.internal.AttributeImpl
Expand Down Expand Up @@ -222,4 +223,15 @@ abstract class IViewAdapter<T : View> : AbstractParser() {
protected open fun postCreateAttrHandlers(
handlers: MutableMap<String, AttributeHandlerScope<T>.() -> Unit>) {
}

/**
* A hook that can be used by subclasses to create view instances for the given view [name].
*
* @param name The fully qualified class name of the view to create.
* @param context The Android context.
* @return The view instance, or `null`.
*/
open fun onCreateView(name: String, context: Context): View? {
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package com.itsaky.androidide.inflater.internal.adapters

import android.content.Context
import android.view.TextureView
import android.view.View
import com.itsaky.androidide.annotations.uidesigner.IncludeInDesigner
import com.itsaky.androidide.annotations.uidesigner.IncludeInDesigner.Group.WIDGETS
import com.itsaky.androidide.inflater.AttributeHandlerScope
Expand Down Expand Up @@ -49,4 +51,11 @@ open class TextureViewAdapter<T : TextureView> : ViewAdapter<T>() {
drawable.ic_widget_textureview)
)
}

override fun onCreateView(name: String, context: Context): View? {
if (name == TextureView::class.java.name) {
return DesignerTextureView(context)
}
return super.onCreateView(name, context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,26 @@ import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import com.itsaky.androidide.inflater.InflateException
import com.itsaky.androidide.inflater.internal.ViewAdapterIndexImpl
import com.itsaky.androidide.utils.ILogger
import java.lang.reflect.Method

/** @author Akash Yadav */
object ViewFactory {

private val log = ILogger.newInstance("ViewFactory")

fun createViewInstance(name: String, context: Context): View {
val adapter = ViewAdapterIndexImpl.INSTANCE.getViewAdapter(name)
return try {
if (adapter != null) {
// Check if adapter can create the view instance
val view = adapter.onCreateView(name, context)
if (view != null) {
return view
}
}

val klass = javaClass.classLoader!!.loadClass(name)
val constructor = klass.getConstructor(Context::class.java)
constructor.newInstance(context) as View
Expand All @@ -39,8 +50,8 @@ object ViewFactory {
throw RuntimeException(err)
}
}
fun generateLayoutParams(parent: ViewGroup) : LayoutParams {

fun generateLayoutParams(parent: ViewGroup): LayoutParams {
return try {
var clazz: Class<in ViewGroup> = parent.javaClass
var method: Method?
Expand All @@ -51,7 +62,7 @@ object ViewFactory {
} catch (e: Throwable) {
/* ignored */
}

clazz = clazz.superclass
}
if (method != null) {
Expand Down

0 comments on commit 8414f98

Please sign in to comment.