binding recyclerview with stubdata in inventory

change_to_navgraph
Lucas Delanier 2 years ago
parent 50adddc006
commit 24011493ad

@ -5,6 +5,10 @@ import androidx.fragment.app.Fragment
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.data.Stub
import com.example.shakecraft.view.adapter.AdapterInventory
// TODO: Rename parameter arguments, choose names that match // TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
@ -17,43 +21,23 @@ private const val ARG_PARAM2 = "param2"
* create an instance of this fragment. * create an instance of this fragment.
*/ */
class InventoryFragment : Fragment() { class InventoryFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null var stubdata = Stub().load();
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_inventory, container, false)
}
companion object { val view = inflater.inflate(R.layout.fragment_inventory, container, false)
/** val recyclerView: RecyclerView = view.findViewById(R.id.recyclerviewInventory)
* Use this factory method to create a new instance of with(recyclerView) {
* this fragment using the provided parameters. layoutManager = LinearLayoutManager(view.context)
* adapter = AdapterInventory(stubdata)
* @param param1 Parameter 1. }
* @param param2 Parameter 2. return view
* @return A new instance of fragment InventoryFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
InventoryFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
} }
} }

@ -8,11 +8,15 @@ import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat import androidx.core.view.WindowInsetsControllerCompat
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.data.Stub
import com.example.shakecraft.view.adapter.AdapterInventory
import com.google.android.material.bottomnavigation.BottomNavigationView import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
private var progressValue = 20
private var lastShakePosition = 0f
// Function to hide NavigationBar // Function to hide NavigationBar
@ -38,6 +42,8 @@ class MainActivity : AppCompatActivity() {
hideSystemUI() hideSystemUI()
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
loadFragment(HomeFragment()) loadFragment(HomeFragment())
bottomNav = findViewById(R.id.bottomNav) as BottomNavigationView bottomNav = findViewById(R.id.bottomNav) as BottomNavigationView
bottomNav.setOnItemSelectedListener { bottomNav.setOnItemSelectedListener {
when (it.itemId) { when (it.itemId) {
@ -56,6 +62,7 @@ class MainActivity : AppCompatActivity() {
else -> false else -> false
} }
} }
} }
private fun loadFragment(fragment: Fragment){ private fun loadFragment(fragment: Fragment){
val transaction = supportFragmentManager.beginTransaction() val transaction = supportFragmentManager.beginTransaction()

@ -0,0 +1,17 @@
package com.example.shakecraft.data
import com.example.shakecraft.model.Item
class Stub {
fun load() : List<Item>{
val items : MutableList<Item> = mutableListOf<Item>()
items.add(Item(name = "Beech Log", rarity = 0, stack = 1));
items.add(Item(name = "Bronze Ore", rarity = 0, stack = 1));
items.add(Item(name = "Iron Ore", rarity = 0, stack = 1));
items.add(Item(name = "Diamond", rarity = 0, stack = 1));
return items;
}
}

@ -0,0 +1,9 @@
package com.example.shakecraft.model
data class Item(
var name: String,
var rarity: Int,
var stack: Int,
)

@ -0,0 +1,45 @@
package com.example.shakecraft.view.adapter
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.example.shakecraft.R
import com.example.shakecraft.model.Item
import com.example.shakecraft.view.viewholder.ViewHolderInventory
class AdapterInventory(private val inventory: List<Item>) : RecyclerView.Adapter<AdapterInventory.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView
init {
// Define click listener for the ViewHolder's View
textView = view.findViewById(R.id.item_name)
}
fun bind(item: Item) {
textView.text = item.name
}
}
override fun getItemCount() = inventory.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.list_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val item : Item = inventory[position]
viewHolder.bind(item)
}
}

@ -0,0 +1,3 @@
package com.example.shakecraft.view.viewholder
class ViewHolderInventory(inflate: Any?) {
}

@ -39,7 +39,8 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2"></EditText> app:layout_constraintTop_toBottomOf="@+id/textView2"></EditText>
<ListView <androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerviewInventory"
android:layout_marginHorizontal="10dp" android:layout_marginHorizontal="10dp"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
@ -19,6 +19,7 @@
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<ImageView <ImageView
android:id="@+id/item_image"
android:layout_width="60dp" android:layout_width="60dp"
android:layout_height="60dp" android:layout_height="60dp"
app:srcCompat="@drawable/ic_key" /> app:srcCompat="@drawable/ic_key" />
@ -32,6 +33,7 @@
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<TextView <TextView
android:id="@+id/item_name"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Beech Log" android:text="Beech Log"
@ -45,6 +47,7 @@
<TextView <TextView
android:id="@+id/item_stock"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="1,6K" android:text="1,6K"

Loading…
Cancel
Save