android - 如何使用 android 的可扩展 ListView 显示文件夹和文件列表?

标签 android file android-listview directory expandablelistview

我正在尝试显示文件和文件夹列表,并且我知道如何在 ListView 中执行此操作。但是,为了更好地使用 ui,我宁愿像下图那样。

enter image description here

这是我最初只用 ListView 做的:

我有 2 个 Activity 。在第一个 Activity 中,

  1. 它显示了我创建的 3 个文件夹(类似于系统文件夹)
  2. 当我点击其中一个文件夹时,它将进入第二个 Activity ,仅显示该文件夹中的文件。

不仅如此,在第2个 Activity 中,

  1. 当我点击一个按钮时, ListView 将从普通 ListView 变为检查列表类型 ListView ,以便用户可以删除多个文件。

所以我希望做的是,按照上面显示的图像..

  1. 在蓝色突出显示部分,它将显示 3 个文件夹。
  2. 当我点击箭头按钮时,它会显示对应的文件
  3. 包含编辑按钮后,显示车辆(1 号车、2 号车等)的 ListView 将更改为 list ,以便用户可以删除多个文件。

我该怎么做?因为我以前从未“接触过”可扩展 ListView 。

最佳答案

创建自定义 ExpandableListView 适配器:

public class customListAdapter extends BaseExpandableListAdapter {

    private File folder1;
    private File folder2;

    private String[] groups = {};
    private String[][] children = {};

    public customListAdapter() {
        // Sample data set.  children[i] contains the children (String[]) for groups[i].
        folder1 = new File (Environment.getExternalStorageDirectory(),"/Folder1");
        folder2 = new File (Environment.getExternalStorageDirectory(),"/Folder2");

        String[] fileList1 = folder1.list();
        String[] fileList2 = folder2.list();

        Arrays.sort(fileList1);
        Arrays.sort(fileList2);

        groups = new String[] { "Folder1" , "Folder2" };
        children = new String[][] { fileList1, fileList2 };
    }//constructor


    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                                View convertView, ViewGroup parent) {

        TextView textView = new TextView(this);
        textView.setBackgroundColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(100, 5, 0, 5);
        textView.setTextColor(Color.WHITE);
        textView.setTextSize(23);
        textView.setId(1000);

        textView.setText(getChild(groupPosition, childPosition).toString());
        return textView;
    }//getChildView

    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        TextView textView = new TextView(this);
        textView.setBackgroundColor(Color.WHITE);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(100, 0, 0, 0);
        textView.setTextColor(Color.BLACK);
        textView.setTextSize(25);
        textView.setText(getGroup(groupPosition).toString());

        return textView;
    }

    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    public boolean hasStableIds() {
        return true;
    }

}//customListAdapter

然后,使用以下方式引用它:

setContentView(R.layout.preferedlayout);
// Set up our adapter
listAdapter = new customListAdapter();

expLV = (ExpandableListView) findViewById(R.id.expList);
expLV.setAdapter(listAdapter);

希望这对其他人有帮助。 =)

关于android - 如何使用 android 的可扩展 ListView 显示文件夹和文件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8906471/

相关文章:

android - 使用服务创建一个即使应用程序最小化也会运行的计时器?

python - 使用python将某些文件从一个文件夹复制到另一个文件夹

android - 通过 Intents 将 listView 行位置传递给另一个类

android - 如何在ListView上显示同一张表的多列数据?

android - 如何在加载 UI 项目时显示 ProgressDialog?

android - 如何更改图表标题的颜色

android - Android:除非先按一个按钮,否则按钮2不会播放声音

java - 警告 :Dependency org. apache.httpcomponents :httpclient:4. 0.1 被忽略

c# - 从 C# 中的完整文件路径解析目录名称

c - 如何声明指向文件的动态指针数组