2011-05-26 51 views
0

我試圖與方向指針列表視圖列表中的項目就像gooing緯度: http://www.eurodroid.com/pics/android_google_maps_latitude_update_2.png的Android導航指針

我已經有工作的代碼,但我擔心的表現。 當我只想旋轉方向指針圖像時,每當方向傳感器(這是很多)代碼重建與列表視圖適配器的整個列表,所以它的做法與調用notifyDataSetChanged相同。

我試着看看會發生什麼,如果我只是在傳感器發生變化時才調用setText(),那麼我發現它不會重建整個列表。

public class SensorWatcher{ 

private SensorManager mgr = null; 
public float azimuth = 0; 
private Main main; 
SensorWatcher(Main mainContext){ 
    main = mainContext; 
    mgr = (SensorManager)mainContext.getSystemService(Context.SENSOR_SERVICE); 
    mgr.registerListener(listener, mgr.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_UI); 
} 
private SensorEventListener listener = new SensorEventListener() { 
     int newAzimuth; 
     public void onSensorChanged(SensorEvent e) { 
     if (e.sensor.getType()==Sensor.TYPE_ORIENTATION) { 
      newAzimuth = Math.round(e.values[0]); 
      if(newAzimuth != azimuth){ 
       azimuth= newAzimuth; 
       updateItems(); 
       //main.adapter.notifyDataSetChanged(); 
      } 
     } 
     } 
    @Override 
    public void onAccuracyChanged(Sensor arg0, int arg1) { 
     // TODO Auto-generated method stub 
    } 
}; 
public void updateItems(){ 
    ListView list = main.list; 

    ImageView img; 
    View listItem; 
    if(list != null){ 
     for(int i =list.getFirstVisiblePosition();i<=list.getLastVisiblePosition();i++){ 
      if(i< list.getChildCount()){ 
       listItem=(View) list.getChildAt(i); 
       //listItem = list.getAdapter().getView(i, null, null); 
       img =(ImageView) listItem.findViewById(R.id.allowtest); 

       Bitmap bmp = BitmapFactory.decodeResource(main.getResources(), R.drawable.arrowup); 
       Matrix mtx = new Matrix(); 
       mtx.postRotate(calculateDirection(list,i)); 
       Bitmap rotatedBMP = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mtx, true); 
       BitmapDrawable bmd = new BitmapDrawable(rotatedBMP); 
       img.setImageDrawable(bmd); 
      } 

     } 
    } 
} 

回答

0

要在列表中進行更改,您需要調用notifyDataSetChanged ..沒有其他替代方案。

+0

我沒有調用notifyDataSetChanged,setImageDrawable是和爲什麼可以做一個setText沒有notifyDataSetChanged? – Mark 2011-05-26 09:36:29

+0

這是List的屬性。List是Android中高度優化的小部件。 – Eby 2011-05-26 12:47:24