Skip to content

Commit

Permalink
feat add category recyclerview
Browse files Browse the repository at this point in the history
  • Loading branch information
root14 committed May 17, 2023
1 parent ae2f643 commit fbf8e99
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package com.root14.flashlightappsmarket.model

import android.graphics.drawable.Drawable

/**
* Created by ilkay on 17,May, 2023
*/

data class AppItem(
val icon: Drawable?,
val name: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.root14.flashlightappsmarket.model

import android.graphics.drawable.Drawable

/**
* Created by ilkay on 17,May, 2023
*/
data class CategoryItem(
val icon: Drawable?,
val name: String
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.root14.flashlightappsmarket.view
package com.root14.flashlightappsmarket.view.ui.applicationFragment

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.root14.flashlightappsmarket.model.AppItem

/**
* Created by ilkay on 17,May, 2023
*/
class AppAdapter(private val appList: List<AppItem>) : RecyclerView.Adapter<AppViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AppViewHolder {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.root14.flashlightappsmarket.view
package com.root14.flashlightappsmarket.view.ui.applicationFragment

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.root14.flashlightappsmarket.databinding.ItemAppBinding
import com.root14.flashlightappsmarket.model.AppItem

/**
* Created by ilkay on 17,May, 2023
*/
class AppViewHolder(private val binding: ItemAppBinding) : RecyclerView.ViewHolder(binding.root) {

fun bind(appItem: AppItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.root14.flashlightappsmarket.R
import com.root14.flashlightappsmarket.databinding.FragmentApplicationBinding
import com.root14.flashlightappsmarket.databinding.FragmentMainBinding
import com.root14.flashlightappsmarket.model.AppItem
import com.root14.flashlightappsmarket.view.AppAdapter

/**
* Created by ilkay on 17,May, 2023
*/

/**
* list applications
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.root14.flashlightappsmarket.view.ui.mainfragment

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.root14.flashlightappsmarket.model.CategoryItem

/**
* Created by ilkay on 17,May, 2023
*/
class CategoryAdapter(private val categoryItem: List<CategoryItem>) :
RecyclerView.Adapter<CategoryViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
return CategoryViewHolder.create(parent)
}

override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
val categoryItem = categoryItem[position]
holder.bind(categoryItem)
}

override fun getItemCount(): Int {
return categoryItem.size
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.root14.flashlightappsmarket.view.ui.mainfragment

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.root14.flashlightappsmarket.databinding.ItemAppBinding
import com.root14.flashlightappsmarket.databinding.ListCategoriesBinding
import com.root14.flashlightappsmarket.model.AppItem
import com.root14.flashlightappsmarket.model.CategoryItem

/**
* Created by ilkay on 17,May, 2023
*/
class CategoryViewHolder(private val binding: ListCategoriesBinding) :
RecyclerView.ViewHolder(binding.root) {

fun bind(categoryItem: CategoryItem) {
binding.imageViewIcon.setImageDrawable(categoryItem.icon)
binding.textViewCategoryName.text = categoryItem.name
}

companion object {
fun create(parent: ViewGroup): CategoryViewHolder {
val inflater = LayoutInflater.from(parent.context)
val binding = ListCategoriesBinding.inflate(inflater, parent, false)
return CategoryViewHolder(binding)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.root14.flashlightappsmarket.R
import com.root14.flashlightappsmarket.databinding.FragmentApplicationBinding
import com.root14.flashlightappsmarket.databinding.FragmentMainBinding
import com.root14.flashlightappsmarket.model.AppItem
import com.root14.flashlightappsmarket.view.AppAdapter
import com.root14.flashlightappsmarket.model.CategoryItem
import com.root14.flashlightappsmarket.view.ui.applicationFragment.AppAdapter

/**
* main fragment listing categories
*/
class MainFragment : Fragment() {
private lateinit var binding: FragmentMainBinding
private lateinit var appAdapter: AppAdapter
private lateinit var categoryAdapter: CategoryAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
Expand All @@ -33,5 +34,25 @@ class MainFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val categoryList = createCategoryList()
categoryAdapter = CategoryAdapter(categoryList)
binding.rvCategories.layoutManager = LinearLayoutManager(requireContext())
binding.rvCategories.adapter = categoryAdapter
}

//test
private fun createCategoryList() = listOf<CategoryItem>(
CategoryItem(
context?.let { ContextCompat.getDrawable(it, R.drawable.baseline_flashlight_on_24) },
"Flashlights"
),
CategoryItem(
context?.let { ContextCompat.getDrawable(it, R.drawable.baseline_light_mode_24) },
"Colored Lights"
),
CategoryItem(
context?.let { ContextCompat.getDrawable(it, R.drawable.baseline_sos_24) },
"Sos Alerts"
)
)
}
22 changes: 22 additions & 0 deletions app/src/main/res/layout/list_categories.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">


<ImageView
android:id="@+id/imageViewIcon"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/baseline_photo_24" />

<TextView
android:id="@+id/textViewCategoryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/category_name"
android:textSize="16sp"
android:textStyle="bold" />

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
<string name="flashlights">Flashlights</string>
<string name="colored_lights">Colored Lights</string>
<string name="sos_alerts">SOS Alerts</string>
<string name="category_name">Category Name</string>
</resources>

0 comments on commit fbf8e99

Please sign in to comment.