mirror of
https://gitee.com/zhangsken/AndroidIDE.git
synced 2026-10-16 07:49:44 +08:00
fix(xml-inflater): use custom TextureView when inflating XML layout (#1129)
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
+9
@@ -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
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+14
-3
@@ -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
|
||||
@@ -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?
|
||||
@@ -51,7 +62,7 @@ object ViewFactory {
|
||||
} catch (e: Throwable) {
|
||||
/* ignored */
|
||||
}
|
||||
|
||||
|
||||
clazz = clazz.superclass
|
||||
}
|
||||
if (method != null) {
|
||||
|
||||
Reference in New Issue
Block a user