This util is for kotlin in android .
In the build.gradle of the project root.Add this:
allprojects {
repositories {
//others
maven {
url "https://jitpack.io"
}
}
}
In the build.gradle
of the module.
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android{
//others
dataBinding {
enabled = true
}
kapt {
generateStubs = true
}
}
dependencies {
//others
implementation 'com.github.nb312:NBKotlinUI:0.0.8'
}
If you want to use the code of this library, you must initialize the param in the onCreate of your Application as below:
KotlinUi.context = this
if you follow these steps above,then you can use the library as well.
There is a sample way to use the RecyclerView ,expicially the adapter.
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_demo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/nb_white" />
You must implement the interface NBaseDataItemInter
,it do nothing,just may expand in the future.
class DemoItem(var content: String = "") : NBaseDataItemInter
I define a xml named item_recycler_demo.xml
,you must make sure the root item is layout
, this purpose is to use the DataBinding.If you don't know how to use ,just look at the link DataBinding .
The adpater is very easy to use and is the most important util in this library.code like this:
class DemoAdapter(context: Context) : NBaseAdapter<DemoItem, ItemRecyclerDemoBinding>(context) {
override var layoutId: Int = R.layout.item_recycler_demo
override fun ItemRecyclerDemoBinding.onBindView(dataItem: DemoItem) {
tvContent.text = dataItem.content
}
}
you just need to put the data_item and the layout here. then write the logical in the onBindView
function.You may surprise what is the view ,they are all in the 'ItemRecyclerDemoBinding' with their id style,the ItemRecyclerDemoBinding
is generated by android studio ,the id tv_content
to the tvContent
,In normally,you can use as itemRecyclerDemoBinding.tvContent
,but because we define an extension function with ItemRecyclerDemoBinding
,you can use tvContent directly.
If you want to set a distance between two items,you can just use the recyclerview.verticalLine(distance)
,the distance's unit is 'px',the using of a adapter is bellow.
override fun initRecycler() {
recycler_demo.verticalLine(50)
demoAdapter = DemoAdapter(this)
demoAdapter.clickListener = {
Toast.makeText(this, "you select ${it.content}", Toast.LENGTH_SHORT).show()
}
recycler_demo.adapter = demoAdapter
initRecyclerData()
recycler_demo.scroll2Top()
}
private fun initRecyclerData() {
var contents = mutableListOf("China", "US", "Apple", "Table")
var contentItems = mutableListOf<DemoItem>()
for (index in 1..20) {
var demoItem = DemoItem("${contents[index.rem(4)]} :$index")
contentItems.add(demoItem)
}
demoAdapter.changeAppendPart(contentItems)
}