共享内容重叠底部导航

时间:2019-05-06 18:52:59

标签: android kotlin android-animation

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

当我单击被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将项目从意图中拉出,并设置launchdtitle和launchd_text。

2 个答案:

答案 0 :(得分:2)

添加recyclerView项目的CardView

app:cardMaxElevation="8dp"

并提供您的BNV

app:elevation="16dp" 

尝试一下,尝试使用更高的海拔差异,因为点击的Cardview的海拔高于实际海拔

答案 1 :(得分:1)

您听说过clipToPadding可以在底视图中添加填充,以免妨碍底视图。

因此,请尝试设置clipToPadding = "true"并检查其是否可以解决。

Android what does the clipToPadding Attribute do?

您的布局活动文件还将<RecyclerView>设置为0dp。

在外部使用<RelativeLayout>,并在layout_above="@bnv"中设置<RecyclerView>,与此类似。

<?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>
相关问题