new navigation and button on homepage work well 🚀

change_to_navgraph
Lucas Delanier 2 years ago
parent cc8a42fca5
commit 86f8a8e2b7

@ -15,6 +15,8 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.ProgressBar import android.widget.ProgressBar
import android.widget.TextView
import androidx.navigation.fragment.findNavController
import com.example.shakecraft.data.Stub import com.example.shakecraft.data.Stub
import com.example.shakecraft.model.Boss import com.example.shakecraft.model.Boss
import com.example.shakecraft.model.Generator import com.example.shakecraft.model.Generator
@ -23,9 +25,9 @@ import kotlin.math.pow
import kotlin.math.sqrt import kotlin.math.sqrt
class BossFragment(var player: Player) : Fragment() { class BossFragment() : Fragment() {
var stubdata = Stub().load();
private lateinit var sensorManager: SensorManager private lateinit var sensorManager: SensorManager
private lateinit var accelerometer: Sensor private lateinit var accelerometer: Sensor
private lateinit var accelerometerEventListener: SensorEventListener private lateinit var accelerometerEventListener: SensorEventListener
@ -42,8 +44,16 @@ class BossFragment(var player: Player) : Fragment() {
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
var player = (activity as MainActivity).currentPlayer
// Récupérez une référence à la ProgressBar dans la vue // Récupérez une référence à la ProgressBar dans la vue
val view = inflater.inflate(R.layout.fragment_boss, container, false) val view = inflater.inflate(R.layout.fragment_boss, container, false)
val buttonCollect = view.findViewById<TextView>(R.id.backbutton)
buttonCollect.setOnClickListener{
findNavController().navigate(R.id.action_bossFragment_to_homeFragment)
}
progressBar = view.findViewById(R.id.progressBar) progressBar = view.findViewById(R.id.progressBar)
image = view.findViewById(R.id.imageBoss) image = view.findViewById(R.id.imageBoss)
@ -61,7 +71,6 @@ class BossFragment(var player: Player) : Fragment() {
// Ne faites rien ici // Ne faites rien ici
} }
@SuppressLint("ServiceCast")
override fun onSensorChanged(event: SensorEvent?) { override fun onSensorChanged(event: SensorEvent?) {
val acceleration = sqrt( val acceleration = sqrt(
event!!.values[0].pow(2) + event.values[1].pow(2) + event.values[2].pow(2) event!!.values[0].pow(2) + event.values[1].pow(2) + event.values[2].pow(2)

@ -16,6 +16,8 @@ import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.ProgressBar import android.widget.ProgressBar
import android.widget.TextView import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.navigation.fragment.findNavController
import com.example.shakecraft.data.Stub import com.example.shakecraft.data.Stub
import com.example.shakecraft.model.Generator import com.example.shakecraft.model.Generator
import com.example.shakecraft.model.Player import com.example.shakecraft.model.Player
@ -23,8 +25,7 @@ import kotlin.math.pow
import kotlin.math.sqrt import kotlin.math.sqrt
class CollectFragment(var player: Player) : Fragment() { class CollectFragment() : Fragment() {
var stubdata = Stub().load();
private lateinit var sensorManager: SensorManager private lateinit var sensorManager: SensorManager
private lateinit var accelerometer: Sensor private lateinit var accelerometer: Sensor
private lateinit var accelerometerEventListener: SensorEventListener private lateinit var accelerometerEventListener: SensorEventListener
@ -42,9 +43,16 @@ class CollectFragment(var player: Player) : Fragment() {
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
var player = (activity as MainActivity).currentPlayer
// Récupérez une référence à la ProgressBar dans la vue // Récupérez une référence à la ProgressBar dans la vue
val view = inflater.inflate(R.layout.fragment_collect, container, false) val view = inflater.inflate(R.layout.fragment_collect, container, false)
val buttonCollect = view.findViewById<TextView>(R.id.backbutton)
buttonCollect.setOnClickListener{
findNavController().navigate(R.id.action_collectFragment_to_homeFragment)
}
progressBar = view.findViewById(R.id.progressBar) progressBar = view.findViewById(R.id.progressBar)
sensorManager = requireActivity().getSystemService(Context.SENSOR_SERVICE) as SensorManager sensorManager = requireActivity().getSystemService(Context.SENSOR_SERVICE) as SensorManager
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)

@ -5,6 +5,9 @@ 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 android.widget.Button
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.navigation.fragment.findNavController
// 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 +20,28 @@ private const val ARG_PARAM2 = "param2"
* create an instance of this fragment. * create an instance of this fragment.
*/ */
class HomeFragment : Fragment() { class HomeFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) 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 val view = inflater.inflate(R.layout.fragment_home,container,false)
return inflater.inflate(R.layout.fragment_home, container, false) val buttonCollect = view.findViewById<ConstraintLayout>(R.id.buttonCollect)
buttonCollect.setOnClickListener{
findNavController().navigate(R.id.action_homeFragment_to_collectFragment)
}
val buttonBoss = view.findViewById<ConstraintLayout>(R.id.buttonBoss)
buttonBoss.setOnClickListener{
findNavController().navigate(R.id.action_homeFragment_to_bossFragment)
}
return view
} }
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
HomeFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
} }

@ -12,16 +12,20 @@ import com.example.shakecraft.model.Player
import com.example.shakecraft.view.adapter.AdapterInventory import com.example.shakecraft.view.adapter.AdapterInventory
class InventoryFragment(var player: Player) : Fragment( ) { class InventoryFragment() : Fragment( ) {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View? {
var player = (activity as MainActivity).currentPlayer
val view = inflater.inflate(R.layout.fragment_inventory, container, false) val view = inflater.inflate(R.layout.fragment_inventory, container, false)
val recyclerView: RecyclerView = view.findViewById(R.id.recyclerviewInventory) val recyclerView: RecyclerView = view.findViewById(R.id.recyclerviewInventory)
with(recyclerView) { with(recyclerView) {

@ -8,6 +8,8 @@ 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.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.example.shakecraft.data.Stub import com.example.shakecraft.data.Stub
@ -42,34 +44,14 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
hideSystemUI() hideSystemUI()
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
loadFragment(HomeFragment())
bottomNav = findViewById(R.id.bottomNav) as BottomNavigationView bottomNav = findViewById(R.id.bottomNavigationView) as BottomNavigationView
bottomNav.setOnItemSelectedListener { val navController = findNavController(R.id.fragment)
when (it.itemId) { bottomNav.setupWithNavController(navController)
R.id.home -> {
loadFragment(HomeFragment())
true
}
R.id.inventory -> {
loadFragment(InventoryFragment(currentPlayer))
true
}
R.id.plus -> {
loadFragment(CollectFragment(currentPlayer))
true
}
else -> false
}
}
} }
private fun loadFragment(fragment: Fragment){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container,fragment)
transaction.commit()
}
} }

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/grey_100" />
</shape>
</item>
<item android:top="2dp" >
<shape android:shape="rectangle">
<solid android:color="@color/black_800" />
</shape>
</item>
</layer-list>

@ -1,36 +1,35 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="#ffffff" android:background="@color/black_800"
tools:context=".MainActivity"> tools:context=".MainActivity">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottomNav"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_above="@+id/bottomNav"
android:background="@color/grey_100"></View>
<com.google.android.material.bottomnavigation.BottomNavigationView <com.google.android.material.bottomnavigation.BottomNavigationView
android:background="@color/black_800" android:id="@+id/bottomNavigationView"
android:id="@+id/bottomNav" android:background="@drawable/navbar_background"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/nav_menu" app:menu="@menu/nav_menu"
app:itemIconTint="@drawable/botom_nav_colors" app:itemIconTint="@drawable/botom_nav_colors"
app:itemTextColor="@drawable/botom_nav_colors" app:itemTextColor="@drawable/botom_nav_colors"
android:scrollIndicators="left"/> />
</RelativeLayout>
<fragment
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="718dp"
app:defaultNavHost="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
app:navGraph="@navigation/my_nav"></fragment>
</androidx.constraintlayout.widget.ConstraintLayout>

@ -20,7 +20,7 @@
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<TextView <TextView
android:id="@+id/textView3" android:id="@+id/backbutton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical" android:layout_gravity="left|center_vertical"

@ -20,7 +20,8 @@
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<TextView <TextView
android:id="@+id/textView3" android:clickable="true"
android:id="@+id/backbutton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical" android:layout_gravity="left|center_vertical"

@ -182,13 +182,12 @@
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/collectbutton" android:id="@+id/buttonCollect"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_marginHorizontal="15dp" android:layout_marginHorizontal="15dp"
android:layout_height="60dp" android:layout_height="60dp"
android:clickable="true" android:clickable="true"
android:foreground="?android:attr/selectableItemBackground" android:foreground="?android:attr/selectableItemBackground"
android:layout_marginVertical="10dp" android:layout_marginVertical="10dp"
android:background="@drawable/rounded_border_button" android:background="@drawable/rounded_border_button"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
@ -239,7 +238,7 @@
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout4" android:id="@+id/buttonBoss"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_marginHorizontal="15dp" android:layout_marginHorizontal="15dp"
android:layout_height="60dp" android:layout_height="60dp"
@ -249,7 +248,7 @@
android:background="@drawable/rounded_border_button" android:background="@drawable/rounded_border_button"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/collectbutton"> app:layout_constraintTop_toBottomOf="@+id/buttonCollect">
<LinearLayout <LinearLayout
@ -294,5 +293,6 @@
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

@ -3,15 +3,15 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu 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">
<item <item
android:id="@+id/home" android:id="@+id/homeFragment"
android:icon="@drawable/ic_home" android:icon="@drawable/ic_home"
android:title="@string/home_title" /> android:title="@string/home_title" />
<item <item
android:id="@+id/inventory" android:id="@+id/inventoryFragment"
android:icon="@drawable/ic_inventory" android:icon="@drawable/ic_inventory"
android:title="@string/inventory_title" /> android:title="@string/inventory_title" />
<item <item
android:id="@+id/plus" android:id="@+id/plusFragment"
android:icon="@drawable/ic_plus" android:icon="@drawable/ic_plus"
android:title="@string/plus_title" /> android:title="@string/plus_title" />
</menu> </menu>

@ -3,18 +3,21 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_nav" android:id="@+id/my_nav"
app:startDestination="@id/mainActivity">
<activity app:startDestination="@id/homeFragment">
android:id="@+id/mainActivity"
android:name="com.example.shakecraft.MainActivity"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment <fragment
android:id="@+id/bossFragment" android:id="@+id/bossFragment"
android:name="com.example.shakecraft.BossFragment" android:name="com.example.shakecraft.BossFragment"
android:label="fragment_boss" android:label="fragment_boss"
tools:layout="@layout/fragment_boss" /> tools:layout="@layout/fragment_boss" >
<action
android:id="@+id/action_bossFragment_to_homeFragment"
app:destination="@id/homeFragment"
app:enterAnim="@android:anim/fade_in"
app:popUpTo="@id/homeFragment"
app:popUpToInclusive="true" />
</fragment>
<fragment <fragment
android:id="@+id/collectFragment" android:id="@+id/collectFragment"
android:name="com.example.shakecraft.CollectFragment" android:name="com.example.shakecraft.CollectFragment"
@ -22,27 +25,33 @@
tools:layout="@layout/fragment_collect" > tools:layout="@layout/fragment_collect" >
<action <action
android:id="@+id/action_collectFragment_to_homeFragment" android:id="@+id/action_collectFragment_to_homeFragment"
app:destination="@id/homeFragment" /> app:destination="@id/homeFragment"
app:enterAnim="@android:anim/fade_in"
app:popUpTo="@id/homeFragment"
app:popUpToInclusive="true" />
</fragment> </fragment>
<fragment
android:id="@+id/inventoryFragment"
android:name="com.example.shakecraft.InventoryFragment"
android:label="fragment_inventory"
tools:layout="@layout/fragment_inventory" />
<fragment <fragment
android:id="@+id/plusFragment" android:id="@+id/plusFragment"
android:name="com.example.shakecraft.PlusFragment" android:name="com.example.shakecraft.PlusFragment"
android:label="fragment_plus" android:label="fragment_plus"
tools:layout="@layout/fragment_plus" /> tools:layout="@layout/fragment_plus" />
<fragment
android:id="@+id/inventoryFragment"
android:name="com.example.shakecraft.InventoryFragment"
android:label="fragment_inventory"
tools:layout="@layout/fragment_inventory" />
<fragment <fragment
android:id="@+id/homeFragment" android:id="@+id/homeFragment"
android:name="com.example.shakecraft.HomeFragment" android:name="com.example.shakecraft.HomeFragment"
android:label="HomeFragment" > android:label="HomeFragment" >
<action <action
android:id="@+id/action_homeFragment_to_collectFragment" android:id="@+id/action_homeFragment_to_collectFragment"
app:destination="@id/collectFragment" /> app:destination="@id/collectFragment"
app:enterAnim="@android:anim/fade_in" />
<action <action
android:id="@+id/action_homeFragment_to_bossFragment" android:id="@+id/action_homeFragment_to_bossFragment"
app:destination="@id/bossFragment" /> app:destination="@id/bossFragment"
app:enterAnim="@android:anim/fade_in" />
</fragment> </fragment>
</navigation> </navigation>
Loading…
Cancel
Save