共享内容与底部导航重叠问题

10

我的MainActivity有一个RecyclerView和一个BottomNavigationView。 RecyclerView中的项目是CardView。

当我点击被BottomNavigationView(以下简称BNV)遮挡一半的项目时,它会“弹出”到BNV上方,然后向上滑动成为LaunchedActivity中的标题。

当退出LaunchedActivity时,它会向下滑动,在BNV上方,并“捕获”回原始位置:

enter image description here

我应该如何:

  1. 让共享内容从BNV下面滑出, 或者
  2. 让共享内容开始不可见,并在滑动到标题时淡入淡出。

我尝试调整BNV的高度,尝试将sharedElementEnterTransition设置为Fade(),尝试使用BottomNavigation指定excludeTarget; 但似乎都不能使事情按照我想要的方式工作。

这是MainActivity的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <android.support.v7.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            />
    <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/navigation"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
           />
</android.support.constraint.ConstraintLayout>

Activity_launched在这里:

<?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".LaunchedActivity">

    <android.support.design.widget.AppBarLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

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

    <include layout="@layout/content_launched"/>

    <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_dialog_email"/>

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

并且 content_launched:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:showIn="@layout/activity_launched"
        tools:context=".LaunchedActivity">

    <LinearLayout
            android:id="@+id/launched_header"
            android:transitionName="header" android:layout_width="0dp" android:layout_height="wrap_content"
                  app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
                  android:orientation="vertical" app:layout_constraintEnd_toEndOf="parent"
                  android:background="@android:color/holo_blue_bright"
                  android:layout_marginEnd="8dp">
        <TextView
                android:text="TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:id="@+id/launched_title"/>
        <TextView
                android:text="TextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:id="@+id/launched_text"/>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

RecyclerView中的项调用MainActivity.onItemClicked:

   fun onItemClicked(view: View, item: Item) {
        val intent = Intent(applicationContext, LaunchedActivity::class.java)
        intent.putExtra("ITEM", item)
        val options = ActivityOptions.makeSceneTransitionAnimation(
            this,
            android.util.Pair<View, String>(view, "header")
        )
        startActivity(intent, options.toBundle())
    }

这是在回收器中的数组:

data class Item(val title: String, val text: String): Serializable

val itemList = listOf(Item("One", "1"), Item("Two", "2"),
        Item("Three", "3"), Item("Four", "4"), Item("Five", "5"))

最后,这是从LaunchedActivity.onCreate:

   with(window) {
        requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
        requestFeature(Window.FEATURE_ACTIVITY_TRANSITIONS)
        sharedElementEnterTransition = AutoTransition()
        sharedElementExitTransition = AutoTransition()
    }

LaunchedActivity.onCreate从intent中提取项并设置launched_title和launched_text。


2
你能分享一下你的布局和onClick代码吗? - Vanshaj Daga
2
给RecyclerView和底部导航栏之间留出边距。还请分享您的布局和活动代码。 - TheAnkush
我已经添加了布局和代码。 - brismith
我确信问题出在ActivityOptions.makeSceneTransitionAnimation上,因为如果我没记错的话它会作为覆盖层渲染...所以它会始终在所有内容的上方。 - user10539074
2个回答

3

在RecyclerView的每个项目中添加CardView

app:cardMaxElevation="8dp"

并给出你的BNV。
app:elevation="16dp" 

试试看,用更大的高度差,因为点击的卡片视图比实际高度要高。


1
我仍然得到相同的效果:卡片在BNV之上弹出,之后又回弹。 - brismith
@brismith 尝试在 fun onItemClicked(view: View, item: Item) 中写入 bottomNavigationView?.bringtoFront()。 - Akash Dubey
bnv.bringToFront不影响行为。 - brismith

1

你听说过clipToPadding吗?它可以为底部视图添加填充,以避免它们被遮挡。

因此,请尝试设置clipToPadding="true",并检查是否解决了问题。

Android中的clipToPadding属性是什么?

另外,你的布局活动文件中<RecyclerView>设置为0dp。

<RecyclerView>外使用一个<RelativeLayout>,并将<RecyclerView>中的layout_above="@bnv"设置为类似此示例。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/navigation1"
        android:layout_weight="1">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <com.nxtoff.plzcome.commonutills.BottomNavigationViewPager
                android:id="@+id/viewpager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:layout_below="@+id/viewpager"
                android:background="#999999" />

        </RelativeLayout>


    </FrameLayout>


    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation1"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:itemBackground="@android:color/white"
        app:itemIconTint="@drawable/nav_item_colors"
        app:itemTextColor="@drawable/nav_item_colors"
        app:menu="@menu/navigation" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/xfb_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_margin="22dp"
        android:layout_marginBottom="30dp"
        android:background="@color/colorloader"
        android:clickable="true"
        android:src="@drawable/plus_fab"
        android:tint="@color/colorviolet"
        app:backgroundTint="@color/colorloader"
        app:elevation="8dp"
        app:fabSize="normal"
         />



</RelativeLayout>

编辑 1:

尝试运行这段代码并告诉我。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:layout_above="@id/bottomNavigationView"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="8dp"
        />
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/navigation"
        android:layout_alignParentBottom="true"
        />
</RelativeLayout>

我已经添加了clipPadding和clipChildren属性,但没有效果。同时,我将ConstraintLayout(这就是为什么rv的高度和宽度为0)切换到RelativeLayout,并指定rv的layout_above为bnv。但我仍然得到相同的行为。顺便说一下,我看到其他Android应用程序也有相同的效果:其中两个是Zinio和PlayerFM。我希望我不会被困在这里。 - brismith
我相信这个问题一定有解决办法.. 我仍然认为rv不接受layout_above.. 让我试一下,然后再回复你。 - sanjeev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接