這幾天在開發過程中涉及一個動態重新整理UI的程式,重新整理過程通過開闢個新的線程來執行,但操作過程中一直沒測試到結果通過對程式的跟蹤調試後發現主介面一直沒重新整理,折騰一番後,發現需要通過Handler組件通知主線程更新。這裡引用一個簡單的例子說明下,如果有什麼問題可以直接留言給我。
Handler homeAcHandler = new Handler(){
public void handleMessage(Message msg)
{
MyImageView myImageView = null;
if(msg.what == 0)
{
Log.d(LOG_TAG, "has recive msg");
synchronized (locationinfo.LOC_OBJECT) {
for (int i = 0; i <arraylist.size(); i++) {
int location_x,location_y;
location_x=(int)locationinfo.neighbourCollectionFinal.get(i).x;
location_y=(int)locationinfo.neighbourCollectionFinal.get(i).y;
myImageView=new MyImageView(XX.this,150,50,0.2f,0.5f,0.1f,0.6f);
myImageView.setImageResource(R.drawable.stars_woman);
mMyImageViewList.add(myImageView);
}
}
for(int i=0;i<mMyImageViewList.size();i++)
{
mStarsRealtiveLayout.addView(mMyImageViewList.get(i));//添加到介面
}
mMyImageViewList.clear();//清理
}
}
}
文章出處:EyeAndroid