【问题标题】:How can i create folders list like DropBox in android?如何在 android 中创建像 DropBox 这样的文件夹列表?
【发布时间】:2016-02-14 17:12:37
【问题描述】:

我想在 android 的列表视图中显示文件夹,但我必须构建一些不是静态预制的片段,因为我不知道文件夹树将如何。我想创建类似 Dropbox 的东西。更改文件夹内容时,它们如何显示文件夹内容列表。如何在运行时生成带有列表视图的片段?

【问题讨论】:

    标签: java android listview android-activity fragment


    【解决方案1】:

    显然,并非应用的所有内容都必须是静态的。在 Android 上,实现“DropBox 文件夹内容”类型表示的一种方法是使用 ListView。

    基本上,您要做的就是创建一个 ListView 实例(通过将其添加到您的布局或以编程方式添加到您的 ViewGroup)并为其设置一个有效的适配器。

    适配器负责处理“可变”数据。

    可以在这里找到一个很好的综合教程Using lists in Android (ListView) - Tutorial

    编辑:

    要在运行时创建片段,在收到 onItemClick 事件后,请尝试以下方法:

    1. 创建一个 Activity 并将这个 View 包含在它的布局中:

      <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"
          android:layout_below="@+id/toolbar"
          android:theme="@style/Style" />;
      
    2. 创建一个方法,类似这样:

      instantiateNewFolder(NewFolderDescriptor descriptor){
          FragmentManager fragmentManager = getSupportFragmentManager(); // or getFragmentManager()
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          fragmentTransaction.setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out);
          fragmentTransaction.replace(R.id.fragment_container, NewFolderFragment.instantiateNew(descriptor));
          fragmentTransaction.commit();
      }
      
    3. 在onCreate()中调用这个方法,当你必须展示一个新的片段时

    【讨论】:

    • 谢谢.. 但我该怎么做。我知道如何使用自定义列表视图和处理列表视图内容的适配器创建片段。但是我可以创建一个片段并简单地将数据更改为它并在单击列表项时在不同的数据时再次调用?
    • 好的,我明白了。我建议您将初始片段放在 ViewPager 中,并在 onItemClick() 事件之后将其更改为新片段,同时将以前使用的片段保留在历史堆栈中。另外,我不建议使用嵌套片段。我会用有用的 sn-p 更新我的答案
    • 不完全是 ViewPager,而是 FrameLayout
    • 举个例子就好了。非常感谢您的帮助和时间:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    相关资源
    最近更新 更多