Jelajahi Sumber

app渐进式启动页面初始化

everywindchase 1 tahun lalu
induk
melakukan
a7d07cf487

+ 1 - 0
app/build.gradle

@@ -80,6 +80,7 @@ dependencies {
     implementation libs.xui
     implementation libs.recyclerview
     implementation  libs.glide
+    implementation libs.androidx.navigation.ui.ktx
     testImplementation 'junit:junit:4.13.2'
     androidTestImplementation 'androidx.test.ext:junit:1.1.5'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

+ 2 - 2
app/src/main/AndroidManifest.xml

@@ -14,7 +14,7 @@
             android:theme="@style/Theme.Escortapp"
             tools:targetApi="31">
         <activity
-                android:name=".activity.LaunchActivity"
+                android:name=".ui.activity.LaunchActivity"
                 android:exported="true"
                 android:configChanges="screenSize|keyboardHidden|orientation|keyboard"
                 android:windowSoftInputMode="adjustPan|stateHidden"
@@ -31,7 +31,7 @@
 
 
         <activity
-                android:name=".activity.MainActivity"
+                android:name=".ui.activity.MainActivity"
                 android:configChanges="screenSize|keyboardHidden|orientation|keyboard|locale|layoutDirection"
                 android:launchMode="singleTask"
                 android:windowSoftInputMode="adjustPan|stateHidden"

+ 0 - 12
app/src/main/java/jx/cn/escort/activity/MainActivity.kt

@@ -1,12 +0,0 @@
-package jx.cn.escort.activity
-
-import androidx.appcompat.app.AppCompatActivity
-import android.os.Bundle
-import jx.cn.escort.R
-
-class MainActivity : AppCompatActivity() {
-    override fun onCreate(savedInstanceState: Bundle?) {
-        super.onCreate(savedInstanceState)
-        setContentView(R.layout.activity_main)
-    }
-}

+ 2 - 2
app/src/main/java/jx/cn/escort/activity/LaunchActivity.kt → app/src/main/java/jx/cn/escort/ui/activity/LaunchActivity.kt

@@ -1,4 +1,4 @@
-package jx.cn.escort.activity
+package jx.cn.escort.ui.activity
 
 
 
@@ -21,7 +21,7 @@ class LaunchActivity: BaseSplashActivity()  {
     }
 
     override fun onSplashFinished() {
-        ActivityUtils.startActivity(this,MainActivity::class.java)
+        ActivityUtils.startActivity(this, MainActivity::class.java)
         finish()
     }
 

+ 75 - 0
app/src/main/java/jx/cn/escort/ui/activity/MainActivity.kt

@@ -0,0 +1,75 @@
+package jx.cn.escort.ui.activity
+
+import android.content.Context
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.MenuItem
+import android.view.View
+import android.view.ViewGroup
+import android.widget.Toast
+import androidx.appcompat.app.AppCompatActivity
+import androidx.constraintlayout.widget.ConstraintLayout
+import androidx.fragment.app.Fragment
+import androidx.fragment.app.FragmentTransaction
+import androidx.navigation.Navigation
+import com.google.android.material.navigation.NavigationView
+import com.xuexiang.xui.widget.actionbar.TitleBar
+import jx.cn.escort.R
+
+class MainActivity : AppCompatActivity() {
+    override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+        setContentView(R.layout.activity_main)
+
+
+        var layout= findViewById<View>(R.id.top_bar)
+        var topBarView=(layout as ViewGroup).getChildAt(0)  as  TitleBar
+        topBarView.setLeftClickListener(object:View.OnClickListener {
+
+            override fun onClick(v: View?) {
+                Toast.makeText(baseContext,"测试",Toast.LENGTH_SHORT).show()
+            }
+        })
+
+
+       var layoutInflater=baseContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
+        var bottomNav= findViewById<NavigationView>(R.id.bottom_nav)
+        var navContent=findViewById<ViewGroup>(R.id.nav_content)
+        bottomNav.setNavigationItemSelectedListener(object:NavigationView.OnNavigationItemSelectedListener{
+            override fun onNavigationItemSelected(item: MenuItem): Boolean {
+               var id=item.itemId
+                when(id){
+                   R.id.index-> {
+                        val  fragment=  layoutInflater.inflate(R.layout.fragment_index,navContent)   as  Fragment
+                       switchFragment(fragment)
+                   }
+                    R.id.tasks->{
+                        val  fragment=  layoutInflater.inflate(R.layout.fragment_tasks,navContent)   as  Fragment
+                        switchFragment(fragment)
+                    }
+                    R.id.mine->{
+                        val  fragment=  layoutInflater.inflate(R.layout.fragment_mine,navContent)   as  Fragment
+                        switchFragment(fragment)
+                    }
+                }
+
+                return  true
+            }
+
+
+        })
+
+
+    }
+
+
+
+   private fun switchFragment(fragment:Fragment){
+        var fm=supportFragmentManager
+        var  transaction: FragmentTransaction =fm.beginTransaction()
+        transaction.replace(R.id.nav_content,fragment)
+         transaction.commitAllowingStateLoss()
+    }
+
+
+}

+ 20 - 0
app/src/main/java/jx/cn/escort/ui/fragment/BaseFragment.kt

@@ -0,0 +1,20 @@
+package jx.cn.escort.ui.fragment
+
+
+import android.annotation.SuppressLint
+import android.os.Bundle
+import android.view.LayoutInflater
+import android.view.View
+import android.view.ViewGroup
+import androidx.fragment.app.Fragment
+import jx.cn.escort.R
+
+class BaseFragment:Fragment() {
+
+
+    @SuppressLint("ResourceType")
+    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
+        return   inflater.inflate(R.id.fragment_nav_index,container)
+
+    }
+}

