Use navigation
This commit is contained in:
parent
f6836b4996
commit
7898784995
6 changed files with 42 additions and 16 deletions
|
@ -1,6 +1,7 @@
|
|||
plugins {
|
||||
id 'com.android.application'
|
||||
id 'kotlin-android'
|
||||
id 'androidx.navigation.safeargs'
|
||||
}
|
||||
|
||||
android {
|
||||
|
@ -34,6 +35,8 @@ android {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
|
||||
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
|
||||
implementation 'androidx.appcompat:appcompat:1.3.1'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
|
||||
implementation 'androidx.core:core-ktx:1.6.0'
|
||||
|
|
|
@ -2,22 +2,14 @@ package com.causa_arcana
|
|||
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import androidx.navigation.findNavController
|
||||
|
||||
class AppActivity : AppCompatActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_app)
|
||||
|
||||
val fragmentTransaction = supportFragmentManager.beginTransaction()
|
||||
fragmentTransaction.replace(R.id.activity_main__frame, MainFragment() {
|
||||
onArticlesListItemClick()
|
||||
})
|
||||
fragmentTransaction.commit()
|
||||
}
|
||||
|
||||
fun onArticlesListItemClick() {
|
||||
val fragmentTransaction = supportFragmentManager.beginTransaction()
|
||||
fragmentTransaction.replace(R.id.activity_main__frame, ArticleFragment())
|
||||
fragmentTransaction.commit()
|
||||
}
|
||||
override fun onSupportNavigateUp(): Boolean =
|
||||
findNavController(R.id.activity_app__nav).navigateUp()
|
||||
}
|
||||
|
|
|
@ -5,10 +5,11 @@ import androidx.fragment.app.Fragment
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
class MainFragment(private val onArticlesListItemClick: () -> Unit) : Fragment() {
|
||||
class MainFragment : Fragment() {
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater,
|
||||
container: ViewGroup?,
|
||||
|
@ -24,7 +25,11 @@ class MainFragment(private val onArticlesListItemClick: () -> Unit) : Fragment()
|
|||
view.findViewById(R.id.fragment_main__articles_list_rv)
|
||||
articlesListRecyclerView.layoutManager = LinearLayoutManager(requireContext())
|
||||
articlesListRecyclerView.adapter = ArticlesListRecyclerViewAdapter {
|
||||
onArticlesListItemClick()
|
||||
onArticleCardsListItemClick()
|
||||
}
|
||||
}
|
||||
|
||||
private fun onArticleCardsListItemClick() {
|
||||
findNavController().navigate(R.id.action_open_article)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,9 +7,12 @@
|
|||
android:layout_height="match_parent"
|
||||
tools:context=".AppActivity">
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/activity_main__frame"
|
||||
<fragment
|
||||
android:id="@+id/activity_app__nav"
|
||||
android:name="androidx.navigation.fragment.NavHostFragment"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
android:layout_height="match_parent"
|
||||
app:navGraph="@navigation/nav_graph"
|
||||
app:defaultNavHost="true"/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
|
22
app/src/main/res/navigation/nav_graph.xml
Normal file
22
app/src/main/res/navigation/nav_graph.xml
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?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/mainFragment">
|
||||
|
||||
<fragment
|
||||
android:id="@+id/mainFragment"
|
||||
android:name="com.causa_arcana.MainFragment"
|
||||
android:label="fragment_main"
|
||||
tools:layout="@layout/fragment_main">
|
||||
<action
|
||||
android:id="@+id/action_open_article"
|
||||
app:destination="@id/articleFragment"/>
|
||||
</fragment>
|
||||
<fragment
|
||||
android:id="@+id/articleFragment"
|
||||
android:name="com.causa_arcana.ArticleFragment"
|
||||
android:label="fragment_article"
|
||||
tools:layout="@layout/fragment_article"/>
|
||||
</navigation>
|
|
@ -6,6 +6,7 @@ buildscript {
|
|||
}
|
||||
|
||||
dependencies {
|
||||
classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5'
|
||||
classpath "com.android.tools.build:gradle:7.0.0"
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21"
|
||||
|
||||
|
|
Reference in a new issue