1
0
Fork 0

Cleaning up level-select recycler view adapter

This commit is contained in:
Eoin Mcloughlin 2020-05-15 17:17:30 +01:00
parent b8602d4677
commit 3c4716b0c4
5 changed files with 44 additions and 50 deletions

View file

@ -0,0 +1,29 @@
package com.example.ecwt
object KochLessonDefinitions {
private const val LessonOrder = "KMRSUAPTLOWI.NJEF0YVG5/Q9ZH38B?427C1D6X" // This order is from http://www.hfradio.org/koch_2.html //TODO Add prosign lessons?
val Lessons : MutableList<KochLesson> = ArrayList()
init {
for (i in 1 until LessonOrder.length) {
Lessons.add(KochLesson(i))
}
}
data class KochLesson(val lessonIndex : Int) {
override fun toString() : String {
// The zeroth lesson doesn't exist, since it would only contain a single character //TODO raise exception?
if(lessonIndex == 1) {
// The first lesson is a little special, as it introduces two letters:
return LessonOrder.substring(0, 1) + " and " + LessonOrder.substring(1, 2)
}
return LessonOrder.substring(lessonIndex , lessonIndex + 1)
}
fun indexForHumans() : Int {
return lessonIndex
}
}
}

View file

@ -2,33 +2,8 @@ package com.example.ecwt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.AbsListView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.ecwt.dummy.DummyContent
class LevelSelectAdaptor : RecyclerView.Adapter<LevelSelectAdaptor.LevelSelectViewHolder>() {
class LevelSelectViewHolder(val textView : LinearLayout) : RecyclerView.ViewHolder(textView)
override fun getItemCount(): Int {
return 3
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): LevelSelectViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.fragment_training_level, parent, false) as LinearLayout
return LevelSelectViewHolder(itemView)
}
override fun onBindViewHolder(holder: LevelSelectViewHolder, position: Int) {
holder.textView.findViewById<TextView>(R.id.content).text = "OMG"
}
}
class LevelSelectActivity : AppCompatActivity(),
TrainingLevelFragment.OnListFragmentInteractionListener {
@ -38,21 +13,19 @@ class LevelSelectActivity : AppCompatActivity(),
setContentView(R.layout.activity_level_select)
mLevelSelectLayoutManager = LinearLayoutManager(this)
mLevelSelectViewAdapter = LevelSelectAdaptor()
mLevelSelectView = findViewById<RecyclerView>(R.id.levelSelection_view).apply {
setHasFixedSize(true)
layoutManager = mLevelSelectLayoutManager
adapter = mLevelSelectViewAdapter
//adapter = mLevelSelectViewAdapter
}
}
override fun onListFragmentInteraction(item: DummyContent.DummyItem?) {
override fun onListFragmentInteraction(lesson: KochLessonDefinitions.KochLesson?) {
TODO("Not yet implemented")
}
private lateinit var mLevelSelectView : RecyclerView
private lateinit var mLevelSelectLayoutManager : RecyclerView.LayoutManager
private lateinit var mLevelSelectViewAdapter : LevelSelectAdaptor
}

View file

@ -10,9 +10,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.ecwt.dummy.DummyContent
import com.example.ecwt.dummy.DummyContent.DummyItem
/**
* A fragment representing a list of Items.
* Activities containing this fragment MUST implement the
@ -46,7 +43,7 @@ class TrainingLevelFragment : Fragment() {
columnCount <= 1 -> LinearLayoutManager(context)
else -> GridLayoutManager(context, columnCount)
}
adapter = TrainingLevelRecyclerViewAdapter(DummyContent.ITEMS, listener)
adapter = TrainingLevelRecyclerViewAdapter(KochLessonDefinitions.Lessons, listener)
}
}
return view
@ -78,8 +75,7 @@ class TrainingLevelFragment : Fragment() {
* for more information.
*/
interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
fun onListFragmentInteraction(item: DummyItem?)
fun onListFragmentInteraction(lesson: KochLessonDefinitions.KochLesson?)
}
companion object {

View file

@ -8,17 +8,11 @@ import android.widget.TextView
import com.example.ecwt.TrainingLevelFragment.OnListFragmentInteractionListener
import com.example.ecwt.dummy.DummyContent.DummyItem
import kotlinx.android.synthetic.main.fragment_training_level.view.*
/**
* [RecyclerView.Adapter] that can display a [DummyItem] and makes a call to the
* specified [OnListFragmentInteractionListener].
* TODO: Replace the implementation with code for your data type.
*/
class TrainingLevelRecyclerViewAdapter(
private val mValues: List<DummyItem>,
private val mLessons: List<KochLessonDefinitions.KochLesson>,
private val mListener: OnListFragmentInteractionListener?
) : RecyclerView.Adapter<TrainingLevelRecyclerViewAdapter.ViewHolder>() {
@ -26,7 +20,7 @@ class TrainingLevelRecyclerViewAdapter(
init {
mOnClickListener = View.OnClickListener { v ->
val item = v.tag as DummyItem
val item = v.tag as KochLessonDefinitions.KochLesson
// Notify the active callbacks interface (the activity, if the fragment is attached to
// one) that an item has been selected.
mListener?.onListFragmentInteraction(item)
@ -40,17 +34,17 @@ class TrainingLevelRecyclerViewAdapter(
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = mValues[position]
holder.mIdView.text = item.id
holder.mContentView.text = item.content
val lesson = mLessons[position]
holder.mIdView.text = "Lesson " + lesson.indexForHumans().toString()
holder.mContentView.text = lesson.toString()
with(holder.mView) {
tag = item
tag = lesson
setOnClickListener(mOnClickListener)
}
}
override fun getItemCount(): Int = mValues.size
override fun getItemCount(): Int = mLessons.size
inner class ViewHolder(val mView: View) : RecyclerView.ViewHolder(mView) {
val mIdView: TextView = mView.item_number

View file

@ -9,6 +9,8 @@
<fragment
android:id="@+id/levelSelection_view"
android:name="com.example.ecwt.TrainingLevelFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>