欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

刷新Activity中的scrollview示例(局部ui刷新)

程序员文章站 2022-06-29 09:25:51
1.创建一个包含scrollview的布局文件: 2.实现scrollview的刷新复制代码 代码如下:package com.example.uirefresh; i...

1.创建一个包含scrollview的布局文件:

2.实现scrollview的刷新

复制代码 代码如下:

package com.example.uirefresh;

import android.app.activity;
import android.os.bundle;
import android.view.view;
import android.widget.button;
import android.widget.scrollview;
import android.widget.textview;

public class mainactivity extends activity implements android.view.view.onclicklistener{
private button btnexit;
private textview text;
private scrollview scroll;
private boolean bool = true;
@override
protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    btnexit = (button) findviewbyid(r.id.exit);
    text    = (textview) findviewbyid(r.id.text);
    scroll  = (scrollview) findviewbyid(r.id.scroll);
    btnexit.setonclicklistener(this);
    //在主线程main中开一子线程来刷新局部的scrollview
    new thread(new refreshthread()).start();   
}

@override
public void onclick(view v) {
    switch(v.getid()) {
    case r.id.exit :
        finish();
        break;
    }

}  
public class refreshthread implements runnable{

    @override
    public void run() {
        while(bool) {
            try {
                thread.sleep(5000);//每五秒刷新一次
            } catch (interruptedexception e) {
                e.printstacktrace();
            }
            synchronized (this) {
                                //用post方法刷新
                text.post(new runnable() {                 
                    @override
                    public void run() {
                        text.append("hello world...n");
                    }
                });
            }
        }

    }      
}  
@override
protected void ondestroy(){
    super.ondestroy();
    bool = false;
}
}