티스토리 뷰

SWDesk/App

Kotlin + RecyclerView 테스트 예제

bizmaker 2019. 12. 17. 18:27

 

<MainActivity.kt>

//Reference : https://m.blog.naver.com/PostView.nhn?blogId=cosmosjs&logNo=221208701289&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F
//Reference : https://cocomo.tistory.com/412
// Reference : https://m.blog.naver.com/cosmosjs/221205326784
// Reference : https://www.raywenderlich.com/1560485-android-recyclerview-tutorial-with-kotlin

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false);

        //fetchJson()
        ShowData()
    }

    fun ShowData(){
        var myList = ArrayList<CommandInfo>()
        var Index1 = 0
        while(Index1 < 5){
            val CI1 = CommandInfo("CN"+(Index1+1), Index1)
            myList.add(CI1)
            Index1++
        }
        Toast.makeText(this, "Clicked: ${myList.size}", Toast.LENGTH_SHORT).show()
        recyclerView.adapter = RecyclerAdapter0(myList)
    }
}

// Reference : https://codechacha.com/ko/android-recyclerview/
class RecyclerAdapter0(private val myList: ArrayList<CommandInfo>) :
    RecyclerView.Adapter<RecyclerAdapter0.ViewHolder>() {

    override fun getItemCount():Int{
        return myList.count()
    }

    override fun onBindViewHolder(holder: RecyclerAdapter0.ViewHolder, position: Int) {
        val item = myList[position]
        val listener = View.OnClickListener {it ->
            Toast.makeText(it.context, "Clicked: ${item.Commandname}", Toast.LENGTH_SHORT).show()
        }
//        holder.apply {
//            bind(listener, item)
 //           itemView.tag = item
//        }
        holder.bind(listener, item)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
            RecyclerAdapter0.ViewHolder {
        val inflatedView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_raw, parent, false)
        return RecyclerAdapter0.ViewHolder(inflatedView)
    }

    class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {

        private var view: View = v

        fun bind(listener: View.OnClickListener, item: CommandInfo) {
            view.bt_commandname.text = item.Commandname
            view.tv_commandno.text = item.CommandNo.toString()
            view.setOnClickListener(listener)
        }
    }
}

data class JsonObj(val result: List<CommandInfo>)
data class CommandInfo(val Commandname:String, val CommandNo:Int){

}

 

<build.gradddle(Module: app)>

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.testapp191208_1"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    implementation 'androidx.recyclerview:recyclerview:1.0.0'
    implementation 'com.squareup.okhttp3:okhttp:3.9.0'
    implementation 'com.google.code.gson:gson:2.8.2'
}

<activity_main.xml>

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
    <TextView
        android:id="@+id/introgreeting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text = "Hello~ Bilient"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <androidx.recyclerview.widget.RecyclerView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="20dp"
        android:scrollbars="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/introgreeting"
        app:layout_constraintTop_toBottomOf="@id/introgreeting" />
    </androidx.constraintlayout.widget.ConstraintLayout>


<item_raw.xml>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/bt_commandname"
        android:text = "Command-name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tv_commandno"
        android:text = "No. of Command"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

 

반응형

'SWDesk > App' 카테고리의 다른 글

BPM 사용자용 안드로이드 앱 Ver.1.1(한글)  (0) 2019.12.22
Kotlin + RecyclerView + JSON 연동 테스트  (0) 2019.12.21
[BPM] BPM User Tool, Ver.1.0  (0) 2019.12.14
Kotlin + Dialog  (0) 2019.12.11
Dynamic Table Creation Test02  (0) 2019.12.11