如何使用android expandablelistview显示文件夹和文件列表?

时间:2012-01-18 07:13:57

标签: android file android-listview directory expandablelistview

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

enter image description here

这是我最初对ListView做的事情:

我有2项活动。在第1次活动中,

  1. 它显示了由我创建的3个文件夹(类似于系统文件夹)
  2. 当我点击其中一个文件夹时,它会打算进行第二个活动,只显示该文件夹中的文件。
  3. 不仅如此,在第二项活动中,

    1. 当我点击按钮时,列表视图将从正常列表视图更改为检查列表类型列表视图,以便用户可以删除多个文件。
    2. 所以我希望按照上面显示的图像进行操作。

      1. 在蓝色高亮显示,它将显示3个文件夹。
      2. 当我点击箭头按钮时,它会显示与之对应的文件
      3. 包含编辑按钮,显示车辆的列表视图(1号车辆,2号车辆等)将变为检查清单,以便用户可以删除多个文件。
      4. 我该怎么做?因为我之前从未“触及过”可扩展的列表视图。

2 个答案:

答案 0 :(得分:1)

创建自定义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);
希望这有助于其他人。 =)

答案 1 :(得分:0)

如果我正确理解了你的问题,你可以在ListView中设置一个适配器来扩展你想要的某种布局。

示例:

listView = (ListView)findViewById(R.id.feedList);
MyAdapter adapter = new MyAdapter(this, items, Color.WHITE);
listView.setAdapter(adapter);

items是某些排序实体的ArrayList

适配器代码:

public class MyAdapter extends BaseAdapter {
private ArrayList<MyItem> items = null;
private LayoutInflater layoutInflater = null;
MyAdapter(Context context, ArrayList<MyItem> items, int bgColor){
    this.items = items;
    layoutInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = layoutInflater.inflate(R.layout.my_layout, null);
    ImageView imageView = (ImageView) row.findViewById(R.id.image);
    imageView.setImageResource(R.drawable.my_image);
    TextView text = (TextView) row.findViewById(R.id.title);
    text.setText("Hi");
    return row;
}
}