android addView in background thread

后端 未结 1 1725
忘了有多久
忘了有多久 2020-12-29 13:51

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.

so I wo

相关标签:
1条回答
  • 2020-12-29 14:29

    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);
    
    0 讨论(0)
提交回复
热议问题