添加TextViews并更新ViewGroup以显示TextViews



我已经花了几个小时搜索和尝试,但我无法走上正确的道路。

第一个我的代码:

public class MyThreadLoc extends Thread{
Context context;
final ViewGroup view;
MyThreadLoc(Context context){
    super();
    this.context = context; 
    view = (ViewGroup) findViewById(R.id.mLayout);
}
int height;
@Override public void run() {
    Looper.prepare();   
    int i = 0;
    int y = 10;
    while(running && i < 20){ 
        i++;
        if(y >= 200){
        runOnUiThread(new Runnable() {
            public void run() {
                view.removeAllViews();
                view.setBackgroundColor(Color.BLACK);
                }
            });
        y=10;
        }
    try {
    Thread.sleep( 500 );
    Log.d("mystuff", "Sleep");  
        } catch ( InterruptedException e ) { e.printStackTrace(); }
    final ViewGroup.LayoutParams lParam = new    ViewGroup.LayoutParams(500,30); //MATCH_PARENT, 30);
        while(y < 200 && running){ 
        final float yb = y;
        runOnUiThread(new Runnable() {
            TextView tview; 
            public void run() {
            Log.d("mystuff", "innerrun");
                            synchronized (view){
                            tview = new TextView(context);
                            tview.setText("Test");
                            tview.setY(yb);
                            view.addView(tview, lParam);
                            height = tview.getHeight();
                            view.updateViewLayout(tview, new      RelativeLayout.LayoutParams(500,30));
                            Log.d("mystuff", Integer.toString(view.getChildCount()));
                             try {
                                    Thread.sleep( 50 );
                                    Log.d("mystuff", "Sleep");  
                                    } catch ( InterruptedException e ) { e.printStackTrace(); }
                            }
                        }
                    });
                 y = y + 100; //height +5;
                }

视图不会首先出现,但在最后会出现两个带有"测试"的文本视图。我如何更新它,以便看到出现的每个文本视图?

为什么帮助我解决这个问题的错误没有早点出现,所以我不需要问这个问题?

错误是:"跳过了300帧!应用程序可能在其主线程上做了太多工作"

这让我想到:应用程序可能在其主线程上做了太多工作

所以我从RunOnUiThread中删除了大部分内容,现在它可以工作了。只有2个文本视图出现,因为我有y<200设置为低以显示更多文本视图。

最新更新