如何使ViewPager.arrowScroll()第一次工作? [英] How can I make ViewPager.arrowScroll() work the first time?

查看:432
本文介绍了如何使ViewPager.arrowScroll()第一次工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewPager(使用FragmentStatePagerAdapter来分页片段).用户可以使用向左/向右滑动来从一个片段翻页到下一个片段.这很好.我还添加了< > 按钮进行分页.这些的onClick侦听器使用以下方式实现:

I have a ViewPager (using a FragmentStatePagerAdapter to page fragments). The user can use left/right swiping to page from one fragment to the next; this works fine. I've also added < and > buttons for paging. The onClick listeners for these are implemented using:

mViewPager.arrowScroll(View.FOCUS_LEFT);

mViewPager.arrowScroll(View.FOCUS_RIGHT);

这在大多数情况下都有效:当用户点击< > 时,视图将按预期向左或向右滚动.

This works most of the time: when the user taps < or >, the view scrolls left or right as expected.

但是在> 按钮的第一次轻击上,什么都没有发生.我确实听到喀哒"声,确认发生了轻击.而且我有一条日志语句,显示onClick侦听器已被调用,而mViewPager.arrowScroll(View.FOCUS_RIGHT)被调用了.我什至用arrowScroll()检查返回的结果;是true!但是没有分页发生.

But on the first tap of the > button, nothing happens. I do hear a "click" confirming that the tap occurred. And I have log statements showing that the onClick listener was called, and mViewPager.arrowScroll(View.FOCUS_RIGHT) was called. I even check the result return by arrowScroll(); it's true! Yet no paging happens.

然后,在所有< > 按钮上轻按即可.

After that, all tapping on the < and > buttons works fine.

为什么第一次调用arrowScroll(View.FOCUS_RIGHT)无效,我该如何解决?

Why would the first call to arrowScroll(View.FOCUS_RIGHT) have no effect, and how can I fix it?

我想我可以尝试第一次调用它两次,但是由于我不知道为什么没有记录在案的行为,所以我不知道这种方法是否会在某些手机或Android版本上导致重复寻呼

I guess I can try calling it twice the first time, but since I don't know why the documented behavior isn't happening, I don't know whether that approach will cause a double paging on some phones or Android versions.

// Before any button taps.
QuestionAdapter: getItem(0)
QuestionAdapter: instantiateItem: position 0
QuestionAdapter: getItem(1)
QuestionAdapter: instantiateItem: position 1
QuestionAdapter: setPrimaryItem: position 0
QuestionAdapter: setPrimaryItem: position 0
// The screen is displaying page 0.
// Now I tap the > button:
QuestionAdapter: setPrimaryItem: position 0
// The screen is still displaying page 0.
// Now I tap the > button again:
QuestionAdapter: getItem(2)
QuestionAdapter: instantiateItem: position 2
QuestionAdapter: setPrimaryItem: position 1
QuestionAdapter: setPrimaryItem: position 1
QuestionAdapter: setPrimaryItem: position 1
// Now the screen displays page 1.

推荐答案

好的,我想我已经开始解决问题了.当我跟踪ViewPager.arrowScroll(FOCUS_RIGHT)(

OK, I think I'm starting to figure out the problem. When I trace ViewPager.arrowScroll(FOCUS_RIGHT) (source code here) it looks like what it's doing is trying first to move the focus to the right (hmm, maybe that explains why the argument says FOCUS_RIGHT!).

因此,根据当前具有焦点的对象以及是否在其右边有nextFocus视图,它将仅将焦点移到那里,认为其工作已完成,然后返回true表示成功.

So depending on what currently has focus, and whether there's a nextFocus view that's to the right of it, it will just move focus there, consider its job done, and return true to signal success.

只有在右边没有nextFocus视图时,它才真正按照我的意愿进行pageRight().

Only if there's not a nextFocus view to the right will it actually pageRight() as I want it to do.

对于我来说,当我第一次按下> 时,currentFocus = this.findFocus()返回null.然后,它调用nextFocused = FocusFinder.findNextFocus()并在当前显示的页面上显示一个ListView.由于currentFocus为null而nextFocus不为null(在其他条件下),因此arrowScroll()可以将焦点设置到ListView上.

In my case, when I first press >, currentFocus = this.findFocus() returns null. Then it calls nextFocused = FocusFinder.findNextFocus() and comes up with a ListView that's on the currently displayed page. Since currentFocus is null and nextFocus is not (among other conditions), arrowScroll() is satisfied with setting focus to the ListView.

第二次我点击> 时,currentFocus = this.findFocus()返回ListView,并且nextFocused = FocusFinder.findNextFocus()产生null.因为nextFocused为null,所以它不会尝试nextFocused.requestFocus(),而是调用pageRight(),这是我首先要的.

The second time I tap >, currentFocus = this.findFocus() returns the ListView, and nextFocused = FocusFinder.findNextFocus() yields null. Because nextFocused is null, it doesn't try to nextFocused.requestFocus() but instead calls pageRight(), which is what I wanted in the first place.

在这种情况下,适合ViewPager设计的解决方案是什么?听起来arrowScroll()并非仅用于向左/向右翻页,就像希望向左/向右按钮那样.相反,它的作用是执行键盘上的箭头键应做的事情.因此得名.

That being the case, what is the solution that fits the design of the ViewPager? It sounds like arrowScroll() is not intended for just paging left/right, like a left/right button would be expected to do. Instead it's meant to do what a keyboard arrow key should do; hence the name.

那么接下来应该使用什么方法 来左右翻页,而不考虑当前有什么或可以得到焦点?

So then what method should be used to just page left/right, without regard for what currently has or can get focus?

我可以尝试通过在调用arrowScroll之前将焦点设置在正确的视图上来解决arrowScroll的行为,但这似乎有点麻烦.有pageRightpageLeft方法,它们看起来确实可以满足我的需要,但是它们不是公开的,也没有文档说明!

I could try to work around arrowScroll's behavior by setting focus to the right view before calling arrowScroll, but that seems like a kludge. There are the pageRight and pageLeft methods, which look like they do exactly what I need, but they're not public, nor documented!

好吧,pageRightpageLeft调用setCurrentItem(mCurItem + or - 1)来完成其工作,该方法是 公开且已记录的方法.所以我想我可以将pageRightpageLeft的代码复制到自己的代码中.

Well, pageRight and pageLeft call setCurrentItem(mCurItem + or - 1) to do their work, a method that is public and documented. So I guess I can just copy the code for pageRight and pageLeft into my own code.

这种API设计对我来说似乎很奇怪,因为在寻呼机屏幕上具有向左/向右按钮,无论焦点如何,都可以向左/向右翻页.同样令人沮丧的是,arrowScroll()的文档以及用于左右分页的ViewPager的文档如此含糊. 但无论如何,我认为我已经找到了一个不错的解决方案.

This API design seems strange to me, given how common it is to have left/right buttons on a pager screen that are expected to page left/right regardless of focus. It's also frustrating that the documentation for arrowScroll(), and for paging the ViewPager left/right, is so vague. But in any case I think I've found a decent solution.

这篇关于如何使ViewPager.arrowScroll()第一次工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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