Android上的OnOptionsItemSelected无法正常使用

时间:2018-07-26 07:54:33

标签: android android-toolbar android-collapsingtoolbarlayout menu-items

这是问题所在,我正在使用从此链接https://github.com/saulmm/CoordinatorBehaviorExample获得的CollapsingToolbarBehavior。但是当我在折叠时单击工具栏上的图标时,就像一个无效链接。甚至没有调用OnOptionsItemSelected,是因为与其他布局堆叠还是什么?

这是主要活动中的代码:

    mToolbar = (Toolbar) findViewById(R.id.main_toolbar);
    mToolbar.inflateMenu(R.menu.edit);
    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.edit, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int id = item.getItemId();
            Log.d("in","hahhaah");
            if(id == R.id.btn_back_edit){
                onBackPressed();
                return true;
            }

            if (id == R.id.btn_edit) {
                invalidateOptionsMenu();
                Intent intent = new Intent(SettingProfileActivity.this, SettingProfileActivityOn.class);
                startActivity(intent);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        public void onBackPressed() {
            super.onBackPressed();
            Intent intent = new Intent(SettingProfileActivity.this,MainActivity.class);
            intent.putExtra("LoadFragment", "settings");
            startActivityForResult(intent,0);
            finish();
        }

这是布局代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    tools:ignore="RtlHardcoded">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <ImageView
                android:id="@+id/main.imageview.placeholder"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:scaleType="centerCrop"
                android:background="@color/colorPrimary"
                android:tint="#11000000"
                app:layout_collapseMode="parallax" />

            <FrameLayout
                android:id="@+id/main.framelayout.title"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_gravity="bottom|center_horizontal"
                android:background="@color/colorPrimary"
                android:orientation="vertical"
                app:layout_collapseMode="parallax">

                <LinearLayout
                    android:id="@+id/main.linearlayout.title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/sebelum.anim"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:gravity="bottom|center"
                        android:fontFamily="@font/raleway_medium"
                        android:layout_marginTop="30dp"
                        android:text="Robert"
                        android:textColor="@android:color/white"
                        android:textSize="24sp" />

                    <TextView
                        android:id="@+id/jenis_usaha"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="@font/raleway_medium"
                        android:layout_gravity="center_horizontal"
                        android:gravity="bottom|center"
                        android:layout_marginTop="6dp"
                        android:layout_marginBottom="5dp"
                        android:text="Robert"
                        android:textColor="@android:color/white"
                        android:textSize="20sp" />

                </LinearLayout>
            </FrameLayout>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.constraint.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/cl"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="0dp">

            <include
                android:id="@+id/tool_bar"
                layout="@layout/custom_toolbar">

            </include>

            <View
                android:id="@+id/view2"
                android:layout_width="0dp"
                android:layout_height="52dp"
                android:background="@color/placeholder_bg"
                app:layout_constraintBottom_toTopOf="@+id/text_nama_profile"
                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"
                app:layout_constraintVertical_chainStyle="packed" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="16dp"
                android:layout_marginEnd="24dp"
                android:layout_marginLeft="24dp"
                android:layout_marginRight="24dp"
                android:layout_marginStart="24dp"
                android:layout_marginTop="16dp"
                android:fontFamily="@font/raleway_semibold"
                android:text="PERSONAL DETAILS"
                android:textAlignment="center"
                android:textAppearance="@android:style/TextAppearance.Medium"
                android:textColor="#6b6c6d"
                app:layout_constraintBottom_toBottomOf="@+id/view2"
                app:layout_constraintEnd_toEndOf="@+id/view2"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="@+id/view2"
                app:layout_constraintTop_toTopOf="@+id/view2"
                app:layout_constraintVertical_bias="0.0" />

            <TextView
                android:id="@+id/text_nama_profile"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:fontFamily="@font/raleway_medium"
                android:text="Robert"
                android:textAppearance="@android:style/TextAppearance.Medium"
                app:layout_constraintBottom_toTopOf="@+id/text_jk_profile"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/view2" />
<View
                android:id="@+id/batas6"
                android:layout_width="0dp"
                android:layout_height="1.75dp"
                android:layout_marginTop="9dp"
                android:background="@color/placeholder_bg"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toBottomOf="@id/text_alamat_profile"
                app:layout_constraintVertical_bias="0.0" />

        </android.support.constraint.ConstraintLayout>
    </android.support.v4.widget.NestedScrollView>

    <android.support.v7.widget.Toolbar
        android:id="@+id/main.toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:layout_anchor="@id/main.framelayout.title"
        app:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:title="">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Space
                android:layout_width="@dimen/image_final_width"
                android:layout_height="@dimen/image_final_width" />

            <TextView
                android:id="@+id/main.textview.title"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="8dp"
                android:gravity="center_vertical"
                android:text="Robert"
                android:textColor="@android:color/white"
                android:textSize="20sp" />

        </LinearLayout>
    </android.support.v7.widget.Toolbar>

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="@dimen/image_width"
        android:layout_height="@dimen/image_width"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/person"
        app:finalHeight="@dimen/image_final_width"
        app:finalYPosition="2dp"
        app:civ_border_color="@android:color/white"
        app:civ_border_width="2dp"
        app:layout_behavior=".AvatarImageBehavior"
        app:startHeight="2dp"
        app:startToolbarPosition="2dp"
        app:startXPosition="2dp"
        />

</android.support.design.widget.CoordinatorLayout>

这是菜单文件中的代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/btn_back_edit"
        android:title=""
        app:showAsAction="always"
        android:icon="@drawable/ic_back"
        />
    <item
        android:id="@+id/btn_edit"
        android:title=""
        app:showAsAction="always"
        android:icon="@drawable/ic_edit"
        />
</menu>

这是custom_toolbar布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/toolbar">

</android.support.v7.widget.Toolbar>

1 个答案:

答案 0 :(得分:1)

这里的问题与您使用工具栏的inflateMenu方法而不使用AppCompactActivity的setSupportActionBar有关。

有多种解决方法,一种可能是使用工具栏的setOnMenuItemClickListener