我正在使用Google地图片段在RecyclerView中显示地图。我使用此代码显示Google地图:
SupportMapFragment supportMapFragment =
SupportMapFragment.newInstance();
supportFragment.beginTransaction().replace(R.id.map_container,
supportMapFragment).commit();
但问题是它只在地图容器的第一个元素中显示地图。 RecyclerView中的框架布局,但不是全部,如果我滚动RecyclerView,那么第一个元素也会消失。出于测试目的,我尝试更改地图容器的背景资源,它适用于所有元素但是如何在所有元素中显示地图片段?
适配器
public class PrivateDialogMessagesAdapter extends BaseDialogMessagesAdapter implements OnMapReadyCallback {
private static int EMPTY_POSITION = -1;
Context context;
GoogleMap map;
FragmentManager supportFragment;
private int lastRequestPosition = EMPTY_POSITION;
private int lastInfoRequestPosition = EMPTY_POSITION;
private FriendOperationListener friendOperationListener;
public PrivateDialogMessagesAdapter(
BaseActivity baseActivity,
FriendOperationListener friendOperationListener,
List<CombinationMessage> objectsList,
ChatUIHelperListener chatUIHelperListener,
Dialog dialog,
Context context,
FragmentManager fragmentManager) {
super(baseActivity, objectsList);
this.friendOperationListener = friendOperationListener;
this.chatUIHelperListener = chatUIHelperListener;
this.dialog = dialog;
this.context = context;
this.supportFragment = fragmentManager;
}
@Override
public PrivateDialogMessagesAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
switch (viewType) {
case TYPE_REQUEST_MESSAGE:
return new ViewHolder(this, layoutInflater.inflate(R.layout.item_friends_notification_message, viewGroup, false));
case TYPE_OWN_MESSAGE:
// MapFragment mapFragment = (MapFragment) ((Activity)context).getFragmentManager().findFragmentById(R.id.location_message_own);
//mapFragment.getMapAsync(this);
return new ViewHolder(this, layoutInflater.inflate(R.layout.item_message_own, viewGroup, false));
case TYPE_OPPONENT_MESSAGE:
return new ViewHolder(this, layoutInflater.inflate(R.layout.item_private_message_opponent, viewGroup, false));
default:
return null;
}
}
@Override
public void onBindViewHolder(BaseClickListenerViewHolder<CombinationMessage> baseClickListenerViewHolder, int position) {
CombinationMessage combinationMessage = getItem(position);
boolean ownMessage = !combinationMessage.isIncoming(currentUser.getId());
ViewHolder viewHolder = (ViewHolder) baseClickListenerViewHolder;
boolean friendsRequestMessage = DialogNotification.Type.FRIENDS_REQUEST.equals(
combinationMessage.getNotificationType());
boolean friendsInfoRequestMessage = combinationMessage
.getNotificationType() != null && !friendsRequestMessage;
if (viewHolder.verticalProgressBar != null) {
viewHolder.verticalProgressBar.setProgressDrawable(baseActivity.getResources().getDrawable(R.drawable.vertical_progressbar));
}
if (friendsRequestMessage) {
viewHolder.messageTextView.setText(combinationMessage.getBody());
viewHolder.timeTextMessageTextView.setText(DateUtils.formatDateSimpleTime(combinationMessage.getCreatedDate()));
setVisibilityFriendsActions(viewHolder, View.GONE);
} else if (friendsInfoRequestMessage) {
viewHolder.messageTextView.setText(combinationMessage.getBody());
viewHolder.timeTextMessageTextView.setText(DateUtils.formatDateSimpleTime(combinationMessage.getCreatedDate()));
setVisibilityFriendsActions(viewHolder, View.GONE);
lastInfoRequestPosition = position;
} else if (combinationMessage.getAttachment() != null) {
resetUI(viewHolder);
setViewVisibility(viewHolder.progressRelativeLayout, View.VISIBLE);
viewHolder.timeAttachMessageTextView.setText(DateUtils.formatDateSimpleTime(combinationMessage.getCreatedDate()));
if (ownMessage && combinationMessage.getState() != null) {
setMessageStatus(viewHolder.attachDeliveryStatusImageView, State.DELIVERED.equals(
combinationMessage.getState()), State.READ.equals(combinationMessage.getState()));
}
displayAttachImageById(combinationMessage.getAttachment().getAttachmentId(), viewHolder);
} else if (combinationMessage.getBody().toString().equals("no")) {
resetUI(viewHolder);
setViewVisibility(viewHolder.ownLocationMessageContainer, View.VISIBLE);
viewHolder.ownLocationMessageContainer.setBackgroundColor(context.getResources().getColor(R.color.main_color));
SupportMapFragment supportMapFragment = SupportMapFragment.newInstance();
supportFragment.beginTransaction().replace(R.id.map_container, supportMapFragment).commit();
} else {
resetUI(viewHolder);
setViewVisibility(viewHolder.textMessageView, View.VISIBLE);
viewHolder.messageTextView.setText(combinationMessage.getBody());
viewHolder.timeTextMessageTextView.setText(DateUtils.formatDateSimpleTime(combinationMessage.getCreatedDate()));
if (ownMessage && combinationMessage.getState() != null) {
setMessageStatus(viewHolder.messageDeliveryStatusImageView, State.DELIVERED.equals(
combinationMessage.getState()), State.READ.equals(combinationMessage.getState()));
} else if (ownMessage && combinationMessage.getState() == null) {
//viewHolder.messageDeliveryStatusImageView.setImageResource(android.R.color.transparent);
}
}
if (!State.READ.equals(combinationMessage.getState()) && !ownMessage && baseActivity.isNetworkAvailable()) {
combinationMessage.setState(State.READ);
QBUpdateStatusMessageCommand.start(baseActivity, ChatUtils.createQBDialogFromLocalDialog(dataManager, dialog), combinationMessage, true);
}
// check if last messageCombination is request messageCombination
boolean lastRequestMessage = (position == getAllItems().size() - 1 && friendsRequestMessage);
if (lastRequestMessage) {
findLastFriendsRequest();
}
// check if friend was rejected/deleted.
if (lastRequestPosition != EMPTY_POSITION && lastRequestPosition < lastInfoRequestPosition) {
lastRequestPosition = EMPTY_POSITION;
} else if ((lastRequestPosition != EMPTY_POSITION && lastRequestPosition == position)) { // set visible friends actions
setVisibilityFriendsActions(viewHolder, View.VISIBLE);
initListeners(viewHolder, position, combinationMessage.getDialogOccupant().getUser().getUserId());
}
}
@Override
public int getItemViewType(int position) {
return getItemViewType(getItem(position));
}
public void clearLastRequestMessagePosition() {
lastRequestPosition = EMPTY_POSITION;
}
public void findLastFriendsRequestMessagesPosition() {
new FindLastFriendsRequestThread().run();
}
private void setVisibilityFriendsActions(ViewHolder viewHolder, int visibility) {
setViewVisibility(viewHolder.acceptFriendImageView, visibility);
setViewVisibility(viewHolder.dividerView, visibility);
setViewVisibility(viewHolder.rejectFriendImageView, visibility);
}
private void initListeners(ViewHolder viewHolder, final int position, final int userId) {
viewHolder.acceptFriendImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
friendOperationListener.onAcceptUserClicked(position, userId);
}
});
viewHolder.rejectFriendImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
friendOperationListener.onRejectUserClicked(position, userId);
}
});
}
private void findLastFriendsRequest() {
for (int i = 0; i < getAllItems().size(); i++) {
findLastFriendsRequest(i, getAllItems().get(i));
}
}
private void findLastFriendsRequest(int position, CombinationMessage combinationMessage) {
boolean ownMessage;
boolean friendsRequestMessage;
boolean isFriend;
if (combinationMessage.getNotificationType() != null) {
ownMessage = !combinationMessage.isIncoming(currentUser.getId());
friendsRequestMessage = DialogNotification.Type.FRIENDS_REQUEST.equals(
combinationMessage.getNotificationType());
if (friendsRequestMessage && !ownMessage) {
isFriend = dataManager.getFriendDataManager().
getByUserId(combinationMessage.getDialogOccupant().getUser().getUserId()) != null;
if (!isFriend) {
lastRequestPosition = position;
}
}
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng sydney = new LatLng(-33.867, 151.206);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
googleMap.setMyLocationEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
googleMap.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
}
private class FindLastFriendsRequestThread extends Thread {
@Override
public void run() {
findLastFriendsRequest();
}
}
}
XML布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="horizontal"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="vertical">
<include layout="@layout/view_chat_progress_bars" />
<include layout="@layout/view_chat_text_message_own" />
<include layout="@layout/view_chat_attach_message_own" />
<include layout="@layout/view_location_message_own" />
</LinearLayout>
</FrameLayout>
XML布局二
<FrameLayout android:layout_height="100dp"
android:layout_width="100dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_location_message_own"
android:background="@color/red">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map_container" />
</FrameLayout>