刷新或强制重绘片段 [英] Refresh or force redraw the fragment

查看:181
本文介绍了刷新或强制重绘片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个膨胀的XML布局的片段。我的要求是更新文本大小对我的片段在我的所有视图我的活动恢复时。我试过

I have a fragment that inflates an xml layout. My requirement is to update the text size on all my views inside my fragment when my Activity is resumed. I tried

    fragment.getView().invalidate();

这似乎并没有做的工作。我也试过

which didn't seem to do the work. I also tried

    fragment.getView().requestLayout();

这也不能工作。

which didn't work either.

在另一个活动,我有一个ListFragment这需要做同样的事情。我试过

On another activity, I have a ListFragment which needs to do the same thing. I tried

    listfragment.getListView().invalidate();

其中的伎俩,刷新我的列表视图和重画里面的所有项目。

which did the trick, refreshing my list view and redrawing all the items inside it.

我不明白为什么一个人的作品而不是其他。

I don't understand why one works but not the other.

我还看到有人推荐发起的片段交易,并用新的替换当前的片段,它已使我想知道

I have also seen people recommending initiating a fragment transaction and replacing the current fragment with a new one, and it has kept me wondering

  1. 为什么要创建一个全新的片段取代我现在的片段时,我需要的是更新的,我的片段包含。

  1. Why should I create a whole new fragment and replace my current fragment when all I need is to refresh the text on the views that my fragment contains.

片段的交易方法$ P $的定义在我活动的布局XML我片段pvent我,我会在合适的位置,以编程方式插入片段。

Fragment transaction method will prevent me from defining my fragment in the layout xml of my activity and I will have to programatically insert the fragment at the right position.

有没有简单的方法呢?

推荐答案

我不认为有是一个方法。片段重建它的用户界面上onCreateView()...但在创建或重新创建片段时发生。

I do not think there is a method for that. The fragment rebuilds it's UI on onCreateView()... but that happens when the fragment is created or recreated.

您将不得不实现自己的updateUI方法,或者在那里你会指定哪些元素应该如何更新。这是相当一个很好的做法,因为你需要做的,当片段反正创建的。

You'll have to implement your own updateUI method or where you will specify what elements and how they should update. It's rather a good practice, since you need to do that when the fragment is created anyway.

然而,如果这还不够,你可以不喜欢用的同一个替换片段迫使它叫 onCreateView()

However if this is not enough you could do something like replacing fragment with the same one forcing it to call onCreateView()

FragmentTransaction tr = getFragmentManager().beginTransaction();
tr.replace(R.id.your_fragment_container, yourFragmentInstance);
tr.commit()

注意

要刷新的ListView您需要调用 notifyDataSetChanged() ListView的适配器。

To refresh ListView you need to call notifyDataSetChanged() on the ListView's adapter.

这篇关于刷新或强制重绘片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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