Android addView in the background

I need to add many views to the loop, while this snippet does this, the application will also have a navigation box and an action bar where the user can do something.

therefore, I would like this process not to a) slow down the application by blocking the user, b) it is preferable to add views to the background thread.

The dilemma is that I think the android does not like viewing views in a thread other than the UI, so what is the best fit for this? I plan to have a runtime view object visible as a fragment, while the rest of the views are created using addView and its associated calculations

+6
source share
1 answer

Instead of adding a view to the background thread, you can set things up by placing multiple Runnables in the user interface thread. The code below is a very simplified version of this method, but it is similar to how it was done in the Android Launcher application:

 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); 
+17
source

Source: https://habr.com/ru/post/954634/


All Articles