代码之家  ›  专栏  ›  技术社区  ›  Ali

工具栏单击侦听器未应用

  •  0
  • Ali  · 技术社区  · 7 年前

    在安装工具栏的片段中有以下代码:

    with(root) {
                with(activity as AppCompatActivity) {
                    setupActionBar(toolbar) {
                        setDisplayHomeAsUpEnabled(true)
                        setDisplayShowHomeEnabled(true)
                    }
                }
                toolbar.setNavigationOnClickListener { activity?.finish() }
            }
    

    下面是我的setupActionBar方法:

    fun AppCompatActivity.setupActionBar(toolbar : Toolbar, action: ActionBar.() -> Unit) {
        setSupportActionBar(toolbar)
        supportActionBar?.run {
            action()
        }
    }
    

    我的布局中的工具栏:

        <?xml version="1.0" encoding="utf-8"?>
    <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=".ui.detail.DetailFragment">
    
        <data>
            <variable
                    name="artist"
                    type="com.sample.android.lastfm.model.LastFmArtist"/>
        </data>
    
        <androidx.coordinatorlayout.widget.CoordinatorLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    
            <com.google.android.material.appbar.AppBarLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                <com.google.android.material.appbar.CollapsingToolbarLayout
                        android:layout_width="match_parent"
                        android:layout_height="300dp"
                        app:collapsedTitleTextAppearance="@style/CollapsedToolbar"
                        app:contentScrim="?attr/colorPrimary"
                        app:expandedTitleTextAppearance="@style/ExpandedToolbar"
                        app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                        app:title="@{artist.name}">
    
                    <ImageView
                            android:id="@+id/movie_poster"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:scaleType="centerCrop"
                            app:layout_collapseMode="parallax"
                            app:imageUrl="@{artist.images}"
                            tools:ignore="contentDescription"/>
    
                    <androidx.appcompat.widget.Toolbar
                            android:id="@+id/toolbar"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:minHeight="?attr/actionBarSize"
                            app:layout_collapseMode="pin"
                            app:theme="@style/Toolbar"/>
    
                    <com.google.android.material.tabs.TabLayout
                            android:id="@+id/tabLayout"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/custom_tab_layout_height"
                            app:tabGravity="fill"
                            app:tabIndicatorColor="@color/colorAccent"
                            app:tabIndicatorHeight="4dp"
                            app:tabMode="fixed"/>
    
                </com.google.android.material.appbar.CollapsingToolbarLayout>
    
            </com.google.android.material.appbar.AppBarLayout>
    
            <androidx.viewpager.widget.ViewPager
                    android:id="@+id/viewpager"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
    </layout>
    

    当我单击工具栏时,它不会将我导航回上一个屏幕。似乎它是不可点击的。

    2 回复  |  直到 7 年前
        1
  •  0
  •   End User    7 年前
    (activity as AppCompatActivity).setSupportActionBar(toolbar)
    (activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)
    

    然后在你的碎片里

    覆盖 onOptionsItemSelected

     override fun onOptionsItemSelected(item: MenuItem?): Boolean {
    
                when (item?.itemId) {
    
                    android.R.id.home -> {
                     activity?.onBackPressed()
                        }
    
            }
         return true
    }  
    
        2
  •  0
  •   Mohamed Mo'nes    7 年前

    将工具栏添加到xml片段 并在onCreateView中尝试此代码

    Toolbar toolbar = view.findViewById(R.id.toolbar); toolbar.setNavigationIcon(R.drawable.ic_back_button); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getActivity().onBackPressed(); } });

        3
  •  0
  •   Ali    7 年前

    工具栏不应为:

    <androidx.appcompat.widget.Toolbar
                            android:id="@+id/toolbar"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:minHeight="?attr/actionBarSize"
                            app:layout_collapseMode="pin"
                            app:theme="@style/Toolbar"/>
    

    相反,它应该是:

    <androidx.appcompat.widget.Toolbar
                            android:id="@+id/toolbar"
                            android:layout_width="match_parent"
                            android:layout_height="?attr/actionBarSize"
                            app:layout_collapseMode="pin"
                            app:theme="@style/Toolbar"/>