How can I get to GoogleMap from RecyclerView.ViewHolder
I have:
public class MapRowHolder extends RecyclerView.ViewHolder
{
public MapRowHolder(View view) {
super(view);
}
}
xml:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.v7.cardview="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android.support.v7.cardview:cardBackgroundColor="@color/blue"
android.support.v7.cardview:cardCornerRadius="@dimen/card_corner"
android.support.v7.cardview:cardElevation="@dimen/card_elevation">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="right|center_vertical"
android:gravity="center_vertical|right"
android:layout_width="fill_parent">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="250dp"
android:tag="maps"
android:id="@+id/map_view_fragment" tools:context=".Map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_centerInParent="true"/>
</LinearLayout>
adapter:
public class MapAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
RecyclerView.ViewHolder mh;
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_row, viewGroup, false);
mh = (RecyclerView.ViewHolder)new PodatkiZaMapRowHolder(v);
return mh;
}
public void onBindViewHolder(RecyclerView.ViewHolder RowHolder, int polozaj) {
MapRowHolder mapRowHolder = (MapRowHolder)RowHolder;
}
}
It displays map correctly. And it is zoomed out to whole earth. And positioned to 0,0 in center.
How can I get maps fragment (in ViewHolder)? I would like to move camera to other location (I can do this after I get GoogleMap object) How and where should I get GoogleMap object?
If I use MapView instead map fragment It works, but doesn't refresh map. Map refreshes after I click on map a few times. Regards, Ales
What I ended up is to dynamically add SupportMapFragment to the FrameLayout inside item layout. You just have to create additional FrameLayout with unique id.
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/car_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Peugeot 407 2.0 HDI"
android:textSize="20dp"
android:gravity="center_horizontal"
android:textColor="?attr/colorPrimary"
android:textStyle="bold"
android:layout_marginBottom="5dp"
/>
<include layout="@layout/ride_item_summary" />
<FrameLayout
android:id="@+id/map_layout"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
adapter:
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
super.onBindViewHolder(holder, position);
switch (getItemViewType(position)) {
case 1:
if (first) {
... //check if polyline is not null, init other views
FrameLayout view = (FrameLayout) ((RideItemHolder) holder).mMapLayout;
FrameLayout frame = new FrameLayout(mContext);
frame.setId(1010101); //you have to set unique id
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 170, mContext.getResources().getDisplayMetrics());
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, height);
frame.setLayoutParams(layoutParams);
view.addView(frame);
GoogleMapOptions options = new GoogleMapOptions();
options.liteMode(true);
SupportMapFragment mapFrag = SupportMapFragment.newInstance(options);
//Create the the class that implements OnMapReadyCallback and set up your map
mapFrag.getMapAsync(new MapReadyCallback());
FragmentManager fm = fragment.getChildFragmentManager();
fm.beginTransaction().add(frame.getId(), mapFrag).commit();
first = false;
}
}
break;
case 2:
...
private class MapReadyCallback implements OnMapReadyCallback {
@Override
public void onMapReady(GoogleMap googleMap) {
drawMap(googleMap);
}
}