简体   繁体   English

聆听 ViewGroup 中的变化

[英]Listen to changes in ViewGroup

I have created a very small library to change the fonts of my app.我创建了一个非常小的库来更改我的应用程序的字体。

Up until now I have been changing fonts after inflating layouts and just after adding TextViews to my layouts.到目前为止,我一直在膨胀布局之后以及在将 TextView 添加到我的布局之后更改字体。 It works, but I have to remember to do this for each and every change in my layouts that involves a TextView .它有效,但我必须记住对涉及TextView布局中的每一个更改都执行此操作。 This is not sustainable for a large project and long term maintenance of my code.这对于大型项目和我的代码的长期维护来说是不可持续的。

I want a way to listen for changes on the layout from the Activity so I can do the font changes there.我想要一种方法来侦听Activity布局的更改,以便我可以在那里进行字体更改。

I experimented with implementing ViewGroup.OnHierarchyChangeListener on my Activity like this:我尝试在我的Activity上实现ViewGroup.OnHierarchyChangeListener ,如下所示:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
                .findViewById(android.R.id.content)).getChildAt(0);
        viewGroup.setOnHierarchyChangeListener(this);
    }

This seemed to be a great solution because I could listen to onChildViewAdded(View parent, View child) and get a reference to whatever View was added to my ViewGroup .这似乎是一个很好的解决方案,因为我可以收听onChildViewAdded(View parent, View child)并获得对添加到我的ViewGroup任何View的引用。 The only problem is that this way to obtain viewGroup doesn't include the ActionBar, but just adding the same listener to the ActionBar would suffice.唯一的问题是,这种获取viewGroup的方式不包括ActionBar,但只需将相同的侦听器添加到ActionBar就足够了。

However, onChildViewAdded is never called.但是,永远不会调用onChildViewAdded

I am doing mostly Fragment transactions inside my Activity 's layout.我主要在Activity的布局中进行Fragment事务。 I don't know if this is what is stopping the callback from happening.我不知道这是否是阻止回调发生的原因。

I'm now trying with ViewTreeObserver.OnDrawListener , is this the way to go or is there another workaround to listen for changes in a ViewGroup ?我现在正在尝试使用ViewTreeObserver.OnDrawListener ,这是要走的路还是有另一种解决方法来侦听ViewGroup更改?

The view.setOnHierarchyChangeListener() only gives you callbacks when views are added to this view specifically. view.setOnHierarchyChangeListener()仅在将视图专门添加到此视图时才会给您回调。 When you add views to this view's children, you don't get any callbacks.当您向此视图的子视图添加视图时,您不会收到任何回调。 You will need to listen to them individually.您将需要单独聆听它们。

Look at the code at https://gist.github.com/JakeWharton/7189309 :查看https://gist.github.com/JakeWharton/7189309上的代码:

Whenever a view is added to this, attach a listener to hierarchy changes for all ViewGroup-type children.每当向此添加视图时,将侦听器附加到所有 ViewGroup 类型子项的层次结构更改。

This code in the listener demonstrates the idea:监听器中的这段代码演示了这个想法:

@Override public void onChildViewAdded(View parent, View child) {
    delegate.onChildViewAdded(parent, child);

    if (child instanceof ViewGroup) {
      ViewGroup childGroup = (ViewGroup) child;
      childGroup.setOnHierarchyChangeListener(this);
      for (int i = 0; i < childGroup.getChildCount(); i++) {
        onChildViewAdded(childGroup, childGroup.getChildAt(i));
      }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM