Jovan
Jovan

Reputation: 1741

Map Fragment inside listview?


I need some information because i don't know if this is possible to do.
I have Fragment activity and in there i have tabs. Tabs are list fragments. My problem:

I need some custom view in list. I have some information and below that i have a map with a pin that points to that place. Is this possible to have??? MapFragment inside ListFrragment???
If you can say is this possible and point me in the right direction how to implement it I would be very grateful!!!
Thanks...

Upvotes: 3

Views: 2323

Answers (2)

Vinicius Lima
Vinicius Lima

Reputation: 1631

I just went through a similar problem and I came up with the following solution. By the way, now play services has google map lite mode.

Let's suppose you have a ListView using an BaseAdapter, so you should override your getView method. This is how my getView looks like:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    if ( convertView == null )
        convertView = new CustomItem(mContext,myLocations.get(position));

    return convertView;
}

Where class CustomItem is the FrameLayout that represents my row.

public class CustomItem extends FrameLayout {

public int myGeneratedFrameLayoutId;

public CustomItem(Context context,Location location) {
    super(context);
    myGeneratedFrameLayoutId = 10101010 + location.id; // choose any way you want to generate your view id

    LayoutInflater inflater = ((Activity) context).getLayoutInflater();

    FrameLayout view = (FrameLayout) inflater.inflate(R.layout.my_custom_item,null);
    FrameLayout frame = new FrameLayout(context);
    frame.setId(myGeneratedFrameLayoutId);

    int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, getResources().getDisplayMetrics());
    LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT,height);
    frame.setLayoutParams(layoutParams);

    view.addView(frame);

    GoogleMapOptions options = new GoogleMapOptions();
    options.liteMode(true);
    MapFragment mapFrag = MapFragment.newInstance(options);

    //Create the the class that implements OnMapReadyCallback and set up your map
    mapFrag.getMapAsync(new MyMapCallback(location.lat,location.lng));

    FragmentManager fm = ((Activity) context).getFragmentManager();
    fm.beginTransaction().add(frame.getId(),mapFrag).commit();

    addView(view);
}

Hope it helps someone.

Upvotes: 4

CommonsWare
CommonsWare

Reputation: 1007359

Is this possible to have?

Using some sort of static map image, sure. Google has a static maps API for that -- while primarily for use on the Web, in principle you should be able to get it from an Android app as well.

MapFragment inside ListFrragment?

Putting a fragment in a ListView row will be difficult to impossible, as ListView is expecting its children to be Views, so you would probably need to use MapView instead of MapFragment. In addition, you have all the issues with scrolling of the map. And, this is a very heavyweight solution, so I would expect performance issues.

Upvotes: 4

Related Questions