我是Android开发方面的新手,我在RecyclerView内部的Cardview中获得了Google地图的奇怪行为。起初,Google地图显示灰色背景,但是当我点击地图时,它会被部分加载。
有什么建议吗?提前致谢,对不起我的英语。
NotificationAdapter.java
public class NotificationAdapter extends FirebaseRecyclerAdapter<FirebaseDBNotification, NotificationAdapter.NotificationViewHolder> {
private static Context context;
public NotificationAdapter(Class<FirebaseDBNotification> modelClass, @LayoutRes int modelLayout, Class<NotificationViewHolder> viewHolderClass, Query query, Context context) {
super(modelClass, modelLayout, viewHolderClass, query);
this.context = context;
}
@Override
protected void populateViewHolder(NotificationViewHolder viewHolder, FirebaseDBNotification model, int position) {
viewHolder.onBindViewHolder(model,context);
}
public static class NotificationViewHolder extends RecyclerView.ViewHolder {
TextView textViewLevelName,textViewLevelNumber;
CardView cardView;
MapView mapView;
GoogleMap gmap;
String position;
public NotificationViewHolder(View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.card_view);
textViewLevelName = itemView.findViewById(R.id.levelName);
textViewLevelNumber = itemView.findViewById(R.id.levelNumber);
mapView = itemView.findViewById(R.id.mapLite);
}
public void onBindViewHolder(FirebaseDBNotification model,Context context) {
textViewLevelName.setText("Detected " + model.getLevelName().replace('_',' ') +" air quality alert");
textViewLevelNumber.setText("AQI level " + model.getLevelNumber());
position = model.getPosition();
mapView.onCreate(null);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
if(gmap==null) {
gmap = googleMap;
MapsInitializer.initialize(itemView.getContext());
String[] positionArray = position.split(",");
double lon = Double.parseDouble(positionArray[0]);
double lat = Double.parseDouble(positionArray[1]);
LatLng position = new LatLng(lat, lon);
gmap.addMarker(new MarkerOptions().position(position).title("Hello world"));
CameraPosition camPos = new CameraPosition.Builder()
.target(position)
.zoom(15f)
.build();
gmap.animateCamera(CameraUpdateFactory.newCameraPosition(camPos));
}
}
});
}
}
}
清单
<permission
android:name="com.uca.ii.pfc.awarenessqualityair.activities.permission.MAPS_RECEIVE"
android:protectionLevel="signature" >
</permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" />
<uses-permission android:name="com.google.android.gms.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.uca.ii.pfc.awarenessqualityair.activities.permission.MAPS_RECEIVE"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
(INSIDE <application>)
<meta-data android:name="com.google.android.geo.API_KEY" android:value="API_KEY"/>
fragment_notification.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/notificationItemLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible"
app:cardCornerRadius="2dp"
app:cardElevation="1dp"
app:contentPadding="5dp">
<LinearLayout
android:id="@+id/layout123"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.gms.maps.MapView
android:id="@+id/mapLite"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="180dp" />
<TextView
android:id="@+id/levelName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/levelNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
没有点击
点击一下
两次点击
我遗失了一些东西,但我不知道是什么。