Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/adapter/SimpleParcelableUsersAdapte...

75 lines
2.4 KiB
Kotlin
Raw Normal View History

2016-07-07 05:42:08 +02:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.adapter
import android.content.Context
import android.view.View
import android.view.ViewGroup
2017-03-01 15:12:25 +01:00
import com.bumptech.glide.RequestManager
2016-07-07 05:42:08 +02:00
import org.mariotaku.twidere.R
import org.mariotaku.twidere.model.ParcelableUser
import org.mariotaku.twidere.model.UserKey
2017-01-07 07:16:02 +01:00
import org.mariotaku.twidere.view.holder.SimpleUserViewHolder
2016-07-07 05:42:08 +02:00
class SimpleParcelableUsersAdapter(
2016-07-07 05:42:08 +02:00
context: Context,
2017-03-01 15:12:25 +01:00
layoutRes: Int = R.layout.list_item_simple_user,
2017-03-02 07:59:19 +01:00
requestManager: RequestManager
) : BaseArrayAdapter<ParcelableUser>(context, layoutRes, requestManager = requestManager) {
2016-07-07 05:42:08 +02:00
override fun getItemId(position: Int): Long {
val item = getItem(position)
2020-06-09 01:09:31 +02:00
return item?.hashCode()?.toLong() ?: -1
2016-07-07 05:42:08 +02:00
}
2016-07-07 09:39:32 +02:00
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
2016-07-07 05:42:08 +02:00
val view = super.getView(position, convertView, parent)
2017-02-22 02:39:15 +01:00
val holder = view.tag as? SimpleUserViewHolder<*> ?: run {
2017-01-07 07:16:02 +01:00
val h = SimpleUserViewHolder(view, this)
view.tag = h
return@run h
2016-07-07 05:42:08 +02:00
}
val user = getItem(position)
2017-01-07 07:16:02 +01:00
holder.displayUser(user)
2016-07-07 05:42:08 +02:00
return view
}
fun setData(data: List<ParcelableUser>?, clearOld: Boolean = false) {
2016-07-07 05:42:08 +02:00
if (clearOld) {
clear()
}
if (data == null) return
for (user in data) {
if (clearOld || findUserPosition(user.key) < 0) {
add(user)
}
}
}
fun findUserPosition(userKey: UserKey): Int {
for (i in 0 until count) {
2016-07-07 05:42:08 +02:00
if (userKey == getItem(i).key) return i
}
return -1
}
}