Skip to content

Instantly share code, notes, and snippets.

@abualgait
Last active March 8, 2021 11:02
Show Gist options
  • Select an option

  • Save abualgait/375addeaa541b8000bb7413b132caa9b to your computer and use it in GitHub Desktop.

Select an option

Save abualgait/375addeaa541b8000bb7413b132caa9b to your computer and use it in GitHub Desktop.
a recyclerview allows to select single item
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke
android:width="@dimen/_1sdp"
android:color="#707070" />
</shape>
</item>
<item
android:bottom="3dp"
android:left="3dp"
android:right="3dp"
android:top="3dp">
<selector>
<item android:state_selected="true">
<shape android:shape="oval">
<stroke
android:width="@dimen/_1sdp"
android:color="@color/colorAccent" />
<solid android:color="@color/colorAccent" />
</shape>
</item>
</selector>
</item>
</layer-list>
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="item"
type="com.odiggo.app.ui.paymentdetailsactivity.PaymentOption" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="@dimen/_10sdp">
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/custom_radio_shape" />
<Space
android:layout_width="@dimen/_10sdp"
android:layout_height="0dp" />
<ImageView
android:id="@+id/ivPaymentMethod"
android:layout_width="50dp"
android:layout_height="32dp"
android:src="@drawable/ic_fawry" />
<Space
android:layout_width="@dimen/_10sdp"
android:layout_height="0dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
style="@style/TextAppearance.Text.Bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{item.title}"
android:textColor="@color/greyish_brown"
android:textSize="@dimen/_14ssp"
tools:text="@tools:sample/lorem" />
<TextView
android:id="@+id/radio"
style="@style/TextAppearance.Text.Regular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{item.subTitle}"
android:textColor="@color/greyish_brown"
tools:text="Pay with Cash when your order arrives at your doorstep." />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/odiggo" />
</LinearLayout>
</layout>
class PaymentChoiceViewHolder(
val context: Context,
private val binding: ItemPaymentBinding,
val callback: (item: PaymentOption, view: View, position: Int) -> Unit
) : RecyclerViewHolder(binding.root) {
var selectedItem = -1
override fun onBindView(`object`: Any, position: Int) {
val data = `object` as PaymentOption
binding.item = data
binding.ivPaymentMethod.setImageResource(data.icon)
binding.root.isSelected = data.isSelected
if (selectedItem == position) {
data.isSelected = true
binding.root.isSelected = true
} else {
data.isSelected = false
binding.root.isSelected = false
}
binding.root.onClicked {
callback(data, binding.root, position)
}
}
}
data class PaymentOption(
var id: Int = 0,
@DrawableRes
var icon: Int = 0,
var title: String? = "",
var subTitle: String? = "",
var isSelected: Boolean = false
)
class PaymentOptionsListAdapter(
val context: Context,
val callback: (item: PaymentOption, view: View, position: Int) -> Unit
) :
ListAdapter<PaymentOption, RecyclerViewHolder>(DIFF_CALLBACK) {
private var selectedItem = -1
fun itemClickedChanged(newPosition: Int) {
val prevItem = selectedItem
this.selectedItem = newPosition
notifyItemChanged(prevItem)
notifyItemChanged(newPosition)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
return PaymentChoiceViewHolder(
context,
ItemPaymentBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
), callback
)
}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
(holder as PaymentChoiceViewHolder).selectedItem = selectedItem
holder.onBindView(getItem(position), position)
}
companion object {
private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<PaymentOption>() {
override fun areItemsTheSame(oldItem: PaymentOption, newItem: PaymentOption): Boolean =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: PaymentOption, newItem: PaymentOption): Boolean =
oldItem == newItem
}
}
}
class MainActivity : Activity() {
private val paymentOptionsAdapter: PaymentOptionsListAdapter by lazy {
PaymentOptionsListAdapter(
activity(),
::onItemClick
)
}
var selectedPaymentType: Int = -1
private fun onItemClick(
item: PaymentOption,
view: View,
position: Int
) {
paymentOptionsAdapter.itemClickedChanged(position)
selectedPaymentType = item.id
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment