完整实现:子工程定义接口 + 主工程注册实现

37 阅读1分钟

LayoutRegistry

// LayoutRegistry.kt (子工程)
object LayoutRegistry {
    /**
     * 创建瀑布流布局的函数变量
     * 参数:Context, tgId, PageManagerInterface, ChannelTopicMsg
     * 返回:FrameLayoutSubPageConstraintLayout
     */
    var createWaterfallLayout: ((Context, Long, FrameLayoutPageInterface, ChannelTopicMsg) -> FrameLayoutSubPageConstraintLayout)? =
        null

}

DefaultWaterfallLayout

// DefaultWaterfallLayout.kt    映射  主项目的  DiscoverTopicWaterfallLayout
class DefaultWaterfallLayout(context: Context) : FrameLayoutSubPageConstraintLayout(context) {

    override fun onInitPageManager(tgId: Long, pageManagerInterface: FrameLayoutPageInterface) {
    }

    override fun getTopBackgroundColor(): Int {
        return Color.TRANSPARENT
    }

    override fun setDayNightTheme() {
    }

    override fun setLanguageText() {
    }
}

子项目实现

val discoverTopicWaterfallLayout = LayoutRegistry.createWaterfallLayout?.invoke(
    context, tgId, this@AiTopicLayout.pageManagerInterface, channelTopicMsg
) ?: DefaultWaterfallLayout(context)

主工程 创建

// 注册自定义布局创建方法  用于主题3  瀑布流的
LayoutRegistry.createWaterfallLayout = { ctx, id, manager, msg ->
    DiscoverTopicWaterfallLayout(ctx).apply {
        setChannelTopicMsg(msg)
        initPageManager(id, manager)
        // 可以在这里添加其他初始化代码
    }
}