【发布时间】:2016-02-14 17:12:37
【问题描述】:
我想在 android 的列表视图中显示文件夹,但我必须构建一些不是静态预制的片段,因为我不知道文件夹树将如何。我想创建类似 Dropbox 的东西。更改文件夹内容时,它们如何显示文件夹内容列表。如何在运行时生成带有列表视图的片段?
【问题讨论】:
标签: java android listview android-activity fragment
我想在 android 的列表视图中显示文件夹,但我必须构建一些不是静态预制的片段,因为我不知道文件夹树将如何。我想创建类似 Dropbox 的东西。更改文件夹内容时,它们如何显示文件夹内容列表。如何在运行时生成带有列表视图的片段?
【问题讨论】:
标签: java android listview android-activity fragment
显然,并非应用的所有内容都必须是静态的。在 Android 上,实现“DropBox 文件夹内容”类型表示的一种方法是使用 ListView。
基本上,您要做的就是创建一个 ListView 实例(通过将其添加到您的布局或以编程方式添加到您的 ViewGroup)并为其设置一个有效的适配器。
适配器负责处理“可变”数据。
可以在这里找到一个很好的综合教程Using lists in Android (ListView) - Tutorial
编辑:
要在运行时创建片段,在收到 onItemClick 事件后,请尝试以下方法:
创建一个 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" />;
创建一个方法,类似这样:
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();
}
在onCreate()中调用这个方法,当你必须展示一个新的片段时
【讨论】: