精简模式下的MapView导致RecyclerView无法正确滚动

时间:2019-05-25 22:22:02

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

我正在Android Studio中构建一个聊天应用程序,目的是在地图上发送路线。我已经通过使用RecyclerView和ViewHolders实现了这一点,它们在精简模式(map:liteMode="true")中使用MapViews保留了必要的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)时键盘也对视图的高度感到困惑。

我曾尝试关闭精简模式,但由于我的MapView在ViewHolders中,而不在Activity或Fragments中,因此滚动延迟和处理生命周期事件成为一个问题,请参阅官方文档:

  

此类[MapView]的用户必须转发来自   包含此视图的活动或片段到其中的相应视图   这个班。

我还尝试将布局的高度从android:layout_height =“ wrap_content”更改为android:layout_height =“ 250dp”,但这也根本没有用。

此外,滚动仅适用于仅包含文本或空白的RelativeLayout而非MapView的View。

我使用了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滚动到已发送地图的底部,而不是滚动到其中部。我该如何实现?

1 个答案:

答案 0 :(得分:0)

我在滚动时遇到了类似的问题,并检查了父级布局的高度为0dp。我将其更改为match_parent,并且有效。

android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"