gpt4 book ai didi

android - 将第二个 fragment 安装到 Activity 中

转载 作者:行者123 更新时间:2023-11-30 01:24:30 25 4
gpt4 key购买 nike

在我的 Activity 中,我动态地转换了一个 fragment ,它占据了屏幕的动态部分(您可以向上或向下滑动它以覆盖整个屏幕或仅覆盖底部 ~250dp)。

我现在想动态地将另一个 fragment 添加到同一个 Activity 中,但我需要这个新 fragment 与第一个 fragment 相邻。这意味着您仍然可以上下滑动第一个,但第二个将留在第一个后面或移动以腾出空间。

Google 告诉我使用同一个 fragment 管理器并只添加一个 fragment ,但同一个 fragment 管理器已经完成了提交(添加了第一个 fragment )。

有什么办法可以实现吗?谢谢!

代码:

在我的 onCreate Activity 中,我添加了一个名为 buildingFragment(蓝色)的 fragment

    fragmentTransaction = getFragmentManager().beginTransaction().add(R.id.fragment_container, buildingFragment, "BUILDINGPAGE");
fragmentTransaction.hide(buildingFragment);
fragmentTransaction.commit();

buildingFragment 在 onCreateView 中对自身进行动画处理,使其大部分内容都在屏幕之外(底部方向):

    // ANIMATE THE BUILDING PAGE TO PEAK FROM BOTTOM
ObjectAnimator anim = ObjectAnimator.ofFloat(buildingPageView, "y", USEABLE_HEIGHT, USEABLE_HEIGHT-TOOLBAR_HEIGHT-VIRT_NAV_BAR);
anim.setDuration(500);
anim.start();

现在这个 BuildingFragment 可以工作了,它可以向上或向下滑动。按下按钮后,我想向 Activity 中添加另一个 fargment(红色),但我需要它占用屏幕上 BuildingFragment(蓝色)当前未占用的其余空间。

不,当我滑动 BuildingFragment(蓝色)时,我需要第二个 fragment (红色)要么留在 Building Fragment 后面,要么只是通过向上或向下移动来腾出空间。任何一种方式都是可以接受的。

这是一个例子:

enter image description here

第二个问题:如果我不想替换 fragment 而是添加一个新 fragment ,我怎样才能将旧 fragment 放在新 fragment 之上?

最佳答案

检查 this lib 它可能会帮助您实现您想要的目标。

<com.sothree.slidinguppanel.SlidingUpPanelLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoPanelHeight="68dp"
sothree:umanoShadowHeight="4dp"
sothree:umanoParallaxOffset="100dp"
sothree:umanoDragView="@+id/dragView"
sothree:umanoOverlay="true"
sothree:umanoScrollableView="@+id/list">

<!-- MAIN CONTENT -->

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--LOAD FIRST FRAGMENT-->

</FrameLayout>
<!-- SLIDING LAYOUT -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--LOAD SECOND FRAGMENT-->

</FrameLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>

关于android - 将第二个 fragment 安装到 Activity 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36594254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com