首页 > 系统 > Android > 正文

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

2020-04-11 11:56:51
字体:
来源:转载
供稿:网友

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;
}
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表