How to know when the RecyclerView has finished laying down the items?

后端 未结 12 1417
野的像风
野的像风 2020-11-29 20:06

I have a RecyclerView that is inside a CardView. The CardView has a height of 500dp, but I want to shorten this height if the Re

12条回答
  •  攒了一身酷
    2020-11-29 20:47

    Here is an alternative way:

    You can load your recycler view in a thread. Like this

    First, create a TimerTask

    void threadAliveChecker(final Thread thread){
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    if(!thread.isAlive()){
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // stop your progressbar here
                            }
                        });
                    }
                }
            },500,500);
        }
    

    Second, create a runnable

    Runnable myRunnable = new Runnable() {
                            @Override
                            public void run() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        // load recycler view from here
                                        // you can also toast here
                                    }
                                });
                            }
                        };
    
    

    Third, create a thread

    Thread myThread = new Thread(myRunnable);
    threadAliveChecker();
    // start showing progress bar according to your need (call a method)
    myThread.start();
    

    Understanding the above code now:

    1. TimerTask - It will run and will check the thread (every 500 milliseconds) is running or completed.

    2. Runnable - runnable is just like a method, here you have written the code that is needed to be done in that thread. So our recycler view will be called from this runnable.

    3. Thread - Runnable will be called using this thread. So we have started this thread and when the recyclerView load (runnable code load) then this thread will be completed (will not live in programming words).

    So our timer is checking the thread is alive or not and when the thread.isAlive is false then we will remove the progress Bar.

提交回复
热议问题