1
0
Fork 0

For #355 - Creates ViewHolder for HistoryItem

master
Jeff Boek 2019-02-08 16:49:14 -08:00
parent 64e96495cc
commit aac39f97f2
4 changed files with 72 additions and 23 deletions

View File

@ -9,8 +9,20 @@ import org.mozilla.fenix.mvi.ActionBusFactory
import org.mozilla.fenix.mvi.Change
import org.mozilla.fenix.mvi.UIComponent
import org.mozilla.fenix.mvi.ViewState
import java.net.URL
data class HistoryItem(val url: String)
data class HistoryItem(val url: String) {
val title: String
get() = siteTitle()
private fun siteTitle(): String {
return try {
URL(url).host
} catch (e: Exception) {
url
}
}
}
class HistoryComponent(
private val container: ViewGroup,

View File

@ -9,10 +9,12 @@ import android.content.Context
import android.graphics.Color
import android.os.Build
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.reactivex.Observable
@ -44,8 +46,20 @@ class HistoryUIView(
}
}
private class HistoryAdapter(val context: Context) : RecyclerView.Adapter<HistoryAdapter.ViewHolder>() {
class ViewHolder(val textView: TextView) : RecyclerView.ViewHolder(textView)
private class HistoryAdapter(val context: Context) : RecyclerView.Adapter<HistoryAdapter.HistoryListItemViewHolder>() {
class HistoryListItemViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
private var title = view.findViewById<TextView>(R.id.history_title)
private var url = view.findViewById<TextView>(R.id.history_url)
fun bind(item: HistoryItem) {
title.text = item.title
url.text = item.url
}
companion object {
const val LAYOUT_ID = R.layout.history_list_item
}
}
private var items: List<HistoryItem> = emptyList()
@ -54,23 +68,20 @@ private class HistoryAdapter(val context: Context) : RecyclerView.Adapter<Histor
notifyDataSetChanged()
}
@RequiresApi(Build.VERSION_CODES.M)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val textView = TextView(context).apply {
val lp = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, // Width of TextView
RelativeLayout.LayoutParams.WRAP_CONTENT
)
setLayoutParams(lp)
setText("This is a sample TextView...")
setTextColor(Color.parseColor("#ff0000"))
}
return ViewHolder(textView)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryListItemViewHolder {
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
return HistoryListItemViewHolder(view)
}
override fun getItemViewType(position: Int): Int {
return HistoryListItemViewHolder.LAYOUT_ID
}
override fun getItemCount(): Int = items.count()
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView.text = "Cell: ${items[position]}"
override fun onBindViewHolder(holder: HistoryListItemViewHolder, position: Int) {
val item = items[position]
holder.bind(item)
}
}

View File

@ -1,12 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_height="wrap_content"
android:padding="4dp"
android:paddingStart="20dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/url"
android:id="@+id/history_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
android:layout_marginTop="8dp"
android:textColor="@color/history_title"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/history_url"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="@color/history_url"
android:textSize="12sp"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp"
android:singleLine="true"
android:layout_marginEnd="20dp"
android:textAlignment="viewStart"
android:ellipsize="end"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/history_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -50,4 +50,7 @@
<color name="library_key_icon_background">#FFF4C3</color>
<color name="library_key_icon">#C13905</color>
<color name="history_title">#000000</color>
<color name="history_url">#696A6A</color>
</resources>