CardView中的MapView仅在单击时更新

时间:2016-06-28 13:26:35

标签: java android google-maps android-recyclerview android-cardview

我在RecyclerView中有几张不同的卡片。一张卡包含一个MapView:

<com.google.android.gms.maps.MapView
            android:id="@+id/map_card_pickup"
            android:layout_width="match_parent"
            android:layout_height="200dp" />

在适配器中,我按如下方式填充MapView(摘录):

public class myCarAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements OnMapReadyCallback {
public static class GeneralViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public Button mShareButton;

    public GeneralViewHolder(View v) {
        super(v);
        mV = v;
        mShareButton = (Button)v.findViewById(R.id.button_share);
    }
}

public static class ViewHolderPickup extends GeneralViewHolder {
    public Button mLinkButton;
    public TextView mInfoText;
    public MapView mMap;

    public ViewHolderPickup(View v) {
        super(v);
        mLinkButton = (Button)v.findViewById(R.id.button_link_pickup);
        mInfoText = (TextView)v.findViewById(R.id.textView_info_pickup);
        mMap = (MapView)v.findViewById(R.id.map_card_pickup);
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v;

    switch(viewType) {
        case 1:
            v = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.fragment_my_car_card_pickup, parent, false);

            ViewHolderPickup viewHolderPickup = new ViewHolderPickup(v);
            viewHolderPickup.mLinkButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((FragmentActivity)mV.getContext()).getSupportFragmentManager().beginTransaction()
                            .replace(R.id.content_main, new PickupFragment())
                            .commit();
                }
            });
            viewHolderPickup.mMap.onCreate(null);
            viewHolderPickup.mMap.getMapAsync(this);

            return viewHolderPickup;
    }

    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    ((GeneralViewHolder)holder).mShareButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast toast = Toast.makeText(mV.getContext(), R.string.cards_shared, Toast.LENGTH_SHORT);
            toast.show();
        }
    });

    switch (mDataset.get(position).type) {
        case PICKUP:
            ((ViewHolderPickup)holder).mInfoText.setText(mDataset.get(position).msg);
            break;
    }
}

@Override
public void onMapReady(GoogleMap map) {
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new
            LatLng(51.0107214,6.9549693), 10));
    map.addMarker(new MarkerOptions()
            .position(new LatLng(51.0107214, 6.9549693)));


 }
}

使用上面的代码,MapView向我展示了一个网格。只有当我点击Mapview时,它才会更新其内容。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,我使用Lite的{​​{1}}版本解决了这个问题:

MapView

这个GitHub项目非常有用:https://github.com/saxman/android-map_list

希望它有所帮助。