如何在片段及其单击侦听器中添加后退箭头按钮

时间:2018-12-04 16:01:50

标签: android android-actionbar fragment toolbar drawer

我有一个活动和片段。在此活动中,我具有工具栏和抽屉。所以我想在片段中添加后退箭头,并为此添加点击监听器。一切都很好,在片段中我有后退箭头按钮,但是当我单击它时,将打开抽屉。那么如何解决它。也许我的代码做错了什么?谢谢大家的帮助。

这是我的活动代码。 MainActivity.class

private Toolbar toolbar;
private DrawerLayout drawer;
private ActionBarDrawerToggle toggle;
private View view;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    view = findViewById(R.id.mainView);
    button = findViewById(R.id.about_us);

    initToolbar();
    drawerInit();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        default:
            return false;
    }
}

@Override
public void replaceFragment(BaseFragment fragment, Boolean isAddToBackStack) {
    String backStateName = fragment.getFragmentName();
    FragmentManager manager = getSupportFragmentManager();

    FragmentTransaction transaction = manager.beginTransaction();
    transaction.replace(R.id.main_fragment_container, fragment, backStateName);
    if (isAddToBackStack) {
        transaction.addToBackStack(backStateName);
    }

    transaction.commit();
}


public void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(toolbar);
}

public void drawerInit() {
    drawer = (DrawerLayout) findViewById(R.id.drawer);

    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);
            float moveFactor = (drawerView.getWidth() * slideOffset);
            view.setTranslationX(moveFactor);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }
    };

    drawer.addDrawerListener(toggle);
    toggle.setDrawerIndicatorEnabled(true);
    toggle.syncState();
}

//fragments back press part
@Override
public void onBackPressed() {
    super.onBackPressed();
}


@Override
protected void onDestroy() {
    super.onDestroy();
}

所以我初始化了工具栏和抽屉。 在这里我初始化我的片段

public class AboutUs extends BaseFragment {

View mainView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.about_us, container, false);

    ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
    ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("About us");

    return mainView;
}
}

因此,我尝试覆盖此片段中的onOptionsItemSelected,但这无济于事。该图标将变为片段中的后退箭头,但是在单击时将打开抽屉。任何想法如何解决这个问题?我尝试了其他类似问题的几乎所有答案,但没有帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

在您的活动中,更改this.finish()的选项菜单侦听器中的this.onBackPressed(),这将调用默认的getFragmentManager().popStackBack(),删除较旧的片段。