Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Direction of Place with Arrow in Android ListView

I have requirement to show list of some places in Android ListView with the direction of that place. I able to that by finding the angle between the locations but the issue is that My Client required the arrow to be rotated when the device was rotated or moved. I failed to found any solution that will rotate arrow as per Sensor Changes. Do any of them across such requirement? Please suggest me some solution to get the list to be displayed as this in this iPhone App. or look at below image.

UPDATED:

I am expecting any answer such that it will be helpful in starting the below requirement. Even displaying the arrow alone is welcome so that I will try to achieve the below. all the samples I have done are shaking and they are not stable.

enter image description here

like image 601
TNR Avatar asked Feb 02 '13 05:02

TNR


Video Answer


2 Answers

Have your adapter hold a collection of objects with all info you want on each list item, including a float value for rotation of arrow.

public class Place {
 private Srting name;
 private double distance;
 private float arrowAngle;

//-- etc etc--

Override Adapter's getView(), set rotation of arrow View:

@Override
public View getView(View currentView, int position, ViewGroup parent){
  //--inflate layout for each row, find views, set values etc etc--

  Place p = getItem(position);
  arrowView.setRotation(p.getArrowAngle());

 //--return inflated view --
}

On sensor change, calculate new angle of each arrow, and update each of the data objects in your adapter.

for (int i = 0 ; i < adapter.getCount(); i++){
    Place p = adapter.getItem(i);

   //--calculate angle--

   p.setArrowAngle(newAngle);
}

adapter.notifyDataSetChanged();

That would do it, but sensors may change very fast and too much updates of ListView data will cause ListView to lag, to limit updates, declare a long variable lastUpdated, On sensor change see if System.nanoTime() - lastupdated > 500, then only call the above updating code. This will limit updates to a 500 millisecond interval.

like image 122
S.D. Avatar answered Oct 15 '22 14:10

S.D.


In your sensor change event handler, call ListView.invalidateViews(). This will cause all of the existing views in the ListView to redraw. SO, if you are currently to the point where you can draw the list correctly and get events from the sensors as you like, then adding this call should get you the behavior you want.

like image 35
Scott Leslie Avatar answered Oct 15 '22 14:10

Scott Leslie