Android 无法在RecyclerView内以LiteMode显示MapView
我有一个来自服务器的保存位置列表,我想在一个带有位置地图快照的RecyclerView中显示这些位置。 我根据谷歌提供的代码示例对其进行了编码,但不幸的是,该列表不会显示在用户界面上。 我正在将代码附加到我的RecyclerView适配器和项目XMLAndroid 无法在RecyclerView内以LiteMode显示MapView,android,android-recyclerview,google-maps-android-api-2,android-mapview,Android,Android Recyclerview,Google Maps Android Api 2,Android Mapview,我有一个来自服务器的保存位置列表,我想在一个带有位置地图快照的RecyclerView中显示这些位置。 我根据谷歌提供的代码示例对其进行了编码,但不幸的是,该列表不会显示在用户界面上。 我正在将代码附加到我的RecyclerView适配器和项目XML public class SavedLocationAdapter extends RecyclerView.Adapter<SavedLocationAdapter.ViewHolder> { private Context mCo
public class SavedLocationAdapter extends RecyclerView.Adapter<SavedLocationAdapter.ViewHolder> {
private Context mContext;
private ArrayList<SavedLocationDetails> savedLocationList;
private LoadMoreLocationsInterface loadMoreLocationsInterface;
public SavedLocationAdapter(Context context, LoadMoreLocationsInterface loadMoreLocationsInterface) {
//super(DIFF_CALLBACK);
super();
mContext = context;
this.savedLocationList = new ArrayList<>();
this.loadMoreLocationsInterface = loadMoreLocationsInterface;
}
public void updateList(List<SavedLocationDetails> savedLocationList) {
this.savedLocationList.addAll(savedLocationList);
notifyDataSetChanged();
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_saved_location, parent, false));
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
SavedLocationDetails savedLocationDetails = savedLocationList.get(position);
if (savedLocationDetails != null)
holder.bindView(savedLocationDetails);
if (position == getItemCount()-1)
loadMoreLocationsInterface.loadMoreLocations(savedLocationDetails.getLid());
}
@Override
public int getItemCount() {
return savedLocationList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
MapView mapView;
TextView locationType;
TextView friendlyName;
TextView fullAddress;
public GoogleMap map;
View layout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
layout = itemView;
locationType = layout.findViewById(R.id.savedLocationTypeTV);
fullAddress = layout.findViewById(R.id.savedLocationAddressTV);
friendlyName = layout.findViewById(R.id.friendlyNameTV);
mapView = layout.findViewById(R.id.savedLocationMap);
if(mapView != null) {
mapView.onCreate(null);
mapView.onResume();
mapView.onPause();
mapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(mContext);
map = googleMap;
setMapLocation();
}
private void setMapLocation() {
if (map == null)
return;
SavedLocationDetails savedLocationDetails = (SavedLocationDetails) mapView.getTag();
if (savedLocationDetails == null)
return;
LatLng locationLatLng = new LatLng(savedLocationDetails.getLat(), savedLocationDetails.getLng());
BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.location_marker_black_solid);
MarkerOptions markerOptions = (new MarkerOptions()).position(locationLatLng).icon(icon);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(locationLatLng, 18f));
map.addMarker(markerOptions);
}
public void bindView(SavedLocationDetails savedLocationDetails) {
layout.setTag(this);
mapView.setTag(savedLocationDetails);
setMapLocation();
if((savedLocationDetails.getLtp() != null) && (savedLocationDetails.getLtp() == 3)) {
locationType.setVisibility(GONE);
friendlyName.setText(savedLocationDetails.getFn());
friendlyName.setVisibility(View.VISIBLE);
} else {
friendlyName.setVisibility(GONE);
locationType.setVisibility(View.VISIBLE);
locationType.setText("Home");
}
fullAddress.setText(savedLocationDetails.getAdd());
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardBackgroundColor="@color/appBar"
app:cardElevation="3dp"
app:cardCornerRadius="30dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.gms.maps.MapView
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/savedLocationMap"
android:layout_width="match_parent"
android:layout_height="225dp"
app:mapType="normal"
map:liteMode="true"
map:mapType = "normal" />
<RelativeLayout
android:id="@+id/savedLocationDataRL"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignBottom="@+id/savedLocationMap"
android:background="@color/white"
android:visibility="gone">
<TextView
android:id="@+id/savedLocationTypeTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Work"
android:textColor="@color/quantum_black_100"
android:textStyle="bold"
android:textSize="15sp"/>
<TextView
android:id="@+id/friendlyNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Friendly Name"
android:textColor="@color/quantum_black_100"
android:textSize="15sp"
android:textStyle="bold"
android:visibility="gone"/>
<TextView
android:id="@+id/savedLocationAddressTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/savedLocationTypeTV"
android:layout_marginTop="3dp"
android:layout_marginStart="10dp"
android:text="Full Address"
android:textColor="@color/quantum_black_100"
android:textSize="13sp"/>
</RelativeLayout>
</RelativeLayout>
</androidx.cardview.widget.CardView>
公共类SavedLocationAdapter扩展了RecyclerView.Adapter{
私有上下文;
私有ArrayList savedLocationList;
专用LoadMoreLocationsInterface LoadMoreLocationsInterface;
public SavedLocationAdapter(上下文上下文,LoadMoreLocationsInterface LoadMoreLocationsInterface){
//超级(差异回调);
超级();
mContext=上下文;
this.savedLocationList=新建ArrayList();
this.loadMoreLocationsInterface=loadMoreLocationsInterface;
}
公共无效更新列表(列表保存位置列表){
this.savedLocationList.addAll(savedLocationList);
notifyDataSetChanged();
}
@非空
@凌驾
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent,int viewType){
返回新的ViewHolder(LayoutInflater.from(parent.getContext())。充气(R.layout.item_saved_location,parent,false));
}
@凌驾
public void onBindViewHolder(@NonNull ViewHolder,int位置){
SavedLocationDetails SavedLocationDetails=savedLocationList.get(位置);
if(savedLocationDetails!=null)
holder.bindView(保存位置详细信息);
if(position==getItemCount()-1)
loadMoreLocationsInterface.loadMoreLocations(savedLocationDetails.getLid());
}
@凌驾
public int getItemCount(){
返回savedLocationList.size();
}
公共类ViewHolder扩展了RecyclerView.ViewHolder在MapReadyCallback上实现{
地图视图;
文本视图位置类型;
TextView friendlyName;
文本视图完整地址;
公共谷歌地图;
视图布局;
公共视图持有者(@NonNull View itemView){
超级(项目视图);
布局=项目视图;
locationType=layout.findViewById(R.id.savedLocationTypeTV);
fullAddress=layout.findviewbyd(R.id.savedLocationAddressTV);
friendlyName=layout.findViewById(R.id.friendlyNameTV);
mapView=layout.findViewById(R.id.savedLocationMap);
如果(地图视图!=null){
onCreate(空);
onResume();
onPause();
getMapAsync(这个);
}
}
@凌驾
4月1日公开作废(谷歌地图谷歌地图){
初始化(mContext);
地图=谷歌地图;
setMapLocation();
}
私有void setMapLocation(){
if(map==null)
返回;
SavedLocationDetails SavedLocationDetails=(SavedLocationDetails)mapView.getTag();
if(savedLocationDetails==null)
返回;
LatLng locationLatLng=新LatLng(savedLocationDetails.getLat(),savedLocationDetails.getLng());
BitmapDescriptor图标=BitmapDescriptorFactory.fromResource(R.drawable.location\u marker\u black\u solid);
MarkerOptions MarkerOptions=(新MarkerOptions()).position(locationLatLng).icon(icon);
map.animateCamera(CameraUpdateFactory.newLatLngZoom(locationLatLng,18f));
map.addMarker(markerOptions);
}
公共void bindView(SavedLocationDetails SavedLocationDetails){
布局.setTag(本);
setTag(savedLocationDetails);
setMapLocation();
if((savedLocationDetails.getLtp()!=null)和&(savedLocationDetails.getLtp()=3)){
locationType.setVisibility(消失);
friendlyName.setText(savedLocationDetails.getFn());
设置可见性(View.VISIBLE);
}否则{
friendlyName.setVisibility(消失);
locationType.setVisibility(View.VISIBLE);
locationType.setText(“主页”);
}
setText(savedLocationDetails.getAdd());
}
}
}
嘿,我也面临同样的问题,但我成功了,你找到了解决方案吗?还是我应该发布答案?如果您仍然需要答案,您可以共享XML吗?@MiguelHincapieC它从一开始就在工作。。只是我没有启用我的回收器视图。。