我需要添加大量的意见在一个循环中,而该片段这样做,应用程序也将有一个抽屉式导航和操作栏,用户可以做的事情。
I need to add lots of views in a loop, while this fragment does that, the app will also have a navigation drawer and action bar where the user can do things.
所以我想这个过程不是一)通过阻止用户拖慢应用程序,B)preferably添加的意见在后台线程。
so I would like this process to not a) slow down the app by blocking the user, b) preferably add the views in a background thread.
的困境是,我认为Android不喜欢在一个非UI线程无以复加的观点,那么,有没有最好的做法呢?我计划有一个进度条视图对象在片段的视图中,同时正在与addView和相关的计算产生的意见其余
The dilemma is that I think android doesn't like views to be added in a non-UI thread, so is there a best practice for this? I plan to have a progress bar view object visible in the fragment's view while the rest of the views are being generated with the addView and associated computations
推荐答案
而不是添加视图在后台线程则可以包裹由发布工作的若干的Runnable
的UI线程。在code以下是该技术的一个高度简化的版本,但它类似于如何在Android的应用程序启动已完成:
Instead of adding view on a background thread you can parcel out the work by posting several Runnables
on the UI thread. The code below is a highly simplified version of that technique but it's similar to how it was done in Android's Launcher app:
private void createAndAddViews(int count} {
for (int i = 0; i < count; i++) {
// create new views and add them
}
}
Runnable r = new Runnable() {
public void run() {
createAndAddViews(4); // add 4 views
if (mMoreViewsToAdd) mTopLevelView.post(this);
}
};
mTopLevelView.post(r);
相关推荐
最新文章