Use RecyclerView instead of LinearLayout for contact numbers

This commit is contained in:
Moez Bhatti
2018-08-10 23:37:51 -04:00
parent caae77a189
commit 883904f529
3 changed files with 73 additions and 16 deletions
@@ -19,8 +19,8 @@
package com.moez.QKSMS.feature.compose package com.moez.QKSMS.feature.compose
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.moez.QKSMS.R import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkAdapter import com.moez.QKSMS.common.base.QkAdapter
import com.moez.QKSMS.common.base.QkViewHolder import com.moez.QKSMS.common.base.QkViewHolder
@@ -35,14 +35,23 @@ class ContactAdapter @Inject constructor() : QkAdapter<Contact>() {
val contactSelected: Subject<Contact> = PublishSubject.create() val contactSelected: Subject<Contact> = PublishSubject.create()
private val numbersViewPool = RecyclerView.RecycledViewPool()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
val view = layoutInflater.inflate(R.layout.contact_list_item, parent, false) val view = layoutInflater.inflate(R.layout.contact_list_item, parent, false)
view.addresses.setRecycledViewPool(numbersViewPool)
return QkViewHolder(view).apply { return QkViewHolder(view).apply {
view.primary.setOnClickListener { view.primary.setOnClickListener {
val contact = getItem(adapterPosition) val contact = getItem(adapterPosition)
contactSelected.onNext(copyContact(contact, 0)) contactSelected.onNext(copyContact(contact, 0))
} }
view.addresses.adapter = PhoneNumberAdapter { contact, index ->
contactSelected.onNext(copyContact(contact, index + 1))
}
} }
} }
@@ -56,17 +65,9 @@ class ContactAdapter @Inject constructor() : QkAdapter<Contact>() {
view.address.text = contact.numbers.first()?.address ?: "" view.address.text = contact.numbers.first()?.address ?: ""
view.type.text = contact.numbers.first()?.type ?: "" view.type.text = contact.numbers.first()?.type ?: ""
view.addresses.removeAllViews() val adapter = view.addresses.adapter as PhoneNumberAdapter
contact.numbers.forEachIndexed { index, number -> adapter.contact = contact
if (index != 0) { adapter.data = contact.numbers.drop(Math.min(contact.numbers.size, 1))
val numberView = View.inflate(view.context, R.layout.contact_number_list_item, null)
numberView.setOnClickListener { contactSelected.onNext(copyContact(contact, index)) }
numberView.address.text = number.address
numberView.type.text = number.type
view.addresses.addView(numberView)
}
}
} }
/** /**
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.feature.compose
import android.view.LayoutInflater
import android.view.ViewGroup
import com.moez.QKSMS.R
import com.moez.QKSMS.common.base.QkAdapter
import com.moez.QKSMS.common.base.QkViewHolder
import com.moez.QKSMS.model.Contact
import com.moez.QKSMS.model.PhoneNumber
import kotlinx.android.synthetic.main.contact_list_item.view.*
class PhoneNumberAdapter(
private val numberClicked: (Contact, Int) -> Unit
) : QkAdapter<PhoneNumber>() {
lateinit var contact: Contact
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QkViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.contact_number_list_item, parent, false)
return QkViewHolder(view)
}
override fun onBindViewHolder(holder: QkViewHolder, position: Int) {
val number = getItem(position)
val view = holder.itemView
// Setting this in onCreateViewHolder causes a crash sometimes. [contact] returns the
// contact from a different row, I'm not sure why
view.setOnClickListener { numberClicked(contact, position) }
view.address.text = number.address
view.type.text = number.type
}
}
@@ -39,7 +39,7 @@
android:id="@+id/avatar" android:id="@+id/avatar"
android:layout_width="40dp" android:layout_width="40dp"
android:layout_height="40dp" android:layout_height="40dp"
android:layout_marginRight="16dp" /> android:layout_marginEnd="16dp" />
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@@ -55,7 +55,8 @@
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:layout_marginTop="2dp">
<com.moez.QKSMS.common.widget.QkTextView <com.moez.QKSMS.common.widget.QkTextView
android:id="@+id/address" android:id="@+id/address"
@@ -83,10 +84,11 @@
</LinearLayout> </LinearLayout>
<LinearLayout <androidx.recyclerview.widget.RecyclerView
android:id="@+id/addresses" android:id="@+id/addresses"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" /> app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/contact_number_list_item" />
</LinearLayout> </LinearLayout>