简体   繁体   English

单击回收站视图上的项目后无法重新聚焦到地图

[英]Can't focus back to maps after clicking an item on recycler view

Im trying to display items over maps fragment using recycler view.我试图使用回收器视图在地图片段上显示项目。 Recycler view is defined inside the map fragment.回收站视图在 map 片段内定义。 When I click an item from the recycler view I reset adapter data by calling listname.clear() and adapter.notifyDataSetChanged().当我从回收器视图中单击一个项目时,我通过调用 listname.clear() 和 adapter.notifyDataSetChanged() 来重置适配器数据。 The recycler view clears but I can't move my map again.回收站视图已清除,但我无法再次移动我的 map。

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:descendantFocusability="blocksDescendants"
android:layout_height="match_parent"
tools:context=".Nearby" >

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/mapConstraint"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/selectListBtn"
        android:layout_width="368dp"
        android:layout_height="56dp"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="7dp"
        android:background="@color/design_default_color_primary_dark"
        android:text="Select Your List"
        android:textColor="#FFFFFF"
        map:layout_constraintBottom_toTopOf="@+id/selectListRecycler"
        map:layout_constraintEnd_toEndOf="parent"
        map:layout_constraintStart_toStartOf="parent"
        map:layout_constraintTop_toTopOf="parent" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/selectListRecycler"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="59dp"
        map:layout_constraintBottom_toBottomOf="parent"
        map:layout_constraintEnd_toEndOf="parent"
        map:layout_constraintStart_toStartOf="parent"
        map:layout_constraintTop_toBottomOf="@+id/selectListBtn" />
</androidx.constraintlayout.widget.ConstraintLayout>

Somehow I managed to regain the focus on map by setting visibility of recyclerview to View.GONE, but the button comes to middle of the fragment and alignment goes wrong.不知何故,我设法通过将 recyclerview 的可见性设置为 View.GONE 来重新关注 map,但是按钮位于片段的中间,并且 alignment 出错了。

recyclerView = (RecyclerView) rootView.findViewById(R.id.selectListRecycler);
                ArrayList<TList> tempList = tLists;
                parent = (ViewGroup) recyclerView.getParent();
                //RecyclerView tempView = (RecyclerView) rootView.findViewById(R.id.selectListRecycler);
                adapter = new TListRecyclerAdapter(tempList);

                recyclerView.setHasFixedSize(false);
                recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                recyclerView.setAdapter(adapter);

                recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getContext(),
                        recyclerView, new ClickListener() {
                    @Override
                    public void onClick(View view, final int position) {
                        selectedList = tLists.get(position);
                        tempList.clear();
                        adapter.notifyDataSetChanged();

                    }

                    @Override
                    public void onLongClick(View view, int position) {
                        //Toast.makeText(Nearby.this, "Long press on position :"+position,
                        //        Toast.LENGTH_LONG).show();
                        String x="";
                    }
                }));

Before clicking select list点击前 select 列表

After clicking select list点击 select 列表后

After setting recyclerview visibility to GONE将 recyclerview 可见性设置为 GONE 后

Option - 1: Simply remove map:layout_constraintBottom_toTopOf="@+id/selectListRecycler" from Button选项 - 1:只需从Button中删除map:layout_constraintBottom_toTopOf="@+id/selectListRecycler"

<Button
    android:id="@+id/selectListBtn"
    android:layout_width="368dp"
    android:layout_height="56dp"
    android:layout_marginStart="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="7dp"
    android:background="@color/design_default_color_primary_dark"
    android:text="Select Your List"
    android:textColor="#FFFFFF"
    map:layout_constraintEnd_toEndOf="parent"
    map:layout_constraintStart_toStartOf="parent"
    map:layout_constraintTop_toTopOf="parent" />

Option - 2: Instead of androidx.constraintlayout.widget.ConstraintLayout use LinearLayout with orientation vertical.选项 - 2:代替androidx.constraintlayout.widget.ConstraintLayout使用具有垂直方向的LinearLayout

android:orientation="vertical"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM