setting focus on parent of listview layout - android

I stocked in a problem. I have a listView and this listView is just for showing data row by row and items are all disabled using override function isEnabled to false.
so this is my code:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:paddingTop="10dp"
android:paddingBottom="10dp">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:divider="#null"
android:dividerHeight="0dp"
android:focusable="false"
android:focusableInTouchMode="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I need to set click listener on baseLayout but every thing I've done not made the focus on baseLayout and listview calls absListView touch listener. any idea?
...

You have to set listener on list cell, such as.
It is example for recyler view, but it is same case on listview.
class SearchListAdapter(private val context: Context) :
RecyclerView.Adapter() {
private var products: List<Repo> = arrayListOf()
var listener: AdapterListener? = null
override fun onCreateViewHolder(parent: ViewGroup, i: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_repo, parent, false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return products.size
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
viewHolder.bind(position, products[position])
}
fun setList(contacts: List<Repo>) {
this.products = contacts
notifyDataSetChanged()
}
inner class ViewHolder(itemView: View) : BindingViewHolder<ItemRepoBinding>(itemView) {
fun bind(pos: Int, contact: Repo) {
if (binding == null) {
return
}
binding.tvTitle.text = contact.name
binding.setVariable(
BR.tintlist,
context.resources.getColorStateList(R.color.xml_color_btn_title)
)
binding.root.setOnClickListener(View.OnClickListener {
if (listener != null) {
listener?.onClick(pos, contact)
}
})
}
}
interface AdapterListener {
fun onClick(position: Int, info: Repo)
}
}

Related

Android Kotlin expanding textview in adapter causes unwanted expanding another textviews

I show a list of items using adapter and recyclerview. Each item shows only 4 lines of biography-textView. In order to see whole biography, user clicks 'expand' textView. My code functions but there is a problem because sometimes (somehow randomly) one 'expand' click (f.e. in item No 1) causes expanding 2-3 biography textView more in another items(f.e. items No 7 and 12). What is wrong with my code? How to check it/identify why items textView are extra expand?
Can anyone help?
class ShowMastersAdapter (
private val masterList: ArrayList<Master>,
private val itemListener: OnItemClickListener
) : RecyclerView.Adapter<ShowMastersAdapter.ShowMasterViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ShowMasterViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_master, parent, false)
return ShowMasterViewHolder(view)
}
override fun onBindViewHolder(holder: ShowMasterViewHolder, position: Int) {
holder.bindData(position)
}
override fun getItemCount(): Int {
return masterList.size
}
inner class ShowMasterViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bindData(position: Int) {
val masterBiography = itemView.findViewById<TextView>(R.id.item_master_biography)
val masterExpandBiography =itemView.findViewById<TextView>(R.id.item_master_expand_text)
masterBiography.text = masterList[position].biography
masterExpandBiography.setOnClickListener {
itemListener.onReadMoreTextClick(masterExpandBiography, masterBiography)
}
}
}
interface OnItemClickListener {
fun onReadMoreTextClick(expandText: TextView, biographyText: TextView)
}
}
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/layout_top_part">
<TextView
android:id="#+id/item_master_biography"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/item_master_margin_start"
android:layout_marginEnd="#dimen/item_master_margin_end"
android:ellipsize="end"
android:maxLines="4"
android:text="unknown"
android:textSize="#dimen/font_main_size_item_master"
app:layout_constraintBottom_toTopOf="#+id/item_master_expand_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/item_master_biography_label" />
<TextView
android:id="#+id/item_master_expand_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="#string/it_master_expand_text"
android:textColor="#color/text_as_link"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
class FragmentShowMasters() : Fragment(), ShowMastersAdapter.OnItemClickListener{
private var listOfMasters: ArrayList<Master> = ArrayList()
private lateinit var myRecycler: RecyclerView
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState:Bundle?): View {
myView = inflater.inflate(R.layout.fragment_show_masters, container, false)
myRecycler = myView.findViewById(R.id.recycler_show_masters)
myRecycler.setHasFixedSize(true)
myRecycler.adapter = ShowMastersAdapter(listOfMasters, this, highlightSearchText)
myRecycler.adapter?.notifyDataSetChanged()
myRecycler.layoutManager = LinearLayoutManager(this.context)
return myView
}
override fun onReadMoreTextClick(expandText: TextView, biographyText: TextView) {
if (biographyText.maxLines == 4) {
biographyText.maxLines = Int.MAX_VALUE
expandText.text = getString(R.string.it_master_shrink_text)
} else {
biographyText.maxLines = 4
expandText.text = getString(R.string.it_master_expand_text)
}
}
}

