如何使Activity在片段中起作用?

时间:2019-07-29 21:02:35

标签: android xml android-fragments kotlin

当我从导航抽屉中选择chillzone片段时,我可以切换这些片段,但是当我尝试从BottomNavigationView中选择某些内容时,没有任何反应。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.MainActivity"
    tools:openDrawer="end">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

chillzone_fragment.xml

<?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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ui.chillzone.ChillZoneActivity"
    tools:openDrawer="start">

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="172dp"
        android:layout_marginLeft="172dp"
        android:layout_marginTop="356dp"
        android:text="@string/chillzone"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

</androidx.constraintlayout.widget.ConstraintLayout>

ChillZoneActivity

package com.rishabhdeepsingh.metaapp.ui.chillzone

import android.os.Bundle
import android.util.Log
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
import android.widget.Toast
import com.rishabhdeepsingh.metaapp.R

class ChillZoneActivity : AppCompatActivity() {

    private lateinit var bottomNavigationView: BottomNavigationView
    private lateinit var textMessage: TextView
    private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        println("~~~~~~~~~~~~~~~~~~~~~~~~")
        when (item.itemId) {
            R.id.navigation_home -> {
                textMessage.setText(R.string.title_home)
                Toast.makeText(this, "Home", Toast.LENGTH_LONG).show()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                textMessage.setText(R.string.title_dashboard)
                Toast.makeText(this, "Dashboard", Toast.LENGTH_LONG).show()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                textMessage.setText(R.string.title_notifications)
                Toast.makeText(this, "Notifications", Toast.LENGTH_LONG).show()
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bottomNavigationView = findViewById(R.id.bottom_nav_view)
        println("~~~~~~~~~~~~~~!!!!!!!!!!!!!!!!!!!!!~~~~~~~~~~")
        Toast.makeText(this, "Hello", Toast.LENGTH_LONG).show()
        textMessage = findViewById(R.id.message)
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
    }
}

ChillZoneFragment.kt

package com.rishabhdeepsingh.metaapp.ui.chillzone

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.rishabhdeepsingh.metaapp.R

class ChillZoneFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.chillzone_fragment, container, false)
    }
}

MainActivity.kt

package com.rishabhdeepsingh.metaapp.ui

import android.os.Bundle
import androidx.core.view.GravityCompat
import androidx.appcompat.app.ActionBarDrawerToggle
import android.view.MenuItem
import android.widget.Toast
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import com.rishabhdeepsingh.metaapp.R
import com.rishabhdeepsingh.metaapp.ui.chillzone.ChillZoneFragment


class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    private lateinit var drawerLayout: DrawerLayout

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val toolbar: Toolbar = findViewById(R.id.main_toolbar)
        setSupportActionBar(toolbar)

        val navView: NavigationView = findViewById(R.id.nav_view)
        navView.setNavigationItemSelectedListener(this)

        drawerLayout = findViewById(R.id.drawer_layout)
        val toggle = ActionBarDrawerToggle(
            this, drawerLayout, toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close
        )
        drawerLayout.addDrawerListener(toggle)
        toggle.syncState()

        if (savedInstanceState == null) {
            supportFragmentManager
                .beginTransaction()
                .replace(R.id.fragment_container, HomeFragment())
                .commit()
            navView.setCheckedItem(R.id.home)
        }
    }

    override fun onBackPressed() {
        if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
            drawerLayout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.
        when (item.itemId) {
            R.id.nav_home -> {
                // Handle the camera action
                supportFragmentManager
                    .beginTransaction()
                    .replace(R.id.fragment_container, HomeFragment())
                    .commit()
            }
            R.id.nav_chillzone -> {
                supportFragmentManager
                    .beginTransaction()
                    .replace(R.id.fragment_container, ChillZoneFragment())
                    .commit()
            }
            R.id.nav_slideshow -> {

            }
            R.id.nav_tools -> {

            }
            R.id.nav_share -> {
                Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show()
            }
            R.id.nav_send -> {

            }
        }
        drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }
}

我希望Toast消息应该与我选择的BottomNavigationView按钮相对应。

3 个答案:

答案 0 :(得分:1)

您的BottomNavigationView相关代码位于ChillZoneActivity中。它必须位于ChillZoneFragment中。

class ChillZoneFragment : Fragment() {

    private lateinit var bottomNavigationView: BottomNavigationView
    private lateinit var textMessage: TextView
    private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        println("~~~~~~~~~~~~~~~~~~~~~~~~")
        when (item.itemId) {
            R.id.navigation_home -> {
                textMessage.setText(R.string.title_home)
                Toast.makeText(this, "Home", Toast.LENGTH_LONG).show()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_dashboard -> {
                textMessage.setText(R.string.title_dashboard)
                Toast.makeText(this, "Dashboard", Toast.LENGTH_LONG).show()
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_notifications -> {
                textMessage.setText(R.string.title_notifications)
                Toast.makeText(this, "Notifications", Toast.LENGTH_LONG).show()
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.chillzone_fragment, container, false)
        bottomNavigationView = view.findViewById(R.id.bottom_nav_view)
        textMessage = view.findViewById(R.id.message)
        bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
        return view
    }
}

P.S-您无法在片段内进行活动“工作”。那不是它的工作原理。片段包含在活动中,绝不会相反。

  

Fragment代表FragmentActivity中的行为或用户界面的一部分。您可以在一个活动中组合多个片段以构建多窗格UI,并在多个活动中重用一个片段。您可以将片段视为活动的模块化部分,该片段具有自己的生命周期,接收其自己的输入事件,并且可以在活动运行时对其进行添加或删除(类似于“子活动”,您可以在不同的活动中重复使用。

     

片段必须始终托管在活动中,并且片段的生命周期直接受到宿主活动的生命周期的影响。

有关更多详细信息,请参见official documentation

答案 1 :(得分:0)

似乎未正确设置底部导航侦听器 尝试切换此:

bottomNavigationView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)

收件人:

bottomNavigationView.setOnNavigationItemSelectedListener(this)

答案 2 :(得分:0)

  

Android Fragment是活动的一部分,也称为子活动。一个活动中可以有多个片段。片段代表一项活动中的多个屏幕。

     

Android片段的生命周期受活动生命周期的影响,因为片段包含在活动中。

     

每个片段都有自己的生命周期方法,这些方法受活动生命周期的影响,因为片段嵌入到活动中。

     

FragmentManager类负责在片段对象之间进行交互。

您不能在片段内添加活动。

希望对您有帮助!