Click>>> Click to see source code
==========================
Call method by reflect
private lateinit var activity: Any
activity.runInActivity()
private fun Any.runInActivity() {// In the code below , [this] is Activity Object
/***
* setContentView(R.layout.activity_main)
* ***/
calls("setContentView")(R.layout.activity_main)
/***
* val npRecyclerView = findViewById<ViewGroup>(R.id.npRecyclerView)
* ***/
val npRecyclerView = call<ViewGroup>("findViewById")(R.id.npRecyclerView)
/***
* val npRefreshLayout = findViewById<ViewGroup>(R.id.npRefreshLayout)
* ***/
val npRefreshLayout = call<ViewGroup>("findViewById")(R.id.npRefreshLayout)
... ...
}
==========================
Create Instance by reflect
/***************************** Import to reflect *****************************/
/** The string below can be used just same as declared class in the code **/
private const val MyLoadMoreRecyclerAdapter = "com.runningmessage.kotlinx.demo.MyLoadMoreRecyclerAdapter"
private const val StaggeredGridLayoutManager = "android.support.v7.widget.StaggeredGridLayoutManager"
private fun Any.runInActivity() {// In the code below , [this] is Activity Object
... ..
// set the load more recycler adapter
val applicationContext = call<Context>("getApplicationContext")()
/***
* the declared string [MyLoadMoreRecyclerAdapter] can be used same as class which is imported
*
* val adapter = com.runningmessage.kotlinx.demo.MyLoadMoreRecyclerAdapter(applicationContext)
* ***/
val adapter: Any? = MyLoadMoreRecyclerAdapter(applicationContext)
/***
* the declared string [StaggeredGridLayoutManager] can be used same as class which is imported
*
* npRecyclerView.setLayoutManager(StaggeredGridLayoutManager(2, 1))
* ***/
npRecyclerView.calls("setLayoutManager")(StaggeredGridLayoutManager(2, 1))
... ...
}
==========================
Call property by reflect
private fun Any.runInActivity() {// In the code below , [this] is Activity Object
... ..
/***
* adapter.isAutoLoadMore = false
***/
adapter.propertys("isAutoLoadMore").value = false
/***
* val count = adapter.dataCount
***/
val count = adapter.property<Int>("dataCount").value ?: 0
... ...
}
==========================
Create Anonymous inner class instance
/***************************** Import to reflect *****************************/
/** The string below can be used just same as declared class in the code **/
private const val OnLoadMoreListener = "com.runningmessage.kotlinx.widget.LoadMoreRecyclerAdapter${'$'}OnLoadMoreListener"
private const val OnRefreshListener = "com.runningmessage.kotlinx.widget.AbsSwipeRefreshLayout${'$'}OnRefreshListener"
private fun Any.runInActivity() {// In the code below , [this] is Activity Object
... ..
/***
* adapter.onLoadMoreListener = object: OnLoadMoreListener{
*
* override fun onLoadMore(){
* // do something
* }
*
* }
* ***/
adapter.propertys("onLoadMoreListener").value = OnLoadMoreListener.createInners {
override("onLoadMore") {
// do something
}
}
/***
* npRefreshLayout.setOnRefreshListener = object: OnRefreshListener{
*
* override fun onRefresh(autoNotify: Boolean){
* // do something
* }
*
* }
* ***/
npRefreshLayout.calls("setOnRefreshListener")(OnRefreshListener.createInners {
override("onRefresh") {
val autoNotify: Boolean = it?.get(0) as? Boolean ?: false
// do something
}
})
}