刷新片段内的图 [英] Refreshing a view inside a fragment
问题描述
我有搜索,看起来像这样的许多问题,但还没有找到我的答案的任何人。
我有一个有3个标签通过操作栏访问活动。我加入3片段膨胀的自定义视图我做了扩展视图类来实现这一点。
目前,该数据库的变化,我试图通过调用无效()/ postinvalidate()刷新我的选项卡中的观点,但是这并不能正常工作。用于呼叫片段onCreateView正如许多其他的选择予考虑的也是如此。
当我去到另一个选项卡,然后回去,但是,这种变化已经取得了和我的看法是更新的,它应该是。
我如何可以模拟,改变到另一个选项卡时,会发生同样的事情?是什么发生。我想看一看片段生命周期(试着拨打onCreateView())的数字出来,但它只是不希望刷新/重绘,因为它应该。
该数据是否正确,因为当我改变到另一个选项卡中的数据被更改。
我删除了一些code,因为它不再是相关的。我实现了,而不是我自己的Observer模式Cursorloaders通知的变化。这是我的主要活动现在。
现在的问题是我该怎么办,现在如果我要重新绘制这些片段内的视图。如果我申请fragmentObject.getView()。无效(),这是行不通的。我有同样的问题和以前一样,但现在我的观察,通知在数据库中的变化正确地与装载机实现。
公共类ArchitectureActivity扩展活动实现LoaderManager.LoaderCallbacks<光标> {
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
动作条动作条= getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab EditTab = actionbar.newTab()的setText(编辑)。
ActionBar.Tab VisualizeTab = actionbar.newTab()的setText(可视化)。
ActionBar.Tab AnalyseTab = actionbar.newTab()的setText(分析)。
片段editFragment =新EditFragment();
片段visualizeFragment =新VisualizeFragment();
片段analyseFragment =新AnalyseFragment();
EditTab.setTabListener(新MyTabsListener(editFragment));
VisualizeTab.setTabListener(新MyTabsListener(visualizeFragment));
AnalyseTab.setTabListener(新MyTabsListener(analyseFragment));
actionbar.addTab(EditTab);
actionbar.addTab(VisualizeTab);
actionbar.addTab(AnalyseTab);
ArchitectureApplication architectureApplication =(ArchitectureApplication)getApplicationContext();
architectureApplication.initialize();
getLoaderManager()initLoader(0,空,这一点)。
getLoaderManager()initLoader(1,null,则此)。
}
公共装载机<光标> onCreateLoader(INT ID,捆绑参数){
如果(ID == 0){
返回新CursorLoader(这一点,GraphProvider.NODE_URI,NULL,NULL,NULL,NULL);
}否则如果(ID == 1){
返回新CursorLoader(这一点,GraphProvider.ARC_URI,NULL,NULL,NULL,NULL);
}
返回null;
}
公共无效onLoadFinished(装载机<光标>装载机,光标光标){
//重新加载数据,实际情况是因为切换到另一个选项卡时,新的数据显示,高达精
Log.e(数据,装);
}
公共无效onLoaderReset(装载机<光标>装载机){
}
}
我已经找到了解决办法,刷新我的片段里面的观点。我重新创建(新CustomView)认为每一个数据库已经被更新的时间。我调用的setContentView(CustomView视图)后。它看起来更像是一个黑客,但它的作品,没有别的,我试图做。
虽然我的问题并没有真正解决,我给了奖励亚历克斯·洛克伍德。他对装载机的建议使我的应用程序更好,它使我不停地寻找,我终于找到了解决办法。
I have searched the numerous questions that look like this one, but haven't found my answer in any of them.
I have an activity that has 3 tabs accessible through the action bar. I achieved this by adding 3 fragments that inflate a custom view I made extending the view class.
At the moment the database changes, I try to refresh the view in my tab by calling invalidate()/postinvalidate(), but this does not work. The same is true for calling onCreateView of the fragment just as many other options I considered.
When I go to another tab and go back, however, the change has been made and my view is updated as it should be.
How can I simulate the same thing that happens when changing to another tab? What does happen. I tried to look at the Fragment lifecycle (tried to call onCreateView()) to figure it out but it just doesn't want to refresh/redraw as it should.
The data is loaded properly, as the data is changed when I change to another tab.
I deleted some of the code as it is no longer relevant. I implemented Cursorloaders instead of my own Observer pattern to notify a change. This is my main activity right now.
The question is what should I do now if I want to redraw the view inside these fragments. If I apply fragmentObject.getView().invalidate() it does not work. I'm having the same problem as before, but now my Observer to notify a change in the database is properly implemented with loaders.
public class ArchitectureActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab EditTab = actionbar.newTab().setText("Edit");
ActionBar.Tab VisualizeTab = actionbar.newTab().setText("Visualize");
ActionBar.Tab AnalyseTab = actionbar.newTab().setText("Analyse");
Fragment editFragment = new EditFragment();
Fragment visualizeFragment = new VisualizeFragment();
Fragment analyseFragment = new AnalyseFragment();
EditTab.setTabListener(new MyTabsListener(editFragment));
VisualizeTab.setTabListener(new MyTabsListener(visualizeFragment));
AnalyseTab.setTabListener(new MyTabsListener(analyseFragment));
actionbar.addTab(EditTab);
actionbar.addTab(VisualizeTab);
actionbar.addTab(AnalyseTab);
ArchitectureApplication architectureApplication = (ArchitectureApplication)getApplicationContext();
architectureApplication.initialize();
getLoaderManager().initLoader(0, null, this);
getLoaderManager().initLoader(1, null, this);
}
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id == 0){
return new CursorLoader(this, GraphProvider.NODE_URI , null, null, null, null);
} else if (id == 1){
return new CursorLoader(this, GraphProvider.ARC_URI , null, null, null, null);
}
return null;
}
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
// Reloading of data, actually happens because when switching to another tab the new data shows up fine
Log.e("Data", "loaded");
}
public void onLoaderReset(Loader<Cursor> loader) {
}
}
I've found a workaround to refresh the view inside my fragment. I recreated (new CustomView) the view every time the database has been updated. After that I call setContentView(CustomView view). It looks more like a hack, but it works and nothing else that I tried does.
Although my problem was not actually solved, I gave the reward to Alex Lockwood. His advice on Loaders made my application better and it caused me to keep looking for a solution that I eventually found.
这篇关于刷新片段内的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!