Power layout for updating / redrawing android?

I want to change the position of the layout and after 75 ms return it to the first position to make a movement, and this 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 that the application stops for 750 ms and does nothing. I tried invalidate() , refreshDrawableState() , requestLayout() , postInvalidate() and tried to call onResume() , onRestart() , onPause() .

+6
source share
4 answers

Perhaps you need:

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

after making changes to the layout.

EDIT:

Run the code in another thread:

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

And whenever you need to update the interface from this thread, use:

 activity.runOnUiThread(new Runnable() { @Override public void run() { <code to change UI> } }); 
+16
source
 ActivityName.this.runOnUiThread(new Runnable() { @Override public void run() { <code to change UI> } }); 
0
source

Answer : You can update and re-create the event and save the data by sending it using intent.

 public void refresh() { Intent intent = getIntent(); intent.putExtra("extra_id",details); overridePendingTransition(0, 0); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); finish(); overridePendingTransition(0, 0); startActivity(intent); } 

and get it in onCreate

 details=getIntent().getStringExtra("extra_id") 
0
source

You should try using ValueAnimator (or object animator), the code below is written in kotlin, but the same logic will apply for Java:

 val childCount = someView.childCount val animators = mutableListOf<ValueAnimator>() for (i in 0..childCount) { val child = (someView.getChildAt(i)) val animator = ValueAnimator.ofInt(0, 75) animator.addUpdateListener { val curValue = it.animatedValue as Int (child.layoutParams as ViewGroup.MarginLayoutParams).bottomMargin = curValue child.requestLayout() } animator.duration = 75 animator.startDelay = 75L * i animators.add(animator) } animators.forEach { animator -> animator.start() } 

In fact, you create a group of animators whose start delay is proportional to the number of children, therefore, as soon as one animation ends, a new one begins.

0
source

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


All Articles