我正在製作一個應用程序,顯示地圖卡片視圖與回收者視圖。最後,我得到了我想要的結果。但我有一些問題。我需要一些幫助來解決它Google Map Api,我必須在android recylcer適配器中進行初始化?
我不知道我要在哪裏初始化谷歌地圖。 - 所以選項和地圖位置不直接應用。 when app launch after scroll it one or twice
這是我的回收站適配器代碼
public class EqListAdapter extends RecyclerView.Adapter<EqListAdapter.MyViewHolder>{
Context mContext;
EqData eqData;
View convertView;
public EqListAdapter(Context mContext, EqData eqData) {
this.mContext = mContext;
this.eqData = eqData;
}
@Override
public EqListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(EqListAdapter.MyViewHolder holder, int position) {
final float str = eqData.result.get(position).strength;
final LatLng latlng = new LatLng(eqData.result.get(position).lat,eqData.result.get(position).lon);
final String loc = eqData.result.get(position).location;
final String time = eqData.result.get(position).date;
GoogleMap thismap = holder.gMap;
if (thismap != null){
thismap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 13));
thismap.addMarker(new MarkerOptions().position(latlng));
thismap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
}
});
thismap.getUiSettings().setMapToolbarEnabled(false);
}
holder.locationText.setText(loc);
holder.strText.setText(""+str);
holder.timeText.setText(time);
}
@Override
public void onViewRecycled(MyViewHolder holder) {
super.onViewRecycled(holder);
// Cleanup MapView here?
/* if (holder.gMap != null)
{
holder.gMap.clear();
holder.gMap.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}*/
@Override
public int getItemCount() {
return this.eqData.result.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback
{
TextView strText;
TextView locationText;
TextView timeText;
MapView map;
GoogleMap gMap;
MyViewHolder(View itemView) {
super(itemView);
map = (MapView)itemView.findViewById(R.id.mapFragment);
strText = (TextView)itemView.findViewById(R.id.strText);
locationText = (TextView)itemView.findViewById(R.id.locationText);
timeText = (TextView)itemView.findViewById(R.id.timeText);
if (map != null)
{
map.onCreate(null);
map.onResume();
map.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(mContext);
gMap = googleMap;
}
}
在那裏我有初始化和設置位置來解決這個問題