【问题标题】:Notify fragment`s view childs通知片段的视图子项
【发布时间】:2013-08-17 01:08:45
【问题描述】:

我有一些片段需要在它们可见时运行一些动画。

这些片段位于 ViewPager 中。感谢那个答案 - How to determine when Fragment becomes visible in ViewPager - 我知道当我的片段通过 getUserVisibleHint 方法可见时我会收到通知。

因为我有很多动画,所以我制作了知道如何为自己设置动画的自定义视图。现在我需要在片段的 getUserVisibleHint 方法中通知片段内的所有视图,让他们知道是时候为自己制作动画了。

所以我的问题是: 如何通知片段内的所有视图?

欢迎任何解决方案,但我说过,我需要通知的视图是我创建的自定义视图,所以我相信我也需要某种自定义通知。

【问题讨论】:

    标签: android android-fragments android-animation


    【解决方案1】:

    你说你所有的观点都是习惯的。我假设它们都实现了一个特定的接口(比如说,InterfaceA)。

    以下代码将在实现InterfaceA 接口的每个自定义视图上调用notificationMethod()viewGroup 是您的根视图(例如,LinearLayout)。

    int childcount = viewGroup.getChildCount();
    for (int i=0; i < childcount; i++){
       View view = viewGroup.getChildAt(i); 
       if (view instanceof InterfaceA) { 
           ((InterfaceA)view).notificationMethod();
       }
    }
    

    【讨论】:

    • 我建议不要直接投:View child = viewGroup.getChildAt(i); if (child instanceof InterfaceA) { ((InterfaceA)child).notificationMethod();。这样,如果布局中有任何未实现接口的视图,它将优雅地继续。
    猜你喜欢
    • 2018-10-06
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多