Search code examples
javaandroidbackground-processandroid-recyclerview

Efficiently update RecyclerView Grid or otherwise Display Complex Grid


I have a grid of pixels being displayed using a RecyclerView (as shown in image). When the button is pushed, most of the pixels will be changing colors. I want this update to process each pixels color change more uniformly, even if it takes a very long time to complete (there may be hundreds of thousands of pixels) without blocking the main thread. How is this possible?

If possible I'd like a smooth flow as individual pixels change. The most important thing here is not completing the action quickly but smoothly. I'm not attached to RecyclerView if this isn't the best choice - I was just lead to believe it would be better for performance. I don't need listeners of any kind for the cells. Eventually the cells will become more complex, each housing other cells (Layered Views with background colors). I will be using an SQL database and the Room library for data storage and edit.

The process should be happening over a background thread so as not to halt the UI. Currently I am updating my RecyclerView via myRecyclerAdapter.notifyItemChanged(i); on the onClickListener of the button and my UI will halt while it completes.

screenshot


Solution

  • Try the CustomView,there are very good reasons to use CustomView for creating views that are not provided by the standard widgets. You shoud be able to use a background thread to implement the time sync or delay between each transition,to make it smooth.And use runOnUiThread() method to change the ui i.e pixel colors in your case. for example- you can try stuff like this-

     Runnable onAnimationStarted=new Runnable() {
                @Override
                public void run() {
                //change ui
                }
            };
    Runnable onAnimationStopped=new Runnable() {
                @Override
                public void run() {
                //change ui
                }
            };
           Thread thread = new Thread(){
                    @Override
                    public void run() {
        runOnUiThread(onAnimationStarted);
                  long start= System.currentTimeMillis();;
                  while(System.currentTimeMillis()-start<1000) //waits one second
                {
    
                };
        runOnUiThread(onAnimationStopped);                 
            }
    
            };
                thread.start();