Created
October 30, 2019 09:46
-
-
Save adxgun/13274218d6d2ecea92a3ff2f86583492 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myapplication | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.TextView | |
import androidx.recyclerview.widget.RecyclerView | |
import java.lang.IllegalArgumentException | |
import java.lang.IllegalStateException | |
class TransactionAdapter(val data: ArrayList<Row>): | |
RecyclerView.Adapter<TransactionAdapter.TransactionViewHolder>() { | |
override fun getItemViewType(position: Int): Int { | |
return data[position].rowType | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransactionViewHolder { | |
val inflater = LayoutInflater.from(parent.context) | |
return when(viewType) { | |
ROW_TYPE_DATE -> DateViewHolder(inflater.inflate(R.layout.date_layout, parent, false)) | |
ROW_TYPE_DATA -> DataViewHolder(inflater.inflate(R.layout.txn_layout, parent, false)) | |
else -> throw IllegalArgumentException("Invalid rowType") | |
} | |
} | |
override fun getItemCount() = data.size | |
override fun onBindViewHolder(holder: TransactionViewHolder, position: Int) { | |
val row = data[position] | |
val txn = row.data | |
when(row.rowType) { | |
ROW_TYPE_DATA -> {} | |
ROW_TYPE_DATE -> { | |
txn?.let { (holder as DateViewHolder).dateTextView.text = txn.date } | |
} | |
else -> throw IllegalStateException("invalid/unknown row type") | |
} | |
} | |
open class TransactionViewHolder(view: View): RecyclerView.ViewHolder(view) | |
class DateViewHolder(view: View): TransactionViewHolder(view) { | |
val dateTextView = view.findViewById<TextView>(R.id.dateTextVIew) | |
} | |
class DataViewHolder(view: View): TransactionViewHolder(view) // | |
} | |
// Model | |
package com.example.myapplication | |
import android.util.Log | |
import java.util.* | |
import kotlin.collections.ArrayList | |
val transactionDates = arrayOf( | |
"today", "yesterday", "July 7, 2019", | |
"June 7, 2019", "Jan 10, 2019", "Feb 21, 2019", "Mar 15, 2019", | |
"Apr 13, 2019", "May 19, 2019", "Aug 17, 2019" | |
) | |
const val ROW_TYPE_DATA = 1 | |
const val ROW_TYPE_DATE = 2 | |
class Row(val rowType: Int = ROW_TYPE_DATA, val data: Transaction? = null) | |
class Transaction(val date: String = "") | |
fun fakeTransactionData(): ArrayList<Transaction> { | |
return ArrayList<Transaction>().apply { | |
val size = transactionDates.size - 1 | |
for (n in 0..30) { | |
val rand = (0..size).random() | |
add(Transaction(transactionDates[rand])) | |
} | |
} | |
} | |
// Activity | |
package com.example.myapplication | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.recyclerview.widget.LinearLayoutManager | |
import androidx.recyclerview.widget.RecyclerView | |
import kotlinx.android.synthetic.main.content_main.* | |
class TransactionListActivity: AppCompatActivity() { | |
private val transactions = ArrayList<Row>() | |
private val adapter by lazy { TransactionAdapter(transactions) } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
setupRecyclerView() | |
} | |
private fun setupRecyclerView() { | |
rvTransactions.layoutManager = LinearLayoutManager(this, | |
RecyclerView.VERTICAL, false) | |
rvTransactions.adapter = adapter | |
val txns = fakeTransactionData() | |
val txnsCopy = txns | |
txns.forEach { txn -> | |
val date = txn.date | |
addDate(date, txn) | |
// scan txns one-by-one and add the ones with the same date | |
// just below their date label | |
var count = 0 | |
txnsCopy.forEach { | |
if (it.date == date) { | |
count += 1 | |
transactions.add(Row(ROW_TYPE_DATA, it)) | |
} | |
} | |
} | |
adapter.notifyDataSetChanged() | |
} | |
private fun addDate(date: String, txn: Transaction?) { | |
var exists = false | |
for (it in transactions) { | |
if (it.data != null && it.data.date == date) { | |
exists = true | |
break | |
} | |
} | |
if (!exists) { | |
transactions.add(Row(ROW_TYPE_DATE, txn)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment