Android Kotlin Значок панели навигации застрял как значок кнопки «Назад» - PullRequest
0 голосов
/ 27 мая 2020

У меня есть DashboardFragment в приложении, которое я создал (это приложение имеет график навигации):

class DashboardFragment : Fragment() {

    lateinit var binding : FragmentDashboardBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        // Binding object for this fragment and the layout
        binding = DataBindingUtil.inflate(inflater,
            R.layout.fragment_dashboard, container, false)

        //Navigate to Product stock fragment when clicked
        binding.productStockButton.setOnClickListener(Navigation.createNavigateOnClickListener(
            R.id.action_dashboardFragment_to_productStockOutletList
        ))

        //Navigate to Switching History fragment when clicked
        binding.switchingHistoryButton.setOnClickListener(Navigation.createNavigateOnClickListener(
            R.id.action_dashboardFragment_to_switchingHistoryFragment
        ))

        //Navigate to Outlet List fragment for Outstanding Product when clicked
        binding.outstandingOrderButton.setOnClickListener(Navigation.createNavigateOnClickListener(
            R.id.action_dashboardFragment_to_outletListFragment
        ))


        // Set action bar title to "Main Dashboard"
        (activity as AppCompatActivity).supportActionBar?.title = "Main Dashboard"

        // Declare that this fragment has menu
        setHasOptionsMenu(true)

        //Show the previously hidden Action Bar
        (activity as AppCompatActivity).supportActionBar?.show()

        //Return.... i don't know.
        return binding.root
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        super.onCreateOptionsMenu(menu, inflater)
        inflater.inflate(R.menu.nav_overflow_menu, menu)
    }
}

Это мой график навигации

<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/navigation"
app:startDestination="@id/loginFragment">

<fragment
    android:id="@+id/loginFragment"
    android:name="com.example.switchingandroidappproject.mainFragments.LoginFragment"
    android:label="LoginFragment" >
    <action
        android:id="@+id/action_loginFragment_to_dashboardFragment"
        app:destination="@id/dashboardFragment" />
</fragment>

<fragment
    android:id="@+id/dashboardFragment"
    android:name="com.example.switchingandroidappproject.mainFragments.DashboardFragment"
    android:label="fragment_dashboard"
    tools:layout="@layout/fragment_dashboard" >

    ...

enter image description here

, чтобы показать мой ящик Nav во фрагменте панели инструментов, я программно устанавливаю DashboardFragment в качестве пункта назначения после перехода от loginFragment к фрагменту панели инструментов.

Это моя текущая MainActivity

   class MainActivity : AppCompatActivity() {

    lateinit var drawerLayout: DrawerLayout

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

        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
        drawerLayout = binding.drawerLayout

        //Get navigation controller of this App
        val navController = this.findNavController(R.id.myNavHostFragment)

        //Lock the Nav drawer
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)

        NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
        NavigationUI.setupWithNavController(binding.navView, navController)

    }


    // Set up the back button on action bar
    override fun onSupportNavigateUp(): Boolean {
        val navController = this.findNavController(R.id.myNavHostFragment)

        return NavigationUI.navigateUp(navController, drawerLayout)
    }
}

Это мой LoginFragment

class LoginFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding:FragmentLoginBinding = DataBindingUtil.inflate(inflater,
            R.layout.fragment_login, container, false)

        // Hide the Action bar
        (activity as AppCompatActivity).supportActionBar?.hide()

        binding.loginButton.setOnClickListener {
           //Some unimportant validation
        }


        return binding.root
    }
}

Это - текущий вид приборной панели И Так он выглядит, когда открывается ящик навигации

Единственная проблема здесь - Навигационный ящик lo go. Находясь в DashboardFragment, он функционировал как обычный ящик навигации lo go, за исключением того, что его заменяла кнопка возврата lo go.

И это для приборной панели XML

<layout 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"
tools:context="com.example.android.navigation.DashboardFragment">

<LinearLayout
    android:paddingLeft="12dp"
    android:paddingRight="12dp"
    android:id="@+id/myNavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginRight="16dp">

        <TextView
            android:id="@+id/dashboardTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Welcome, User123"
            android:textColor="#6f6f6f"
            android:textSize="22sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/dashboardTitle"
            android:layout_marginTop="6sp"
            android:text="3 Items"
            android:textColor="#6f6f6f"
            android:textSize="14sp" />

    </RelativeLayout>


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp">


      ....

Этот вопрос основан на моем предыдущем вопросе о немного другом вопросе

Если что-то неясно, дайте мне знать!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...