Commit 23d0e4c3 authored by Lukas Bersinger's avatar Lukas Bersinger
Browse files

move task list to separate fragment

parent 82882ff1
......@@ -29,10 +29,11 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
......
package ch.ost.rj.mge.miniproject.tasktracker.activities
import android.os.Bundle
import android.widget.FrameLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import ch.ost.rj.mge.miniproject.tasktracker.R
import ch.ost.rj.mge.miniproject.tasktracker.adapters.TaskListAdapter
import ch.ost.rj.mge.miniproject.tasktracker.fragments.TaskListFragment
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
import ch.ost.rj.mge.miniproject.tasktracker.repositories.TaskRepository
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
private var taskRepository = TaskRepository()
private lateinit var listFragment: TaskListFragment
private lateinit var taskListContainer: FrameLayout
private lateinit var btnAddTask: FloatingActionButton
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -27,14 +30,16 @@ class MainActivity : AppCompatActivity() {
// TODO
}
viewManager = LinearLayoutManager(this)
viewAdapter = TaskListAdapter(taskRepository.getTasks())
taskListContainer = findViewById(R.id.fltTaskListContainer)
val tasks: List<Task> = taskRepository.getTasks()
listFragment = TaskListFragment.newInstance(tasks)
recyclerView = findViewById(R.id.rvTaskList)
recyclerView.apply {
setHasFixedSize(true)
layoutManager = viewManager
adapter = viewAdapter
if (tasks.isNotEmpty()) {
supportFragmentManager.beginTransaction()
.add(R.id.fltTaskListContainer, listFragment)
.commit()
}
}
......
......@@ -9,7 +9,7 @@ import ch.ost.rj.mge.miniproject.tasktracker.R
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
class TaskListAdapter(private val dataset: ArrayList<Task>) :
class TaskListAdapter(private val dataset: List<Task>) :
RecyclerView.Adapter<TaskListAdapter.TaskListViewHolder>() {
class TaskListViewHolder(val cardView: CardView) : RecyclerView.ViewHolder(cardView)
......
package ch.ost.rj.mge.miniproject.tasktracker.fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import ch.ost.rj.mge.miniproject.tasktracker.R
import ch.ost.rj.mge.miniproject.tasktracker.adapters.TaskListAdapter
import ch.ost.rj.mge.miniproject.tasktracker.models.Task
import ch.ost.rj.mge.miniproject.tasktracker.repositories.TaskRepository
/**
* A simple [Fragment] subclass.
* Use the [TaskListFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class TaskListFragment : Fragment() {
private lateinit var tasks: List<Task>
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewManager = LinearLayoutManager(context)
viewAdapter = TaskListAdapter(tasks)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val fragment = inflater.inflate(R.layout.fragment_task_list, container, false)
recyclerView = fragment.findViewById(R.id.rvTaskList)
recyclerView.apply {
setHasFixedSize(true)
layoutManager = viewManager
adapter = viewAdapter
}
return fragment
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @return A new instance of fragment TaskListFragment.
*/
@JvmStatic
fun newInstance(tasks: List<Task>) = TaskListFragment().apply {
this.tasks = tasks
}
}
}
\ No newline at end of file
......@@ -6,14 +6,16 @@
android:layout_height="match_parent"
tools:context=".activities.MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvTaskList"
android:layout_width="0dp"
android:layout_height="0dp"
<FrameLayout
android:id="@+id/fltTaskListContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btnAddTask"
......
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frgTaskList"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.TaskListFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvTaskList"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<resources>
<string name="app_name">TaskTracker</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment