android - 弹出菜单显示时工具栏滚动

标签 android android-recyclerview android-toolbar android-cardview android-coordinatorlayout

我有一个带 AppbarLayout 的 CoordinatorLayout,它包括 Toolbar 和 TabLayout,还包括显示 3 个 fragment 的 ViewPager,其中包括 CardView 的 RecyclerView。

问题是在 CardView 上单击 ImageView(3 个点)时弹出菜单显示,但工具栏向上滚动。

this is the design with toolbar showing

the issue when clicking then toolbar disappears

这是我的容器 Activity xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="match_parent"
    android:fitsSystemWindows="true"
    android:id="@+id/coordinatorLayout">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add" />

</android.support.design.widget.CoordinatorLayout>

这是卡片 View xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_margin="3dp"
        card_view:cardBackgroundColor="@color/cardview_light_background">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/thumbnail"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1.5"
                android:scaleType="fitCenter" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="5"
                android:layout_margin="5dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/time_at"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#616161"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/alarm_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#616161"
                    android:textSize="15sp"/>

                <TextView
                    android:id="@+id/alarm_message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#616161"
                    android:textSize="15sp"/>

            </LinearLayout>

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_margin="5dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/remaining_time"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_weight="2"
                    android:textSize="12sp"
                    android:textColor="#03A9F4"/>

                <ImageView
                    android:id="@+id/overFlow"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|end"
                    android:src="@drawable/ic_more_vert"/>

            </LinearLayout>

        </LinearLayout>

    </android.support.v7.widget.CardView>

</LinearLayout>

这是 fragment xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TodayFragment">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#EEEEEE"
        tools:context=".TodayFragment">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view_today"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:scrollbars="vertical" />

    </RelativeLayout>

</LinearLayout>

最佳答案

替换

PopupMenu popupMenu = new PopupMenu(MainActivity.this,view);

PopupMenu popupMenu = new PopupMenu(MainActivity.this,view, Gravity.RIGHT);

会帮助你。

关于android - 弹出菜单显示时工具栏滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39412133/

相关文章:

android - 以编程方式 "swipe-to-dismiss"ListView/RecyclerView 中的项目?

android - 在androidx.recyclerview.widget.RecyclerView上找不到参数类型为boolean的属性“app:fastScrollEnabled”的 setter

android - CCPA 检测不跟踪,Google 帐户网络和应用 Activity 已关闭

java - 在 Android 中,是否可以从 RAM 中删除字符串?

android - Recyclerview StaggeredGridLayoutManager - 查找最后一个可见项目

Android v7 工具栏按钮对齐

android - MenuItemImpl 无法转换为relativelayout

android - 如何在工具栏中制作圆角菜单?

java - RecyclerView 滚动滞后

java - LibGDX:循环时使用 SpriteBatches