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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With