Android 如何在非活动文件(即RecyclerView适配器)中调用SupportMapFragment
我想创建迭代地图视图。为此,我使用了RecyclerView 我尝试使用在适配器的构造函数中初始化的上下文调用活动方法getSupportFragmentManager()。但它不起作用 template.xmlAndroid 如何在非活动文件(即RecyclerView适配器)中调用SupportMapFragment,android,google-maps,android-recyclerview,Android,Google Maps,Android Recyclerview,我想创建迭代地图视图。为此,我使用了RecyclerView 我尝试使用在适配器的构造函数中初始化的上下文调用活动方法getSupportFragmentManager()。但它不起作用 template.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orienta
<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>
RecycleServiceAdapter.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) {
}
}
}
公共类HistoryAdapter扩展了RecyclerView.Adapter{
语境;
ArrayList数据表;
公共历史适配器(上下文mContext、ArrayList数据列表){
this.mContext=mContext;
this.dataList=dataList;
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup ViewGroup,int i){
LayoutInflater LayoutInflater=LayoutInflater.from(mContext);
视图=布局更平坦。充气(R.layout.history\u single,viewGroup,false);
最终视窗夹持器视窗夹持器=新视窗夹持器(视图);
返回视图持有者;
}
@凌驾
public void onBindViewHolder(@NonNull ViewHolder ViewHolder,int i){
}
@凌驾
public int getItemCount(){
返回dataList.size();
}
类ViewHolder扩展了RecyclerView.ViewHolder在MapReadyCallback上实现{
支持mappfragma片段;
公共视图持有者(@NonNull View itemView){
超级(项目视图);
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
}
}
}
我想知道如何在非活动java文件中初始化google地图。与上述情况相同。对于RecyclerView适配器,最好在中使用 在
RecyclerView
内部使用MapView
的正式示例:
您可以找到该情况下的MapView
布局设置:
使用或甚至代替
SupportMapFragment
可能会更好?是的,很有效谢谢您的帮助不客气!
/**
* 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 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" />