Code Monkey home page Code Monkey logo

testcomponentdev3's Introduction

TestComponentDev3 使用 XRouter

TestComponentDev3 是组件化项目,XRouter 是支持 kotlin 的注解处理器的简单的路由框架

build.gradle 添加依赖
implementation 'com.github.dazeGitHub:XRouter:5.0.5'
annotationProcessor 'com.github.dazeGitHub.XRouter:annotation_compiler:5.0.5'
kapt 'com.github.dazeGitHub.XRouter:annotation_compiler:5.0.5'
使用路由
@Route(key = Constants.RouterPath.MAIN)
class MainActivity : BaseActivity() {
    fun jumpLoginActivity(view: View?) {
//        liveData.postValue("testData2_postValue");
//        startActivity(Intent(this, XRouter.getInstance().getActivity("login/login")))
        XRouter.getInstance().jumpActivity(this,  url = Constants.RouterPath.LOGIN_TEST_PATH,scheme = null)
    }
}

字段自动注入

@Route(key = Constants.RouterPath.LOGIN)
class LoginActivity : BaseActivity() {

    @Autowired("age")
    var age: Int? = null
    @Autowired(value = "username")
    var userNameStr: String? = null
    @Autowired(value = "user")
    var userObj: User? = null
    
    override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_login)
      XRouterKnife.bind(this, Constants.JSON_TRANS_IMP)

      "LoginActivity age = $age userNameStr = $userNameStr userObj = $userObj".let{
          TLog.e(TAG,it)
          findViewById<TextView>(R.id.tv_receive_msg).text = it
    }  
}
object Constants {
    val GSON = Gson()
    val JSON_TRANS_IMP = IJsonTransferImp()

    const val ROUTER_SCHEME = "dm://"

    object RouterPath {
        const val MAIN = "main/main"
        const val LOGIN = "login/login"
        const val MEMBER = "member/member"

        const val LOGIN_TEST_PATH = "$ROUTER_SCHEME$LOGIN?age=25&username=zhangsan&user={\"username\":\"lisi\", \"age\":30}"
        const val MEMBER_TEST_PATH = "$ROUTER_SCHEME$MEMBER"
    }
}
class IJsonTransferImp : IJsonTransfer {
    override fun transJson2Obj(json: String?, clazz: Class<*>): Any? {
        return when(clazz){
            User::class.java -> {
                Constants.GSON.fromJson<User>(json, clazz)
            }
            else -> {
                null
            }
        }
    }
}

testcomponentdev3's People

Contributors

dazegithub avatar greatdaze avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.