Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force layout to refresh/repaint android?

I want to change position of layout and after 75ms return it to first position to make a movement and that is my code:

for(int i = 0; i < l1.getChildCount(); i++) {  
    linear = (LinearLayout) findViewById(l1.getChildAt(i).getId());  
    LayoutParams params = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);  
    params.bottomMargin = 10;  
    linear.setLayoutParams(params);  
    SystemClock.sleep(75);
}   

The problem is the app is stop for 750ms and don't do anything. I tried invalidate() , refreshDrawableState(), requestLayout(), postInvalidate(), and try to call onResume(), onRestart(), onPause() .

like image 906
Youssef Maouche Avatar asked Aug 27 '13 16:08

Youssef Maouche


People also ask

How do I force redraw a view?

Invoking invalidate() on a View causes it to draw itself via the View. You should check this out: http://developer.android.com/guide/topics/ui/custom-components.html. A TextView internally invalidates itself when you invoke setText() and redraws itself with the new text set via the setText() call.

What is SwipeRefreshLayout?

The swipe-to-refresh user interface pattern is implemented entirely within the SwipeRefreshLayout widget, which detects the vertical swipe, displays a distinctive progress bar, and triggers callback methods in your app.


1 Answers

Maybe you need:

linear.invalidate();
linear.requestLayout();

after making the layout changes.

EDIT:

Run the code on a different thread:

new Thread() {
    @Override
    public void run() {
        <your code here>
    }
}.start();

And whenever you need to update the UI from that thread use:

activity.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        <code to change UI>
    }
});
like image 183
prc Avatar answered Oct 03 '22 19:10

prc