我想创建迭代地图视图。我使用RecyclerView。
我尝试使用我在适配器的构造函数中初始化的上下文调用activity方法getSupportFragmentManager()。但它不起作用。
TEMPLATE.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_margin="8dp"
app:cardElevation="20dp"
android:layout_width="match_parent"
android:layout_height="256dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="29-18-2019 5:30"
android:textSize="18dp"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/map_single_history"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v7.widget.CardView>
recycler view adapter.Java
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ViewHolder> {
Context mContext;
ArrayList<HistoryModel> dataList;
public HistoryAdapter(Context mContext, ArrayList<HistoryModel> dataList) {
this.mContext = mContext;
this.dataList = dataList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.history_single, viewGroup, false);
final ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
}
@Override
public int getItemCount() {
return dataList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
SupportMapFragment mMapFragment;
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
@Override
public void onMapReady(GoogleMap googleMap) {
}
}
}
我想知道如何在非活动java文件中初始化谷歌地图。如上所述。
对于RecyclerView适配器最好在MapView
中使用Lite Mode,如that在MapView
中使用RecyclerView
的官方示例:
/** * Holder for Views used in the {@link LiteListDemoActivity.MapAdapter}. * Once the the <code>map</code> field is set, otherwise it is null. * When the {@link #onMapReady(com.google.android.gms.maps.GoogleMap)} callback is received and * the {@link com.google.android.gms.maps.GoogleMap} is ready, it stored in the {@link #map} * field. The map is then initialised with the NamedLocation that is stored as the tag of the * MapView. This ensures that the map is initialised with the latest data that it should * display. */ class ViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback { MapView mapView; TextView title; GoogleMap map; View layout; private ViewHolder(View itemView) { super(itemView); layout = itemView; mapView = layout.findViewById(R.id.lite_listrow_map); title = layout.findViewById(R.id.lite_listrow_text); if (mapView != null) { // Initialise the MapView mapView.onCreate(null); // Set the map ready callback to receive the GoogleMap object mapView.getMapAsync(this); } } @Override public void onMapReady(GoogleMap googleMap) { MapsInitializer.initialize(getApplicationContext()); map = googleMap; setMapLocation(); } /** * Displays a {@link LiteListDemoActivity.NamedLocation} on a * {@link com.google.android.gms.maps.GoogleMap}. * Adds a marker and centers the camera on the NamedLocation with the normal map type. */ private void setMapLocation() { if (map == null) return; NamedLocation data = (NamedLocation) mapView.getTag(); if (data == null) return; // Add a marker for this item and set the camera map.moveCamera(CameraUpdateFactory.newLatLngZoom(data.location, 13f)); map.addMarker(new MarkerOptions().position(data.location)); // Set the map type back to normal. map.setMapType(GoogleMap.MAP_TYPE_NORMAL); } private void bindView(int pos) { NamedLocation item = namedLocations[pos]; // Store a reference of the ViewHolder object in the layout. layout.setTag(this); // Store a reference to the item in the mapView's tag. We use it to get the // coordinate of a location, when setting the map location. mapView.setTag(item); setMapLocation(); title.setText(item.name); } }
Here您可以找到该案例的MapView
布局设置:
<!-- MapView in lite mode. Note that it needs to be initialised programmatically before it can be used. --> <com.google.android.gms.maps.MapView android:id="@+id/lite_listrow_map" android:layout_width="match_parent" android:layout_height="150dp" map:liteMode="true" map:mapType="none" />