gpt4 book ai didi

android - Google Maps Lite Mode 导致 RecyclerView 卡顿

转载 作者:IT王子 更新时间:2023-10-28 23:57:03 28 4
gpt4 key购买 nike

我有一个 RecyclerView 这是一个垂直滚动的项目列表。每个列表项在 Lite Mode 中包含一个 Google Maps V2 MapView .我正在利用这个返回位图而不是完整 map 的新功能来替代 Google Static Maps API

MapView要求您从父Activity/Fragment对应的方法。从 RecyclerView.Adapter 和/或 RecyclerView.ViewHolder 调用这些的正确位置在哪里?

我怎样才能清理回收的 MapViews 使内存不会泄漏,同时保持列表不卡顿?

Google says Lite Mode can be used in lists:

... ‘lite mode’ map option, ideal for situations where you want to provide a number of smaller maps, or a map that is so small that meaningful interaction is impractical, such as a thumbnail in a list.

ListItem.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.google.android.gms.maps.MapView
android:id="@+id/mapImageView"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="80dp"
android:layout_height="100dp"
map:liteMode="true"
map:mapType="normal"
map:cameraZoom="15"/>

<!-- ... -->

</RelativeLayout>

RecyclerView.Adapter 和 ViewHolder

public class NearbyStopsAdapter extends RecyclerView.Adapter<NearbyStopsAdapter.ViewHolder> {

private final Context mContext;

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

MapView map;

public ViewHolder(View view) {
super(view);
map = (MapView) view.findViewById(R.id.mapImageView);
// Should this be created here?
map.onCreate(null);
map.onResume();
}
}

public NearbyStopsAdapter(Context c) {
this.mContext = c;
}

@Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_nearby_stop, viewGroup, false);
return new ViewHolder(itemView);
}

@Override public void onBindViewHolder(ViewHolder holder, int position) {
//Call Async Map here?
holder.map.getMapAsync(this);
}

@Override public void onViewRecycled(ViewHolder holder) {
// Cleanup MapView here?
// if (holder.map != null) {
// holder.map.onPause();
// holder.map.onDestroy();
// }
}

@Override public void onViewAttachedToWindow(ViewHolder holder) {
// Setup MapView here?
// holder.map.onCreate(null);
// holder.map.onResume();
}

@Override public void onViewDetachedFromWindow(ViewHolder holder) {
// Cleanup MapView here?
// if (holder.map != null) {
// holder.map.onPause();
// holder.map.onDestroy();
// }
}

// ...
}

Logcat:

I/Google Maps Android API﹕ Google Play services package version: 659943
W/Google Maps Android API﹕ Map Loaded callback is not supported in Lite Mode
W/Google Maps Android API﹕ Buildings are not supported in Lite Mode
W/Google Maps Android API﹕ Indoor is not supported in Lite Mode
W/Google Maps Android API﹕ Toggling gestures is not supported in Lite Mode
W/Google Maps Android API﹕ Toggling gestures is not supported in Lite Mode
W/Google Maps Android API﹕ Toggling gestures is not supported in Lite Mode
W/Google Maps Android API﹕ Toggling gestures is not supported in Lite Mode

更新:(2018 年 6 月 8 日)Google 发布了在 ListView 中使用 Lite Maps 的代码示例。 See here

最佳答案

解决方法如下:

  1. ViewHolder类中实现OnMapReadyCallback
  2. onMapReady中,调用MapsInitializer.initialize,保证在获取 map 之前可以使用特征。

Use this class to initialize the Google Maps Android API if features need to be used before obtaining a map. It must be called because some classes such as BitmapDescriptorFactory and CameraUpdateFactory need to be initialized.

  1. onViewRecycled回收 map 。


    public class NearbyStopsAdapter extends RecyclerView.Adapter<NearbyStopsAdapter.ViewHolder> {


@Override
public void onBindViewHolder(ViewHolder holder, int position)
{
//get 'location' by 'position' from data list
//get GoogleMap
GoogleMap thisMap = holder.gMap;
//then move map to 'location'
if(thisMap != null)
//move map to the 'location'
thisMap.moveCamera(...);
}


//Recycling GoogleMap for list item
@Override
public void onViewRecycled(ViewHolder holder)
{
// Cleanup MapView here?
if (holder.gMap != null)
{
holder.gMap.clear();
holder.gMap.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}



public class ViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {

GoogleMap gMap;
MapView map;
... ...

public ViewHolder(View view) {
super(view);
map = (MapView) view.findViewById(R.id.mapImageView);

if (map != null)
{
map.onCreate(null);
map.onResume();
map.getMapAsync(this);
}

}


@Override
public void onMapReady(GoogleMap googleMap) {
//initialize the Google Maps Android API if features need to be used before obtaining a map
MapsInitializer.initialize(getApplicationContext());
gMap = googleMap;

//you can move map here to item specific 'location'
int pos = getPosition();
//get 'location' by 'pos' from data list
//then move to 'location'
gMap.moveCamera(...);

... ...
}

}
}

关于android - Google Maps Lite Mode 导致 RecyclerView 卡顿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28612782/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com