Scroll view inside recyclerview item not working

I am using a Recycler view to show Some Card Views.
Each item of recycler view contains a long text.
I am trying to make that text scrollable.
I tried wrapping the text inside a Nested Scroll View.
But on swiping the item, the whole recycler view is scrolled, instead of the text inside the item.
simpler version of my recycler view item layout.
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="#{()->viewHolder.navigateToDetails()}"
android:paddingHorizontal="25dp"
android:paddingVertical="15dp">
<androidx.core.widget.NestedScrollView
android:id="#+id/crib_text_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constrainedHeight="true"
app:layout_constraintHeight_max="300dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/category">
<com.example.presentation.custom_views.ReadMoreTextView
android:id="#+id/crib_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
app:mainText="#{cribResponse.post}"
tools:mainText="#tools:sample/lorem[20]" />
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
layout of the fragment that contains recycler view
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/crib_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:nestedScrollingEnabled="true"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/item_text_crib" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Adapter class
class CribAdapter(private val rvHost: RVHost) :
PagingDataAdapter<CribResponse, CribViewHolder>(diffCallback) {
private val allAudioViewHolders: MutableList<AudioCribViewHolder> = mutableListOf()
var playingViewHolder: AudioCribViewHolder? = null
companion object {
val diffCallback = object : DiffUtil.ItemCallback<CribResponse>() {
override fun areItemsTheSame(oldItem: CribResponse, newItem: CribResponse): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: CribResponse, newItem: CribResponse): Boolean {
return oldItem.postId == newItem.postId
}
}
}
override fun onBindViewHolder(holder: CribViewHolder, position: Int) {
val item = getItem(position)
item ?: return
holder.bind(item)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CribViewHolder {
return if (viewType == AUDIO_CRIB) {
AudioCribViewHolder(parent = parent, RVHost = rvHost).apply {
markCreated()
allAudioViewHolders.add(this)
}
} else {
TextCribViewHolder(parent = parent, rvHost = rvHost)
}
}
override fun getItemViewType(position: Int): Int {
val item = getItem(position)
val audio = item?.postAudio
return if (audio == null) TEXT_CRIB else AUDIO_CRIB
}
override fun onViewAttachedToWindow(holder: CribViewHolder) {
super.onViewAttachedToWindow(holder)
if (holder is AudioCribViewHolder) {
holder.markAttach()
}
}
override fun onViewDetachedFromWindow(holder: CribViewHolder) {
super.onViewDetachedFromWindow(holder)
if (holder is AudioCribViewHolder) {
val isPlaying = holder.playing.value ?: false
if (isPlaying) {
holder.resetUI()
playingViewHolder = null
rvHost.stop()
}
holder.markDetach()
}
}
fun selfLifecycleDestroyed() {
allAudioViewHolders.forEach {
it.markDestroyed()
}
}
fun getCrib(position: Int): CribResponse? {
return getItem(position)
}
}

All child recyclerView's items changed when one child recyclerview's item is changed while click the item

