如何处理后退箭头点击?

时间:2017-02-21 15:58:04

标签: android android-toolbar

我正在开发一款关于音乐的Android应用。在这个应用程序中,我有两个片段:PopFragment e GenresFragment 在名为fragment_pop.xml的PopFragment的XML文件中,我有一个带有后退箭头的工具栏,可以追溯到GenresFragment。
我的工具栏代码是:

import { Component, forwardRef } from '@angular/core';
import { NG_VALUE_ACCESSOR, ControlValueAccessor } from '@angular/forms';

@Component({
    selector: 'custom-input',
    template: `<div (click)="update()">Click</div>`,
    providers: [{
        provide: NG_VALUE_ACCESSOR,
        useExisting: forwardRef(() => CustomInputComponent),
        multi: true
    }]
})
export class CustomInputComponent implements ControlValueAccessor {

    private onTouchedCallback: () => void = () => {};
    private onChangeCallback: (_: any) => void = () => {};

    update(){
      this.value++;
    }

    get value(): any {
        return this.innerValue;
    };

    set value(v: any) {
        console.log("Change to");
        if (v !== this.innerValue) {
            this.innerValue = v;
            this.onChangeCallback(v);
        }
    }

    writeValue(value: any) {
        if (value !== this.innerValue) {
            this.innerValue = value;
        }
    }

    registerOnChange(fn: any) {
        this.onChangeCallback = fn;
    }

    registerOnTouched(fn: any) {
        this.onTouchedCallback = fn;
    }
}

在名为PopActivity的PopFragment Java文件中,我有一些代码,但它不起作用。
我有这段代码:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    >
<include
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    layout="@layout/toolbar_layout"
    />

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

<ImageButton
    android:id="@+id/arrowPop"
    android:layout_width="54dp"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_arrow_back_black_24dp"
    style="?android:attr/borderlessButtonStyle"
    />
你能帮帮我吗? 谢谢

3 个答案:

答案 0 :(得分:0)

尝试类似:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    inflater.inflate(R.menu.your_menu, menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home: {
            getActivity().onBackPressed();
            break;
        }
    }
    return true;
}

答案 1 :(得分:0)

您通常不需要添加自己的箭头图标,工具栏应该能够为您处理。

if(shouldShowArrow()) {
   drawerLayout.setDrawerLockMode(LOCK_MODE_LOCKED_CLOSED, GravityCompat.START);
   drawerToggle.setDrawerIndicatorEnabled(false);
   MyActivity.this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else { // hamburglar icon
   drawerLayout.setDrawerLockMode(LOCK_MODE_UNLOCKED, GravityCompat.START);
   MyActivity.this.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
   drawerToggle.setDrawerIndicatorEnabled(true);
}
drawerToggle.syncState();

然后您可以定义单击箭头时发生的情况

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    ButterKnife.bind(this);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    drawerToggle = new ActionBarDrawerToggle(MyActivity.this, drawerLayout, toolbar, R.string.open, R.string.close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            MyActivity.this.supportInvalidateOptionsMenu();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            MyActivity.this.supportInvalidateOptionsMenu();
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);

    drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // what to do when you click the arrow
        }
    });
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setHomeButtonEnabled(true);
}

@Override
protected void onPostCreate(Bundle bundle) {
    super.onPostCreate(bundle);
    drawerToggle.syncState();
}

@Override
protected void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

答案 2 :(得分:0)

使用以下代码作为快速修复,模仿正在按下的后退按钮。

switch (view.getId()) {
    case R.id.arrowPop:
        onBackPressed();
        break;
}

使用此代码,您不必自己定义工具栏中的后退按钮,Android会处理它。

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

然后当您点击后退按钮时,Android会为您拨打onBackPressed()