8000 GitHub - nb312/NBKotlinUI: The UI util for kotlin in android .
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

nb312/NBKotlinUI

Repository files navigation

NBKotlinUI

ui
This util is for kotlin in android .

Start.

1. Add an URL of the repository

In the build.gradle of the project root.Add this:

allprojects {
    repositories {
        //others 
        maven {
            url "https://jitpack.io"
        }
       
    }
}

2. dependencies

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'
}

3. init

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.

RecyclerView Example

There is a sample way to use the RecyclerView ,expicially the adapter.

1. Define a recyclerview in xml

  <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" />

2. Define your data item

You must implement the interface NBaseDataItemInter,it do nothing,just may expand in the future.

class DemoItem(var content: String = "") : NBaseDataItemInter

3. define your item.xml

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 .

4. define your adapter

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.

5. use the recyclerview.verticalLine

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)
    }

About

The UI util for kotlin in android .

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published
0