Android: compass + distance in a listview

后端 未结 1 1006
Happy的楠姐
Happy的楠姐 2020-12-23 23:57

I guess you have all tried \"Google Places\" in Maps. This is a list of POI close to you.

I really would like to do the same feature in my application with a list of

相关标签:
1条回答
  • 2020-12-24 00:28

    There are a few options for what you're trying to achieve. Personally, I'd recommend implementing your own Adapter (most likely in this case by extending SimpleCursorAdapter) and encapsulating the updates to distance text and compass heading rotation within that.

    To help with resource management, you probably want to create a SensorListener and LocationListener within the Activity that will host the ListView. Whenever you receive an update call your self-rolled updateCompassAndLocation method from within your Adapter class.

    From within that method you have two options. Either iterate over each of the items that make up the data collection being represented and modify the compass graphic and distance text, or simply record the current location and heading as variables within the class, and call notifyDataSetChanged to force the adapter to update the views itself within the getView method. In either case (especially the latter), you'll need to set the distance text and heading compass values within getView.

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      LinearLayout myView;
    
      MyPOI item = getItem(position);
    
      Location poiLocation = item.getLocation;
    
      int compassHeading = // Calculate heading relative to current heading
      float distance = // Calculate distance to POI
    
    
      if (convertView == null) {
        myView = new LinearLayout(getContext());
        String inflater = Context.LAYOUT_INFLATER_SERVICE;
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(inflater);
        vi.inflate(resource, myView, true);
      } else {
        trainView = (LinearLayout) convertView;
      }
    
      TextView distanceView = (TextView)trainView.findViewById(R.id.distance);
      ImageView compassView = (ImageView)trainView.findViewById(R.id.compass);
    
      distanceView.setText(String.valueOf(distance);
      compassView.setImageLevel(compassHeading);
    }
    
    0 讨论(0)
提交回复
热议问题