Как вызвать SupportMapFragment в неактивных файлах, например, в RecyclerView Adapter - PullRequest
0 голосов
/ 14 февраля 2019

Я хочу создать итеративное представление карты.Для чего я использую RecyclerView.

Я попытался вызвать метод действия 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>

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) {

    }
}
}

Я хочу знать, как инициализировать карты Google в неактивных java-файлах.Как в случае выше.

1 Ответ

0 голосов
/ 14 февраля 2019

Для RecyclerView Adapter лучше использовать 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);
        }
    }

Здесь вы можете найти 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" />
...