Skip to content

Commit

Permalink
creat viewholder and adapter for group
Browse files Browse the repository at this point in the history
  • Loading branch information
kulloveth committed Apr 26, 2020
1 parent ce2e46b commit 53babf1
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 8 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ dependencies {
// Material design
implementation "com.google.android.material:material:$rootProject.materialVersion"

implementation 'com.thoughtbot:expandablerecyclerview:1.3'
implementation 'com.thoughtbot:expandablecheckrecyclerview:1.4'

// Testing
testImplementation 'junit:junit:4.12'
androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.developer.kulloveth.expandablelistsamplewithroom.data

import android.view.View
import android.widget.TextView
import com.developer.kulloveth.expandablelistsamplewithroom.R
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Continents
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Countrys
import com.thoughtbot.expandablerecyclerview.viewholders.ChildViewHolder
import com.thoughtbot.expandablerecyclerview.viewholders.GroupViewHolder


class CountryViewHolder(itemView: View) : ChildViewHolder(itemView) {
val countryName = itemView.findViewById<TextView>(R.id.countryName)

fun bind(countrys: Countrys) {
countryName.text = countrys.countryName
}
}

class ContinentViewHolder(itemView: View) : GroupViewHolder(itemView) {
val continentName = itemView.findViewById<TextView>(R.id.continent)

fun bind(continent: Continents) {
continentName.text = continent.ContinentName
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.thoughtbot.expandablerecyclerview.models.ExpandableGroup
import kotlinx.android.parcel.Parcelize

@Parcelize

@Entity(tableName = "continent-table")
data class Continents(@PrimaryKey @ColumnInfo(name = "continent")val ContinentName: String,
@ColumnInfo(name = "countrys")val countrys: List<Countrys>) : Parcelable
data class Continents(
@PrimaryKey @ColumnInfo(name = "continent") val ContinentName: String,
@ColumnInfo(name = "countrys__layout") val countrys: List<Countrys>
) : ExpandableGroup<Countrys>(ContinentName, countrys)
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.developer.kulloveth.expandablelistsamplewithroom.data.ui

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ExpandableListAdapter
import com.developer.kulloveth.expandablelistsamplewithroom.R
import com.developer.kulloveth.expandablelistsamplewithroom.data.ContinentViewHolder
import com.developer.kulloveth.expandablelistsamplewithroom.data.CountryViewHolder
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Continents
import com.developer.kulloveth.expandablelistsamplewithroom.data.model.Countrys
import com.thoughtbot.expandablerecyclerview.ExpandableRecyclerViewAdapter
import com.thoughtbot.expandablerecyclerview.models.ExpandableGroup

class ContinentAdapter(groups: List<ExpandableGroup<*>>?) :
ExpandableRecyclerViewAdapter<ContinentViewHolder, CountryViewHolder>(
groups
) {


override fun onCreateGroupViewHolder(parent: ViewGroup?, viewType: Int): ContinentViewHolder {
val itemView =
LayoutInflater.from(parent?.context).inflate(R.layout.continent_layout, parent, false)
return ContinentViewHolder(itemView)
}

override fun onCreateChildViewHolder(parent: ViewGroup?, viewType: Int): CountryViewHolder {
val itemView =
LayoutInflater.from(parent?.context).inflate(R.layout.countrys_layout, parent, false)
return CountryViewHolder(itemView)
}

override fun onBindChildViewHolder(
holder: CountryViewHolder?,
flatPosition: Int,
group: ExpandableGroup<*>?,
childIndex: Int
) {
val countrys: Countrys = group?.items?.get(childIndex) as Countrys
holder?.bind(countrys)
}

override fun onBindGroupViewHolder(
holder: ContinentViewHolder?,
flatPosition: Int,
group: ExpandableGroup<*>?
) {
val continents: Continents = group as Continents
holder?.bind(continents)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package com.developer.kulloveth.expandablelistsamplewithroom.data.ui
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.developer.kulloveth.expandablelistsamplewithroom.R
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

}
}
8 changes: 4 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
android:layout_height="match_parent"
tools:context=".data.ui.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rvConinent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:orientation="vertical">

<TextView
android:id="@+id/country"
android:id="@+id/countryName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

Expand Down

0 comments on commit 53babf1

Please sign in to comment.