[英]How to call SupportMapFragment in non activity files i.e. RecyclerView Adapter
I want to create Iterative Map view.我想创建迭代地图视图。 For which Im using RecyclerView.为此我使用 RecyclerView。
I tried calling the activity method getSupportFragmentManager() using context that I initialised in constructor of my adapter.我尝试使用我在适配器的构造函数中初始化的上下文调用活动方法 getSupportFragmentManager()。 But it's not working.但它不起作用。
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>
RecyclerViewAdapter.java RecyclerViewAdapter.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) {
}
}
}
I want to know how to initialise google maps in non activity java files.我想知道如何在非活动 java 文件中初始化谷歌地图。 As in above case.如上例。
For RecyclerView Adapter is better to use MapView
in Lite Mode as in that Official example of using MapView
inside RecyclerView
:对于RecyclerView适配器是更好地使用MapView
在精简版模式作为该使用的官方例子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 you can find MapView
layout settings for that case: 在这里您可以找到该案例的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" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.