如何使用视图寻呼机来处理后退按钮? [英] How to handle back button using view pager?

查看:96
本文介绍了如何使用视图寻呼机来处理后退按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个片段破片A和其使用视图渲染寻呼机破片B点。
如果用户已经从刷卡到B那么presses后退按钮(B中时),那么用户应该去,而不是出来查看传呼机的。我们怎样才能做到这一点?

I am having two fragments Frag A and Frag B which are rendered using view pager . If user has swiped from A to B then presses back button(when in B) then user should go to A instead of coming out of view pager . How can we achieve this ?

添加事务backstack似乎并没有帮助。

Adding transaction to backstack does not seem to help .

感谢

推荐答案

您必须覆盖在活动的的onkeydown()

You have to override the onKeyDown() method in the Activity.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        myViewPager.setCurrentItem(0, true);
        return true;
    } else {
       return super.onKeyDown(keyCode, event);
    }
}

这将捕获后退按钮preSS事件和用户发送到在ViewPager的第一个项目。

This will capture the "back" button press event and send the user to the first item in the ViewPager.

有也是<一个href=\"http://developer.android.com/reference/android/support/v4/view/ViewPager.html#getCurrentItem%28%29\">ViewPager#getCurrentItem()可用于返回仅当用户刷卡。像这样的实现:

There is also ViewPager#getCurrentItem() which can be used to go back only if the user swiped. An implementation like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && myViewPager.getCurrentItem == 1) {
        myViewPager.setCurrentItem(0, true);
        return true;
    } else {
       return super.onKeyDown(keyCode, event);
    }
}

将用户发送回只有当用户是下一个页面上的previous页面。否则,它通常将注册的返回,用户将退出预期。

would send the user back to the previous page only if the user was on the next page. Else, it will register the "back" normally and the user will exit as expected.

您可能希望在其中用户可追溯到previous页面(假设你有超过两页)的情况下。在这种情况下,要实现<一个href=\"http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html\">ViewPager.OnPageChangeListener()每一次使用onPageSelected(INT位置)被称为保存的位置。如果你在一个堆栈实现它们,那么你可以将用户presses回每次弹出他们关闭。如果堆栈是空的,退出程序。

You may want the case in which the user goes back to the previous page (assuming you have more than two pages). In which case, you want to implement the ViewPager.OnPageChangeListener() and save the position every time onPageSelected(int position) is called. If you implement them in a stack, then you can pop them off every time the user presses "back". Once the stack is empty, exit the app.

修改

由于正确地由玉拜菲尔德,一个稍微容易的方法来做到这一点的评论表示将在活动使用 onBack pressed()。这只会在API层面开展工作,5 +

As correctly stated in the comments by Jade Byfield, a slightly easier way to do this would be to use onBackPressed() in the Activity. This will only work in API levels 5+.

@Override
public void onBackPressed() {
  if(i_dont_want_to_leave) {
     myViewPager.setCurrentItem(0, true);
  } else {
    super.onBackPressed(); // This will pop the Activity from the stack.
  }
}

这篇关于如何使用视图寻呼机来处理后退按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