forceLayout(),requestLayout() [英] forceLayout(), requestLayout()

查看:1749
本文介绍了forceLayout(),requestLayout()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android文档阅读认定方法forceLayout()(这是产生下一个布局请求的布局显示)和requestLayout()(这是应该张贴立即进行布局的要求),但我不能让他们表现为标榜。特别是,如果我以后做了Thread.Sleep前一组文字和一,它等待睡眠来一次设置两种文本之前完成,我是否调用forceLayout()和requestLayout()之间。请不要用如何我不应该在UI线程中调用了Thread.Sleep一大堆废话回应。如果我换一个CountDownTimer它工作得很好(只要我有时间刻度足够短,不与睡眠时间干扰,定时器足够长的时间,以使睡眠来完成的时间Thread.sleep代码,以下是一个例子:

  INT I = 0;
TextView中TV2;
TextView的TV1;
LL的LinearLayout;
按钮BT;
@覆盖
    公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    LL =新的LinearLayout(本);
    ll.setOrientation(LinearLayout.VERTICAL);
    TV1 =新的TextView(本);
    TV2 =新的TextView(本);
    BT =新按钮(本);
    bt.setText(preSS启动);
    ll.addView(BT);
    ll.addView(TV1);
    ll.addView(TV2);
    tv2.setText();
    的setContentView(Ⅱ);
    bt.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
            tv1.setText(开始睡觉);
            新CountDownTimer(6000,50){
              公共无效onTick(长msuf)
                {如果(我== 1)
                    {
                尝试{
              视频下载(4000);
              tv2.setText(醒来);
               }
            赶上(InterruptedException的五){};
            }
                我++;
                 }
               公共无效onFinish(){}}开始()。
              }
      });        }


解决方案

[大量的废话有关调用睡眠()在UI线程。如果我得到它的权利,你的意思是有这样的:

  // ...里面onTick()
尝试{
    tv2.setText(几乎醒来); //第一的setText()
    视频下载(4000);
    tv2.setText(醒来); //第二seText()
}

如果你让你的主线程睡眠,它只是停止处理任何东西:当前方法,螺纹环和消息队列。再次醒来时,将完成执行方法,与第二的setText()覆盖第一个,然后离开了线程循环继续,做用户界面刷新,只显示第二个文本。

requestLayout()也不 forceLayout()其实可以立即使UI更新,他们都将进度的线程中循环的布局要求。我不知道,但我认为它们之间的区别是, requestLayout()是由已经改变其父<它的大小/位置的视图称为/ EM>和 forceLayout()是由一种叫做的ViewGroup 需要的及其子的被重新布置。

因此​​[关于调用多废话了睡眠()在UI线程。对于这样的事情叫 postDelayed()在主线程处理器是最好的解决办法可能是,如果你不希望与多线程一塌糊涂。

My reading of the android documentation finds the methods forceLayout() (which is to produce a layout display at the next layout request) and requestLayout() (which is supposed to post an immediate layout request), but I can not get them to behave as advertised. In particular, if I do one set text before a Thread.Sleep and one after, it waits for the Sleep to finish before setting both texts at once, whether or I call the forceLayout() and requestLayout() in between. Please do not respond with a lot of nonsense about how I should not call a Thread.Sleep in the UI thread. If I wrap the Thread.Sleep in a CountDownTimer it works perfectly well (as long as I have the tick time short enough to not interfere with sleep time, and the duration of the timer long enough to permit the Sleep to finish. The following is an example:

    int i=0;
TextView tv2;
TextView tv1;
LinearLayout ll;
Button bt;
@Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ll=new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    tv1=new TextView(this);
    tv2=new TextView(this);
    bt=new Button(this);
    bt.setText("Press to start");
    ll.addView(bt);
    ll.addView(tv1);
    ll.addView(tv2);
    tv2.setText("");
    setContentView(ll);
    bt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            tv1.setText("starting sleep");
            new CountDownTimer(6000,50){
              public void onTick(long msuf)
                {if(i==1)
                    {
                try{
              Thread.sleep(4000);
              tv2.setText("waking up");
               }
            catch(InterruptedException e){};
            }
                i++;
                 }
               public void onFinish(){}}.start();
              }         
      });

        }

解决方案

[lots of nonsense about calling sleep() in UI thread]. If i get it right, you mean having something like:

//...inside onTick()
try {
    tv2.setText("almost waking up"); // first setText()
    Thread.sleep(4000);
    tv2.setText("waking up"); // second seText()
}

If you make your main thread sleep, it will just stop processing anything: the current method, the thread loop and the message queue. Once awake again, it will finish executing the method, with the second setText() overriding the first one, and then leave the thread loop continue and do the UI refresh, showing only the second text.

Not requestLayout() nor forceLayout() can actually make the UI refresh immediately, they will both schedule a layout request in the thread loop. I'm not sure, but I think the difference between them is that requestLayout() is called by a view that has changed its size/position in its parent, and forceLayout() is called by a ViewGroup that needs its children to be re-laid out.

Therefore [more nonsense about calling sleep() in UI thread]. For such things calling postDelayed() on a main thread handler is the best solution probably, if you don't want to mess with multithreading.

这篇关于forceLayout(),requestLayout()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