【发布时间】:2019-10-12 00:48:12
【问题描述】:
我正在 Android Studio 中构建一个聊天应用程序,以便在地图上发送路线。我通过使用 RecyclerView 和 ViewHolders 实现了这一点,它们在 lite 模式下使用 MapViews (map:liteMode="true") 保存必要的 UI。问题是,当我将地图 UI 元素添加到 recyclerView 并使用 scrollToPosition(adapter.getItemCount() - 1) 滚动到最后时,滚动无法跟随视图并且总是略微偏离,如屏幕截图所示
(https://i.postimg.cc/BvMzrHJL/Screenshot-20190526-005535.png)。
此外,键盘在单击时也会对视图的高度感到困惑 (https://i.postimg.cc/Hs6BsHfR/Screenshot-20190526-011647.png)。
我曾尝试关闭 lite 模式,但由于我的 MapViews 在 ViewHolders 中,而不是在 Activities 或 Fragments 中,这会使滚动变得迟缓,并且处理生命周期事件成为一个问题,请参阅官方文档:
这个类[MapView]的用户必须转发所有的生命周期方法 包含此视图的 Activity 或 Fragment 到中的相应视图 这节课。
我也尝试将布局的高度从 android:layout_height="wrap_content" 更改为 android:layout_height="250dp",但这也根本不起作用。
此外,对于仅包含文本或空的 RelativeLayout 而不是 MapView 的视图,滚动也能正常工作。
我使用了来自 Google 开发者文档https://github.com/googlemaps/android-samples/blob/master/ApiDemos/java/app/src/main/java/com/example/mapdemo/LiteListDemoActivity.java的这个示例
这是我的 ViewHolder(两个之一):
private class SentRouteViewHolder extends RecyclerView.ViewHolder implements OnMapReadyCallback
{
MapView sentMap;
TextView routeSentTime;
GoogleMap map;
public SentRouteViewHolder(@NonNull View itemView) {
super(itemView);
sentMap = itemView.findViewById(R.id.sent_map);
routeSentTime = itemView.findViewById(R.id.route_sent_time);
sentMap.onCreate(null);
sentMap.onResume();
sentMap.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getApplicationContext());
map = googleMap;
setMapRoute();
}
void bind(Message message)
{
sentMap.setTag(message);
setMapRoute();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
routeSentTime.setText(simpleDateFormat.format(message.getTime()));
}
void setMapRoute()
{
if(map == null) return;
Message message = (Message) sentMap.getTag();
if(message==null) return;
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
}
并将项目添加到 RecyclerView:
activeCollection.add(newMessage).addOnSuccessListener(documentReference -> {
documentReference.get().addOnSuccessListener(documentSnapshot -> {
adapter.addMessage(documentSnapshot);
adapter.notifyItemInserted(adapter.getItemCount());
chatReycler.scrollToPosition(adapter.getItemCount()-1);
});
});
onBindViewHolder:
SentRouteViewHolder routeViewHolder = (SentRouteViewHolder) viewHolder;
routeViewHolder.bind(message);
onCreateViewHolder:
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.route_sent,parent,false);
Log.v("measure",String.valueOf(v.getMeasuredHeight()));
return new SentRouteViewHolder(v);
RecyclerView 配置:
manager.setStackFromEnd(true);
chatReycler.setLayoutManager(manager);
chatReycler.setAdapter(adapter);
chatReycler.setHasFixedSize(false);
chatReycler.setRecyclerListener(viewHolder -> {
if(viewHolder.getItemViewType()==ChatRecyclerViewAdapter.VIEW_TYPE_ROUTE_RECEIVED)
{
ChatRecyclerViewAdapter.ReceivedRouteViewHolder holder = (ChatRecyclerViewAdapter.ReceivedRouteViewHolder) viewHolder;
if(holder.map!=null)
{
holder.map.clear();
holder.map.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}
else if (viewHolder.getItemViewType()==ChatRecyclerViewAdapter.VIEW_TYPE_ROUTE_SENT)
{
ChatRecyclerViewAdapter.SentRouteViewHolder holder = (ChatRecyclerViewAdapter.SentRouteViewHolder) viewHolder;
if(holder.map!=null)
{
holder.map.clear();
holder.map.setMapType(GoogleMap.MAP_TYPE_NONE);
}
}
});
ViewHolder XML 文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:paddingBottom="3dp"
android:layout_marginBottom="13dp">
<ImageView
android:id="@+id/route_received_background"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginStart="15dp"
android:src="@drawable/message_received_background"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.gms.maps.MapView
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="@+id/received_map"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="9dp"
app:layout_constraintBottom_toBottomOf="@+id/route_received_background"
app:layout_constraintEnd_toEndOf="@+id/route_received_background"
app:layout_constraintStart_toStartOf="@+id/route_received_background"
app:layout_constraintTop_toTopOf="@+id/route_received_background"
map:mapType="normal"
map:liteMode="true"
/>
<TextView
android:id="@+id/route_received_time"
style="@style/TextAppearance.MaterialComponents.Caption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="6dp"
android:textSize="13sp"
app:layout_constraintBottom_toBottomOf="@+id/route_received_background"
app:layout_constraintStart_toEndOf="@+id/route_received_background"
tools:text="11:50" />
</android.support.constraint.ConstraintLayout>
我希望 RecyclerView 滚动到发送地图的底部,而不是中间。我怎样才能做到这一点?
【问题讨论】:
标签: java android google-maps android-recyclerview android-viewholder