Skip to content

Commit 8307688

Browse files
committed
detail
1 parent 782097f commit 8307688

File tree

7 files changed

+71
-5
lines changed

7 files changed

+71
-5
lines changed

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
apply plugin: 'com.android.application'
22
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
34
apply plugin: 'androidx.navigation.safeargs'
45
apply plugin: 'kotlin-kapt'
56

7+
androidExtensions { experimental = true }
8+
69
android {
710
compileSdkVersion 29
811
buildToolsVersion "29.0.3"
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.hoc081098.firestore_coroutinesflow.domain.entity
22

3+
import android.os.Parcelable
4+
import kotlinx.android.parcel.Parcelize
5+
6+
@Parcelize
37
data class Category(
48
val id: String,
59
val name: String,
610
val imageUrl: String,
7-
)
11+
) : Parcelable
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.hoc081098.firestore_coroutinesflow.ui.category_detail
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import androidx.fragment.app.Fragment
6+
import com.hoc081098.firestore_coroutinesflow.R
7+
8+
class CategoryDetailFragment : Fragment(R.layout.category_detail_fragment) {
9+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
10+
super.onViewCreated(view, savedInstanceState)
11+
12+
}
13+
}

app/src/main/java/com/hoc081098/firestore_coroutinesflow/ui/main/CategoryAdapter.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import android.view.ViewGroup
55
import androidx.recyclerview.widget.DiffUtil
66
import androidx.recyclerview.widget.ListAdapter
77
import androidx.recyclerview.widget.RecyclerView
8+
import androidx.recyclerview.widget.RecyclerView.NO_POSITION
89
import com.bumptech.glide.load.engine.DiskCacheStrategy
910
import com.hoc081098.firestore_coroutinesflow.GlideRequests
1011
import com.hoc081098.firestore_coroutinesflow.R
1112
import com.hoc081098.firestore_coroutinesflow.databinding.CategoryItemBinding
1213
import com.hoc081098.firestore_coroutinesflow.domain.entity.Category
1314

1415
class CategoryAdapter(
15-
private val glide: GlideRequests
16+
private val glide: GlideRequests,
17+
private val onClickCategory: (Category) -> Unit,
1618
) : ListAdapter<Category, CategoryAdapter.VH>(
1719
object : DiffUtil.ItemCallback<Category>() {
1820
override fun areItemsTheSame(oldItem: Category, newItem: Category) =
@@ -36,6 +38,15 @@ class CategoryAdapter(
3638
override fun onBindViewHolder(holder: VH, position: Int) = holder.bind(getItem(position))
3739

3840
inner class VH(private val binding: CategoryItemBinding) : RecyclerView.ViewHolder(binding.root) {
41+
init {
42+
binding.root.setOnClickListener onClick@{
43+
val position = bindingAdapterPosition
44+
if (position != NO_POSITION) {
45+
onClickCategory(getItem(position))
46+
}
47+
}
48+
}
49+
3950
fun bind(item: Category) {
4051
glide
4152
.load(item.imageUrl)

app/src/main/java/com/hoc081098/firestore_coroutinesflow/ui/main/MainFragment.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.hoc081098.firestore_coroutinesflow.ui.main
22

33
import android.os.Bundle
4-
import android.util.Log
54
import android.view.View
65
import androidx.core.view.isGone
76
import androidx.core.view.isVisible
87
import androidx.fragment.app.Fragment
8+
import androidx.navigation.fragment.findNavController
99
import androidx.recyclerview.widget.GridLayoutManager
1010
import com.hoc081098.firestore_coroutinesflow.GlideApp
1111
import com.hoc081098.firestore_coroutinesflow.Lce
@@ -23,7 +23,11 @@ class MainFragment : Fragment(R.layout.main_fragment) {
2323
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
2424
super.onViewCreated(view, savedInstanceState)
2525

26-
val categoryAdapter = CategoryAdapter(GlideApp.with(this))
26+
val categoryAdapter = CategoryAdapter(GlideApp.with(this)) { category ->
27+
MainFragmentDirections
28+
.actionMainFragmentToCategoryDetailFragment(category)
29+
.let { findNavController().navigate(it) }
30+
}
2731

2832
binding.recyclerView.run {
2933
setHasFixedSize(true)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<TextView
9+
android:id="@+id/textView2"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:text="TextView"
13+
tools:layout_editor_absoluteX="137dp"
14+
tools:layout_editor_absoluteY="194dp" />
15+
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/navigation/main.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,21 @@
99
android:id="@+id/mainFragment"
1010
android:name="com.hoc081098.firestore_coroutinesflow.ui.main.MainFragment"
1111
android:label="main_fragment"
12-
tools:layout="@layout/main_fragment" />
12+
tools:layout="@layout/main_fragment">
13+
<action
14+
android:id="@+id/action_mainFragment_to_categoryDetailFragment"
15+
app:destination="@id/categoryDetailFragment" />
16+
</fragment>
17+
18+
<fragment
19+
android:id="@+id/categoryDetailFragment"
20+
android:name="com.hoc081098.firestore_coroutinesflow.ui.category_detail.CategoryDetailFragment"
21+
android:label="CategoryDetailFragment">
22+
23+
<argument
24+
android:name="category"
25+
app:argType="com.hoc081098.firestore_coroutinesflow.domain.entity.Category"
26+
app:nullable="false" />
27+
28+
</fragment>
1329
</navigation>

0 commit comments

Comments
 (0)