2

我试图让它运行,我唯一得到的是一个灰色网格,左下角有一个谷歌标志,代替片段。地图旨在显示在 FirebaseRecyclerAdapter 加载的 CardView 中。没有抛出错误并且 API 密钥是正确的。当我快速上下滚动项目几次时,地图似乎在每次滚动时加载缓慢。当没有滚动时,什么都没有加载。

卡片的 XML 是:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="1dp"
    card_view:cardCornerRadius="0dp"
    android:layout_marginLeft="-3dp"
    android:layout_marginRight="-3dp"
    android:id="@+id/cv">
    <com.google.android.gms.maps.MapView 
                class="com.google.android.gms.maps.MapFragment"
                android:id="@+id/mapLite"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                map:mapType="normal"
                map:liteMode="true"/>
    </android.support.v7.widget.CardView>
</LinearLayout>

Firebase 适配器的代码如下:

adapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.card_view_item, PostViewHolder.class, ref.child("message").child(currentGroupKey)) {

    @Override
    protected void populateViewHolder(final PostViewHolder postViewHolder, final Post post, int position) {
        super.populateViewHolder(postViewHolder, post, position);
    }
};

PostViewHolder 的代码:

public class PostViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback {
    public MapView mapView;


    public PostViewHolder(View itemView) {
       super(itemView);

        mapView = (MapView) itemView.findViewById(R.id.mapLite);

        mapView.onCreate(null);
        mapView.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        CameraUpdate updateCamera = CameraUpdateFactory.newCameraPosition(new CameraPosition(new LatLng(40.712784, -74.005941), 10, 0f, 0));
        googleMap.moveCamera(updateCamera);
    }
}

有谁知道可能是什么问题?

4

1 回答 1

2

我找到了答案!问题是 XML 中的名称空间错误。我通过研究这个例子找到了解决方案:https ://github.com/saxman/android-map_list 。贝娄我发布了上面显示的正确版本的文件,以防有人遇到类似问题:

卡的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:cardUseCompatPadding="true"
    card_view:cardElevation="1dp"
    card_view:cardCornerRadius="0dp"
    android:layout_marginLeft="-3dp"
    android:layout_marginRight="-3dp"
    android:id="@+id/cv">
            <com.google.android.gms.maps.MapView
                android:name="com.google.android.gms.maps.MapFragment"
                android:id="@+id/mapLite"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                map:liteMode="true"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

Firebase 适配器的代码:

adapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(Post.class, R.layout.card_view_item, PostViewHolder.class, ref.child("message").child(currentGroupKey)) {
    @Override
    public void onBindViewHolder(PostViewHolder viewHolder, int position) {
        super.onBindViewHolder(viewHolder, position);
        viewHolder.setMapLocation(52.235474, 21.004057);
    }

    @Override
    protected void populateViewHolder(final PostViewHolder postViewHolder, final Post post, int position) {
        super.populateViewHolder(postViewHolder, post, position);
    }
};

和 PostViewHolder:

    public class PostViewHolder extends RecyclerView.ViewHolder {
    public MapView mapView;

    protected GoogleMap mGoogleMap;
    protected LatLng mMapLocation;

    public PostViewHolder(final View itemView) {
        super(itemView);
        mapView = (MapView) itemView.findViewById(R.id.mapLite);

        mapView.onCreate(null);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {

                mGoogleMap = googleMap;
                MapsInitializer.initialize(itemView.getContext());
                googleMap.getUiSettings().setMapToolbarEnabled(true);
                if (mMapLocation != null) {
                    updateMapContents();
                }
            }
        });
    }

    public void setMapLocation(double lat, double lon) {
        mMapLocation = new LatLng(lat, lon);

        if (mGoogleMap != null) {
            updateMapContents();
        }
    }

    protected void updateMapContents() {
        mGoogleMap.clear();
        // Update the mapView feature data and camera position.
        mGoogleMap.addMarker(new MarkerOptions().position(mMapLocation));
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(mMapLocation, 10f);
        mGoogleMap.moveCamera(cameraUpdate);
    }
}
于 2016-01-17T14:25:15.547 回答