Toolbar custom view cannot clickable

2020-05-07 03:01发布

问题:

I need your help!!! I'm in progress to this post.

I have a layout_header

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/bg_shadow_blue">

    <com.andexert.library.RippleView
        android:id="@+id/rippleLeft"
        style="@style/ripple"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        app:rv_type="rectangle">

        <ImageView
            android:id="@+id/ivBack"
            style="@style/imageview_normal"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:src="@mipmap/ic_back"
            android:visibility="gone"/>
    </com.andexert.library.RippleView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/rippleRight"
        android:layout_toRightOf="@+id/rippleLeft">

        <TextView
            android:id="@+id/tvHeader"
            style="@style/text_header"
            android:layout_centerInParent="true"
            android:gravity="center"/>

        <ImageView
            android:id="@+id/ivHeader"
            style="@style/imageview_normal"
            android:layout_centerInParent="true"
            android:paddingBottom="5dp"
            android:src="@mipmap/ic_logo"
            android:visibility="gone"/>

    </RelativeLayout>

    <com.andexert.library.RippleView
        android:id="@+id/rippleRight"
        style="@style/ripple"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        app:rv_type="rectangle">

        <TextView
            android:id="@+id/tvMore"
            style="@style/imageview_normal"
            android:layout_height="match_parent"
            android:drawableLeft="@mipmap/ic_more"
            android:gravity="center_vertical"
            android:padding="10dp"
            android:textColor="@color/white"
            android:visibility="invisible"/>
    </com.andexert.library.RippleView>

</RelativeLayout>

then include this to main layout:

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

        <include layout="@layout/layout_header"/>
    </android.support.v7.widget.Toolbar>

Then in Main class, i have:

setContentView(R.layout.activity_main_1);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.slidingMenu);
        drawerView = (ScrollView) findViewById(R.id.sv_slidingMenu);
        mDrawerToggle = new BXHActionBarDrawerToggle(this, drawerLayout, 0, 0);

        // Set the drawer toggle as the DrawerListener
        drawerLayout.setDrawerListener(mDrawerToggle);

        //------Custom ActionBar
        ActionBar actionBar = getSupportActionBar();

        mIvHeader = (ImageView) findViewById(R.id.ivHeader);
        mTvMore = (TextView) findViewById(R.id.tvMore);
        mTvHeader = (TextView) findViewById(R.id.tvHeader);
        mIvBack = (ImageView) findViewById(R.id.ivBack);
        mIvBack.setVisibility(View.GONE);

        actionBar.setHomeAsUpIndicator(R.mipmap.ic_menu);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
mTvMore.setOnClickListener(this);

Some related code in Main class:

@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (mFragmentStack.size() > 1) {
                    onBackPressed();
                    return true;
                }
        }
        // Pass the event to ActionBarDrawerToggle, if it returns true, then it has handled the app icon touch event
        return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);
    }

My problem is that, the mTvMore doesn't receive onClickListener event. I have tried many ways as "toolbar.setNavigationOnClickListener(); or mDrawerToggle.setToolbarNavigationClickListener();" but it's not working. Please help me, Thanks

回答1:

make return statement into `switch (item.getItemId()) { case R.id.action_add: // EITHER CALL THE METHOD HERE OR DO THE FUNCTION DIRECTLY //your code return true;

    default:
        return super.onOptionsItemSelected(item);
    }`

hope this will work for you



回答2:

maybe you event was consumed by RippleView you used.Try to use you rippleRight to receive the onclick event?