I am working with nested recyclerView. According to business logic, first I have to call an API that fetched the list of item that is shown in the Parent recycler view. After that, if a user clicks any of the items of the parent recycler view, another API is called which fetches a list of sub-items, and I have to show the item list in the inner recycler view of that clicked-positioned parent recycler view.
I successfully implemented showing items in the parent recycler view and sub-items in the clicked-position nested recycler view.
But the problem I am facing is when I clicked any specific item of the parent recycler view, all the other nested recycler view's item get changed with the newly populated sub-items value.
How can I solve this issue?. Here is the sample code
main_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.material.card.MaterialCardView
style="#style/CardViewStyle"
android:id="#+id/chapter_layout"
android:layout_width="match_parent">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/chapter_name_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.material.textview.MaterialTextView
android:id="#+id/header_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
</com.google.android.material.card.MaterialCardView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/chapter_topic_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="3"
tools:listitem="#layout/item_main" />
</androidx.appcompat.widget.LinearLayoutCompat>
MainItemViewHolder.kt
class MainItemViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
fun bind(item: SpecificChapter, onClick: (SpecificChapter, Int) -> Unit) {
with(view) {
chapter_name_text_view.text = "Chapter "+ item.no
header_text_view.text = item.name
chapter_layout.setOnClickListener { onClick(item, bindingAdapterPosition) }
}
}
}
MainItemAdapter.kt
class MainItemAdapter(
val onClick: (SpecificChapter, Int) -> Unit
) : ListAdapter<SpecificChapter, MainItemViewHolder>(DIFF_CALLBACK) {
companion object {
private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<SpecificChapter>() {
override fun areItemsTheSame(old: SpecificChapter, aNew: SpecificChapter) = (old.id == aNew.id)
override fun areContentsTheSame(old: SpecificChapter, aNew: SpecificChapter) = (old == aNew)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainItemViewHolder {
return MainItemViewHolder(parent.inflate(R.layout.main_item))
}
override fun onBindViewHolder(holder: MainItemViewHolder, position: Int) {
holder.bind(getItem(position)!!, onClick)
}
}
sub_item.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textview.MaterialTextView
android:id="#+id/chapter_topic_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</com.google.android.material.card.MaterialCardView>
SubItemViewHolder.kt
class SubItemViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
fun bind(item: SpecificTopic, onClick: (SpecificTopic) -> Unit) {
with(view) {
chapter_topic_text_view.text = item.name
chapter_topic_layout.setOnClickListener { onClick(item) }
}
}
}
SubItemAdapter.kt
class SubItemAdapter(
val onClick: (SpecificTopic) -> Unit
) : ListAdapter<SpecificTopic, SubItemViewHolder>(DIFF_CALLBACK) {
companion object {
private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<SpecificTopic>() {
override fun areItemsTheSame(old: SpecificTopic, aNew: SpecificTopic) = (old.id == aNew.id)
override fun areContentsTheSame(old: SpecificTopic, aNew: SpecificTopic) = (old == aNew)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubItemViewHolder {
return SubItemViewHolder(parent.inflate(R.layout.sub_item))
}
override fun onBindViewHolder(holder: SpecificTopicViewHolder, position: Int) {
holder.bind(getItem(position)!!, onClick)
}
}
activity_chapter.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/chapter_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/main_item"/>
</androidx.appcompat.widget.LinearLayoutCompat>
ChapterActivity.kt
class ChapterActivity : BaseActivity<ChapterViewModel>() {
var chapterPosition = 0
private val mainItemAdapter: MainItemAdapter by lazy {
MainItemAdapter { specificChapter, position ->
// sub item Api call when clicked specific item. response is viewmodel.allTopics
chapterPosition = position
specificChapter.id?.let { viewModel.getChapterWiseTopics(it) }
}
}
private val subItemAdapter: SubItemAdapter by lazy {
SubItemAdapter {
}
}
override fun onResume() {
super.onResume()
// Main item Api call. response is viewmodel.allChapters
viewModel.getChapters("subject_code")
}
override fun observeLiveData() {
// showing Main item in parent recyclerView
observe(viewModel.allChapters) {
val chapterList = ArrayList<SpecificChapter>()
chapterList.add(SpecificChapter(...))
chapter_recycler_view.adapter = mainItemAdapter
mainItemAdapter.submitList(chapterList)
chapter_recycler_view.setItemViewCacheSize(chapterList.size)
}
// showing sub item in nested recyclerView
observe(viewModel.allTopics) {
val topicList = ArrayList<SpecificTopic>()
topicList.add(SpecificTopic(...))
with((chapter_recycler_view.findViewHolderForAdapterPosition(chapterPosition) as MainItemViewHolder).itemView) {
this.chapter_topic_recycler_view.adapter = subItemAdapter
subItemAdapter.submitList(topicList)
}
}
}
}

setOnclicklistener in a listview with a customadapter

i just started using Android studio with Kotlin.
I implemented a listView with a custom adapter, but i don't understand how detect when i click on a item of this listview.
This is my class CalAdapter.kt
class CalAdapter(context: Context,al_session:ArrayList<activity_session>) : BaseAdapter(){
private val mInflator: LayoutInflater
private val al_session:ArrayList<activity_session>
init {
this.mInflator = LayoutInflater.from(context)
this.al_session=al_session
}
override fun getCount(): Int {
return al_session.size
}
override fun getItem(position: Int): Any {
return al_session.get(position)
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
#SuppressLint("ResourceAsColor", "PrivateResource")
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
val view: View?
val vh: ListRowHolder
if (convertView == null) {
view = this.mInflator.inflate(R.layout.calendar_adapter, parent, false)
vh = ListRowHolder(view)
view.tag = vh
} else {
view = convertView
vh = view.tag as ListRowHolder
}
vh.label.text = al_session[position].id.toString()
vh.ivImage.setBackgroundColor(R.color.material_blue_grey_800)
when (al_session[position].provided_sport)
{
"swimming" ->
{
//vh.ivImage.setBackgroundColor(R.color.material_blue_grey_800)
vh.ivImage.setImageResource(R.drawable.ic_swiming)
}
"running" ->
{
//vh.ivImage.setBackgroundColor(R.color.white)
vh.ivImage.setImageResource(R.drawable.ic_running)
}
"cycling" ->
{
//vh.ivImage.setBackgroundColor(R.color.material_blue_grey_800)
vh.ivImage.setImageResource(R.drawable.ic_bicycle)
}
}
return view
}
}
private class ListRowHolder(row: View?) {
val label: TextView = row?.findViewById(R.id.TWActivityID) as TextView
val ivImage: ImageButton = row?.findViewById(R.id.CalActivityButton) as ImageButton
}
This is my function where i implement my listview.
lv is my listview.
fun createActivities()
{
val lv = dynamicList
val test=ArrayList<activity_session>()
var i : Int = 0
while (i < 5) {
if (ActivityList.activityList[i] != null) {
test.add(ActivityList.activityList[i])
}
i += 1
}
val obj_adapter: CalAdapter
obj_adapter = CalAdapter(this.view!!.dynamicList.context, test)
lv.adapter=obj_adapter
}
I created a xml file which represent my listview.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<LinearLayout
android:id="#+id/AdapterLayout"
android:layout_width="215dp"
android:layout_height="140dp"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageButton
android:id="#+id/CalActivityButton"
android:layout_width="wrap_content"
android:layout_height="140dp"
android:layout_weight="1"
app:srcCompat="#drawable/ic_info_black_24dp" />
<TextView
android:id="#+id/TWActivityID"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:text="TextView"
android:visibility="invisible"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="230dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Could someone explain me how to detect when i click on an item of the listview i just created ?
EDIT : Hi again i understood how to do it, i added a setOnClickListener method to my imagebutton in my CalAdapter.kt class
now it looks like that : https://imgur.com/a/Vw9SD
On your activity you should add a listener to the listview.
list.setOnItemClickListener { parent, view, position, id ->
val myItem = parent.getItemAtPosition(position) as MyDataObj
}
You should also have the getItem() method filled correctly on your adapter as you do.

How can I set the first radio button in RecyclerView checked with Kotlin?

I use the Code A to create a RecyclerView with radio button based some searched sample code from website.
1、I don't know if these code is good, is there a better way to implement radio button in RecyclerView?
2、How can I set the first radio button checked default when I start the APP? You know that none of radio button is checked when I start the APP.
Code A
class CustomAdapter (val backupItemList: List<MSetting>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
private var mSelectedItem = -1
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_recyclerview, parent, false)
return ViewHolder(v)
}
fun getSelectedItem():Int{
return mSelectedItem
}
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(backupItemList[position])
holder.itemView.radioButton.setChecked(position == mSelectedItem);
}
override fun getItemCount(): Int {
return backupItemList.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(aMSetting: MSetting) {
itemView.radioButton.tag=aMSetting._id
itemView.textViewUsername.text=aMSetting.createdDate.toString()
itemView.textViewAddress.text=aMSetting.description
itemView.radioButton.setOnClickListener {
mSelectedItem=getAdapterPosition()
notifyDataSetChanged();
}
}
}
}
XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My First" />
<TextView
android:id="#+id/textViewUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Belal Khan"
/>
<TextView
android:id="#+id/textViewAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Ranchi, Jharkhand"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
private var mSelectedItem = -1
...
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(backupItemList[position], position, mSelectedItem)
}
...
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(aMSetting: MSetting, position: Int, selectedPosition: Int) {
itemView.radioButton.tag=aMSetting._id
itemView.textViewUsername.text=aMSetting.createdDate.toString()
itemView.textViewAddress.text=aMSetting.description
if ((selectedPosition == -1 && position == 0))
itemView.radioButton.setChecked(true)
else
if (selectedPosition == position)
itemView.radioButton.setChecked(true)
else
itemView.radioButton.setChecked(false)
itemView.radioButton.setOnClickListener {
mSelectedItem=getAdapterPosition()
notifyDataSetChanged()
}
}
}
If you want to set the first RadioButton in your list to be true, you can check the adapterPosition and if it is 0 (i.e. first item) set it to true.
For example:
class CustomAdapter
{
var mSelectedItem = 0
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(aMSetting: MSetting) {
....
if(adapterPosition == mSelectedItem)
itemView.radioButton.checked = true
else
itemView.radioButton.checked = false
}
}
}

Categories

Resources