Issue#5 Shows basic example of testing NavController usage

This commit is contained in:
Gergely Hegedus 2022-09-23 13:53:05 +03:00
parent faf9cceb8e
commit 00e7a806eb
20 changed files with 383 additions and 2 deletions

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.fnives.test.showcase.examplecase.navcontroller">
<application>
<activity android:name=".NavControllerActivity" android:exported="true"/>
</application>
</manifest>

View file

@ -0,0 +1,17 @@
package org.fnives.test.showcase.examplecase.navcontroller
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.navArgs
class DetailFragment : Fragment(R.layout.fragment_detail) {
private val args by navArgs<DetailFragmentArgs>()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
(view as TextView).text = getString(R.string.home_item, args.position)
}
}

View file

@ -0,0 +1,44 @@
package org.fnives.test.showcase.examplecase.navcontroller
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class HomeFragment : Fragment(R.layout.fragment_home) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recycler = view.findViewById<RecyclerView>(R.id.recycler)
recycler.layoutManager = LinearLayoutManager(view.context)
recycler.adapter = Adapter(onClick = {
if (findNavController().currentDestination?.id != R.id.homeFragment) return@Adapter
findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToDetailFragment(it))
})
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
constructor(parent: ViewGroup) : this(LayoutInflater.from(parent.context).inflate(R.layout.item_home, parent, false))
}
class Adapter(
private val count: Int = 30,
private val onClick: (Int) -> Unit,
) : RecyclerView.Adapter<ViewHolder>() {
override fun getItemCount(): Int = count
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ViewHolder(parent)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val context = holder.itemView.context
(holder.itemView as Button).text = context.getString(R.string.home_item, position)
holder.itemView.setOnClickListener { onClick(position) }
}
}
}

View file

@ -0,0 +1,15 @@
package org.fnives.test.showcase.examplecase.navcontroller
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
// to see the actual screen, not just in test use:
// adb shell am start -n org.fnives.test.showcase/org.fnives.test.showcase.examplecase.navcontroller.NavControllerActivity
// after installing the apk
class NavControllerActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nav_controller)
}
}

View file

@ -0,0 +1,28 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:title="@string/activity_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:navGraph="@navigation/nav_example" />
</androidx.constraintlayout.widget.ConstraintLayout>

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="?attr/textAppearanceHeadline1"
tools:text="Item 5">
</TextView>

View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler"
android:layout_width="match_parent"
tools:listitem="@layout/item_home"
tools:itemCount="30"
android:layout_height="match_parent" />

View file

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/item_cta"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="48dp"
android:textAppearance="?attr/textAppearanceHeadline4"
tools:text="Item: 5" />

View file

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/nav_example.xml"
app:startDestination="@id/homeFragment">
<fragment
android:id="@+id/homeFragment"
android:name="org.fnives.test.showcase.examplecase.navcontroller.HomeFragment"
tools:layout="@layout/fragment_home"
android:label="HomeFragment" >
<action
android:id="@+id/action_homeFragment_to_detailFragment"
app:destination="@id/detailFragment" />
</fragment>
<fragment
android:id="@+id/detailFragment"
android:name="org.fnives.test.showcase.examplecase.navcontroller.DetailFragment"
tools:layout="@layout/fragment_detail"
android:label="DetailFragment" >
<argument
android:name="position"
app:argType="integer" />
</fragment>
</navigation>

View file

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="home_item">Item %1$d</string>
<string name="activity_title">Nav Controller Example</string>
</resources>