草庐IT

android - 具有多个 fragment/ View 的 DialogFragment

coder 2023-11-30 原文

如何用动画一个接一个地显示一个包含多个 fragment 的对话框 fragment ?

我的用例是:

  1. DialogFragment 与 fragment 1 一起显示。它有一个“下一步”按钮
  2. 用户点击下一步
  3. 同一个 dialogFragment 显示带有动画幻灯片的 fragment 2。

任何指针都会有所帮助。

提前谢谢你。

这是我正在使用的基本对话 fragment

public class BaseDialogFragment extends DialogFragment {

    public BaseDialogFragment () {

    }

    public static BaseDialogFragment newInstance(String title) {
        BaseDialogFragment frag = new BaseDialogFragment ();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment, container);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);


        getDialog().getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    }
}

行为是这样的。显示带有流程的对话框的是 BottomNavigation Activity 。下一个/上一个对话框带有滑入/滑出导航。

我也愿意接受其他建议,例如对话主题 Activity

最佳答案

据我了解,您希望有一个父对话 fragment 管理两个子 fragment 。 为此,您必须遵循这些步骤。

  1. 创建父对话框 fragment
  2. 创建两个子 fragment
  3. 将第一个 fragment 添加到父 fragment
  4. 添加从第一个子 fragment 到父 fragment 的回调以用第二个子 fragment 替换它
  5. 向父 fragment 添加功能以替换子 fragment

让我们从第一步开始。我们将创建一个容器对话框 fragment :

class ContainerDialogFragment extends DialogFragment {
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        return inflater.inflate(R.layout.container_fragment, container, false);
    }
}

我们的 container_fragment xml 看起来像:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

然后我们创建两个子 fragment :

class ChildFragment1 extends Fragment {
    //...the content is up to you...
}

class ChildFragment2 extends Fragment {
    //...the content is up to you...
}

我们将第一个 fragment 添加到我们的容器对话框 fragment 中:

class ContainerDialogFragment extends DialogFragment {
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        return inflater.inflate(R.layout.container_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        ChildFragment1 childFragment1 = new ChildFragment1();
        transaction.replace(R.id.fragment_container, childFragment1);
        transaction.commit();
    }
}

现在我们必须添加一个接口(interface)来在父子 fragment 之间进行通信以替换它:

class ChildFragment1 extends Fragment {
    interface ChildFragment1Listener {
        void onButtonPressed();
    }

    //you have to call this method when user pressed to button
    void onButtonPressed() {
        ChildFragment1Listener listener = (ChildFragment1Listener) getParentFragment();
        listener.onButtonPressed();
    }
}

最后,我们必须在我们的容器对话框 fragment 中实现这个接口(interface)并添加替换功能:

class ContainerDialogFragment extends DialogFragment implements ChildFragment1.ChildFragment1Listener {
    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        return inflater.inflate(R.layout.container_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        ChildFragment1 childFragment1 = new ChildFragment1();
        transaction.replace(R.id.fragment_container, childFragment1);
        transaction.commit();
    }

    @Override
    void onButtonPressed() {
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        //Out of simplicity, i am creating ChildFragment2 every time user presses the button. 
        //However, you should keep the instance somewhere to avoid creation.
        ChildFragment2 childFragment2 = new ChildFragment2();
        transaction.replace(R.id.fragment_container, childFragment2);
        //You can add here as well your fragment in and out animation how you like.
        transaction.addToBackStack("childFragment2");
        transaction.commit();
    }
}

就是这样。

关于android - 具有多个 fragment/ View 的 DialogFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52675428/

有关android - 具有多个 fragment/ View 的 DialogFragment的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - 在 Ruby 中循环遍历多个数组 - 2

    我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代

  4. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  5. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  6. ruby - 多个属性的 update_column 方法 - 2

    我有一个具有一些属性的模型:attr1、attr2和attr3。我需要在不执行回调和验证的情况下更新此属性。我找到了update_column方法,但我想同时更新三个属性。我需要这样的东西:update_columns({attr1:val1,attr2:val2,attr3:val3})代替update_column(attr1,val1)update_column(attr2,val2)update_column(attr3,val3) 最佳答案 您可以使用update_columns(attr1:val1,attr2:val2

  7. ruby-on-rails - 在 ruby​​ .gemspec 文件中,如何指定依赖项的多个版本? - 2

    我正在尝试修改当前依赖于定义为activeresource的gem:s.add_dependency"activeresource","~>3.0"为了让gem与Rails4一起工作,我需要扩展依赖关系以与activeresource的版本3或4一起工作。我不想简单地添加以下内容,因为它可能会在以后引起问题:s.add_dependency"activeresource",">=3.0"有没有办法指定可接受版本的列表?~>3.0还是~>4.0? 最佳答案 根据thedocumentation,如果你想要3到4之间的所有版本,你可以这

  8. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  9. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  10. ruby - 使用多个数组创建计数 - 2

    我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']

随机推荐