android:如何设置当我的 ViewFlipper 显示新子项时触发的侦听器

发布于 2024-08-29 11:00:16 字数 688 浏览 5 评论 0原文

我有一个 ViewFlipper,我希望在显示的子项发生更改时触发侦听器。我已经为 ViewFlipper 设置了一个 OnFocusChangeListener,但当我从子 0 翻转到子 1 时,它永远不会触发,反之亦然。 ViewFlipper 包含两个relativelayout,我尝试为它们设置 OnFocusChangeListeners,但当我尝试设置它时,我得到了 ClassCastException。这是我的代码:

RelativeLayout songsLayout = (RelativeLayout) findViewById(R.id.song_page_layout);
songsLayout.setOnFocusChangeListener(new OnFocusChangeListener() {
    public void onFocusChange(View view, boolean hasFocus) {
        showPopUp("View " + view.getId() + " now has focus: " + hasFocus);
    }       
} );

R.is.song_page_layout 是我的relativelayout 之一,showPopUp() 是我用来显示弹出窗口的函数。

有没有人有某种触发器的工作代码,当 ViewFlipper 更改显示的子项时会触发该触发器?

I have a ViewFlipper for which I want a listener to fire when the child displayed is changed. I have set an OnFocusChangeListener to the ViewFlipper but it never fires when I flip from child 0 to child 1 or vice-versa. The ViewFlipper contains two RelativeLayouts and I have tried setting OnFocusChangeListeners for those but I get a ClassCastException when I try to set it. Here's my code:

RelativeLayout songsLayout = (RelativeLayout) findViewById(R.id.song_page_layout);
songsLayout.setOnFocusChangeListener(new OnFocusChangeListener() {
    public void onFocusChange(View view, boolean hasFocus) {
        showPopUp("View " + view.getId() + " now has focus: " + hasFocus);
    }       
} );

R.is.song_page_layout is one of my RelativeLayouts and showPopUp() is a function I use to show, well, popups.

Does anyone have working code for some sort of trigger that fires when a ViewFlipper changes which child is displayed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

渡你暖光 2024-09-05 11:00:16

我认为你不明白 Android 上的“焦点”是什么意思;当用户选择该视图时,例如,当他们通过按下来选择 EditText,或者使用键盘选择按钮时。当 ViewFlipper 切换视图时,它不会获得或失去焦点。

据我所知,您可以:

  1. 不要使用 ViewFlipper 的自动翻转,而是在需要翻转时手动调用 showNext() (然后在代码中自行设置间隔)。

  2. 子类化或重新实现 ViewFlipper 并根据您的喜好添加更改侦听器。

    子类化或

I think you don't understand what "focus" means on Android; it's when a user selects that View, for example when they select an EditText by pressing on it, or they use the keyboard to select a button. When a ViewFlipper switches Views, it does not gain or lose focus.

As far as I know, you can either:

  1. Don't use ViewFlipper's automatic flipping, instead manually calling showNext() when you want it to flip (then setting the interval yourself in code).

  2. Subclass or reimplement ViewFlipper and add a change listener to your liking.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文