Last active
March 8, 2018 14:19
-
-
Save scottschmitz/42f5b8858ff6f3240d6d4276c15c9bbc to your computer and use it in GitHub Desktop.
Expandable List Fragment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View | |
import com.xwray.groupie.ExpandableGroup | |
import com.xwray.groupie.ExpandableItem | |
import com.xwray.groupie.Item | |
import com.xwray.groupie.ViewHolder | |
import kotlinx.android.extensions.LayoutContainer | |
import kotlinx.android.synthetic.main.list_item_brand.* | |
class BrandItem(private val brand: String): Item<BrandItem.BrandViewHolder>(), ExpandableItem { | |
private var expandableGroup: ExpandableGroup? = null | |
override fun getLayout(): Int { | |
return R.layout.list_item_brand | |
} | |
override fun createViewHolder(itemView: View): BrandViewHolder { | |
val holder = BrandViewHolder(itemView) | |
holder.expandCollapse.setOnClickListener { | |
expandableGroup?.onToggleExpanded() | |
} | |
return holder | |
} | |
override fun bind(holder: BrandViewHolder, position: Int) { | |
holder.brandName.text = brand | |
} | |
override fun setExpandableGroup(group: ExpandableGroup) { | |
expandableGroup = group | |
} | |
class BrandViewHolder(override val containerView: View): ViewHolder(containerView), LayoutContainer | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle | |
import android.support.v4.app.Fragment | |
import android.support.v7.widget.GridLayoutManager | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import com.xwray.groupie.* | |
import kotlinx.android.synthetic.main.fragment_main.* | |
class KotlinSyntheticFragment: Fragment() { | |
companion object { | |
val phones = mapOf( | |
"Apple" to listOf( | |
"iPhone", | |
"iPhone 3G", | |
"iPhone 3GS", | |
"iPhone 4", | |
"iPhone 4S", | |
"iPhone 5", | |
"iPhone 5c", | |
"iPhone 5s", | |
"iPhone 6", | |
"iPhone 6 Plus", | |
"iPhone 6s", | |
"iPhone 6s Plus", | |
"iPhone SE", | |
"iPhone 7", | |
"iPhone 7 Plus", | |
"iPhone 8", | |
"iPhone 8 Plus", | |
"iPhone X" | |
), | |
"Google" to listOf( | |
"Pixel", | |
"Pixel XL", | |
"Pixel 2", | |
"Pixel 2 XL" | |
), | |
"HTC" to listOf( | |
"Nexus One" | |
), | |
"Huawei" to listOf( | |
"Nexus 6P" | |
), | |
"LG" to listOf( | |
"Nexus 5", | |
"Nexus 5X" | |
), | |
"Motorola" to listOf( | |
"Nexus 6" | |
), | |
"Samsung" to listOf( | |
"Nexus S", | |
"Galaxy Nexus" | |
) | |
) | |
} | |
private val groupAdapter = GroupAdapter<ViewHolder>() | |
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { | |
return inflater.inflate(R.layout.fragment_main, container, false) | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
super.onViewCreated(view, savedInstanceState) | |
val layoutManager = GridLayoutManager(activity, groupAdapter.spanCount) | |
layoutManager.spanSizeLookup = groupAdapter.spanSizeLookup | |
phones.forEach { (brand, phones) -> | |
val brandItem = BrandItem(brand) | |
val expandableGroup = ExpandableGroup(brandItem) | |
phones.forEach { phone -> | |
expandableGroup.add(PhoneItem(phone)) | |
} | |
groupAdapter.add(expandableGroup) | |
} | |
recyclerView.layoutManager = layoutManager | |
recyclerView.adapter = groupAdapter | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.View | |
import com.xwray.groupie.Item | |
import com.xwray.groupie.ViewHolder | |
import kotlinx.android.extensions.LayoutContainer | |
import kotlinx.android.synthetic.main.list_item_phone.* | |
class PhoneItem(private val phone: String): Item<PhoneItem.PhoneViewHolder>() { | |
override fun getLayout(): Int { | |
return R.layout.list_item_phone | |
} | |
override fun createViewHolder(itemView: View): PhoneViewHolder { | |
return PhoneViewHolder(itemView) | |
} | |
override fun bind(holder: PhoneViewHolder, position: Int) { | |
holder.phoneName.text = phone | |
} | |
class PhoneViewHolder(override val containerView: View): ViewHolder(containerView), LayoutContainer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment