Skip to content

Commit

Permalink
day 215
Browse files Browse the repository at this point in the history
  • Loading branch information
Gurrium committed Jan 20, 2022
1 parent e868a42 commit 4cd980b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
Expand Up @@ -23,6 +23,7 @@ import android.view.ViewGroup
import android.view.accessibility.AccessibilityNodeInfo
import android.widget.Button
import androidx.annotation.RequiresApi
import androidx.navigation.findNavController
import androidx.recyclerview.widget.RecyclerView


Expand Down Expand Up @@ -68,15 +69,8 @@ class LetterAdapter :

// Assigns a [OnClickListener] to the button contained in the [ViewHolder]
holder.button.setOnClickListener {
val context = holder.view.context
// Create an intent with a destination of DetailActivity
val intent = Intent(context, DetailActivity::class.java)
// Add the selected letter to the intent as extra data
// The text of Buttons are [CharSequence], a list of characters,
// so it must be explicitly converted into a [String].
intent.putExtra(WordListFragment.LETTER, holder.button.text.toString())
// Start an activity using the data and destination from the Intent.
context.startActivity(intent)
val action = LetterListFragmentDirections.actionLetterListFragmentToWordListFragment(letter = holder.button.text.toString())
holder.view.findNavController().navigate(action)
}
}

Expand Down
Expand Up @@ -20,6 +20,9 @@ import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.navigation.NavController
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -29,10 +32,19 @@ import com.example.wordsapp.databinding.ActivityMainBinding
* Main Activity and entry point for the app. Displays a RecyclerView of letters.
*/
class MainActivity : AppCompatActivity() {
private lateinit var navController: NavController

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
navController = navHostFragment.navController
setupActionBarWithNavController(navController)
}

override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp() || super.onSupportNavigateUp()
}2
}
Expand Up @@ -15,14 +15,16 @@
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
<androidx.fragment.app.FragmentContainerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="16dp" />
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph" />
</FrameLayout>
@@ -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_graph.xml"
app:startDestination="@id/letterListFragment">

<fragment
android:id="@+id/letterListFragment"
android:name="com.example.wordsapp.LetterListFragment"
android:label="fragment_letter_list"
tools:layout="@layout/fragment_letter_list" >
<action
android:id="@+id/action_letterListFragment_to_wordListFragment"
app:destination="@id/wordListFragment" />
</fragment>
<fragment
android:id="@+id/wordListFragment"
android:name="com.example.wordsapp.WordListFragment"
android:label="fragment_word_list"
tools:layout="@layout/fragment_word_list" >
<argument
android:name="letter"
app:argType="string" />
</fragment>
</navigation>

0 comments on commit 4cd980b

Please sign in to comment.