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

问题描述 投票:1回答:1

我正在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的视图,滚动也可以正常工作。

我使用了来自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
1个回答
0
投票

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

android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
© www.soinside.com 2019 - 2024. All rights reserved.