TEMPAT SAMPAH
app/src/main/res/drawable-hdpi/icon_back_black.png


+ 2 - 9
app/src/main/res/layout/activity_launch.xml

@@ -5,14 +5,7 @@
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        tools:context=".activity.LaunchActivity">
-    <TextView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:text="Hello World!"
-            app:layout_constraintBottom_toBottomOf="parent"
-            app:layout_constraintStart_toStartOf="parent"
-            app:layout_constraintEnd_toEndOf="parent"
-            app:layout_constraintTop_toTopOf="parent"/>
+        tools:context=".ui.activity.LaunchActivity">
+
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 11 - 12
app/src/main/res/layout/activity_main.xml

@@ -5,19 +5,18 @@
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
-        tools:context=".activity.MainActivity">
+        android:background="@color/xui_config_color_gray_9"
+        tools:context=".ui.activity.MainActivity">
 
 
-    <include
-        layout="@layout/layout_top_bar"
-    />
-    <TextView
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:text="Hello World!"
-            app:layout_constraintBottom_toBottomOf="parent"
-            app:layout_constraintStart_toStartOf="parent"
-            app:layout_constraintEnd_toEndOf="parent"
-            app:layout_constraintTop_toTopOf="parent"/>
+    <include android:id="@+id/top_bar"
+             layout="@layout/layout_top_bar"
+        android:visibility="visible"/>
+
+
+    <include android:id="@+id/nav_bar"
+             layout="@layout/layout_nav_bar"
+             android:visibility="visible" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="0dp"/>
+
 
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 18 - 0
app/src/main/res/layout/fragment_index.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<com.xuexiang.xui.widget.alpha.XUIAlphaLinearLayout
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/fragment_nav_index"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+>
+
+
+
+    <androidx.appcompat.widget.AppCompatTextView
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:text="测试1"
+            android:textSize="16sp"
+    />
+
+</com.xuexiang.xui.widget.alpha.XUIAlphaLinearLayout>

+ 18 - 0
app/src/main/res/layout/fragment_mine.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<com.xuexiang.xui.widget.alpha.XUIAlphaLinearLayout
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/fragment_nav_index"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+>
+
+    <androidx.appcompat.widget.AppCompatTextView
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:text="测试2"
+            android:textSize="16sp"
+    />
+
+
+
+</com.xuexiang.xui.widget.alpha.XUIAlphaLinearLayout>

+ 18 - 0
app/src/main/res/layout/fragment_tasks.xml

@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="utf-8"?>
+<com.xuexiang.xui.widget.alpha.XUIAlphaLinearLayout
+        xmlns:android="http://schemas.android.com/apk/res/android"
+        android:id="@+id/fragment_nav_tasks"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+>
+    <androidx.appcompat.widget.AppCompatTextView
+            android:layout_width="match_parent"
+            android:layout_height="match_parent"
+            android:text="测试3"
+            android:textSize="16sp"
+             />
+
+
+
+
+</com.xuexiang.xui.widget.alpha.XUIAlphaLinearLayout>

+ 39 - 0
app/src/main/res/layout/layout_nav_bar.xml

@@ -0,0 +1,39 @@
+<?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"
+        xmlns:tools="http://schemas.android.com/tools"
+        android:layout_width="match_parent"
+        android:layout_height="match_parent"
+       >
+
+
+    <FrameLayout
+            android:id="@+id/top_nav"
+            android:layout_width="match_parent"
+            android:layout_height="62dp"
+            app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintHorizontal_bias="0.0"
+            app:layout_constraintStart_toStartOf="parent"
+            app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0"/>
+    <androidx.fragment.app.FragmentContainerView
+            android:id="@+id/nav_content"
+            android:layout_width="match_parent"
+            android:layout_height="0dp"
+            app:layout_constraintBottom_toTopOf="@+id/bottom_nav"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintHorizontal_bias="0.0"
+            app:layout_constraintStart_toStartOf="parent"
+            app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0"/>
+    <com.google.android.material.bottomnavigation.BottomNavigationView
+            android:id="@+id/bottom_nav"
+            android:layout_width="match_parent"
+            android:layout_height="75dp"
+            app:layout_constraintBottom_toBottomOf="parent"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintHorizontal_bias="0.5"
+            app:layout_constraintStart_toStartOf="parent"
+            app:menu="@menu/layout_nav_menu"/>
+
+</androidx.constraintlayout.widget.ConstraintLayout>

+ 15 - 0
app/src/main/res/menu/layout_nav_menu.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="utf-8"?>
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <item
+            android:id="@+id/index"
+            android:title="首页"
+           />
+    <item
+            android:id="@+id/tasks"
+            android:title="任务"
+            />
+    <item
+            android:id="@+id/mine"
+            android:title="我的"
+            />
+</menu>

+ 6 - 1
gradle/libs.versions.toml

@@ -1,12 +1,17 @@
 [versions]
+kotlin = "1.9.23"
+
 
 
 
 [libraries]
+androidx-navigation-ui-ktx = "androidx.navigation:navigation-ui-ktx:2.6.0"
 core-ktx = "androidx.core:core-ktx:1.13.0"
 appcompat = "androidx.appcompat:appcompat:1.6.1"
 material="com.google.android.material:material:1.12.0"
 constraintlayout="androidx.constraintlayout:constraintlayout:2.1.4"
 recyclerview="androidx.recyclerview:recyclerview:1.3.0"
 xui="com.github.xuexiangjys:XUI:1.2.1"
-glide= 'com.github.bumptech.glide:glide:4.16.0'
+glide= 'com.github.bumptech.glide:glide:4.16.0'
+[plugins]
+kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }