【问题标题】:how to display list of folders and files using android's expandable listview?如何使用 android 的可扩展列表视图显示文件夹和文件列表?
【发布时间】:2012-01-18 07:13:56
【问题描述】:

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

这就是我最初只使用 ListView 所做的:

我有 2 项活动。第一次活动,

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

不仅如此,第二次活动,

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

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

  1. 在蓝色高亮处,它将显示 3 个文件夹。
  2. 当我点击箭头按钮时,它会显示对应的文件
  3. 包含编辑按钮后,显示车辆(1 号、2 号等)的列表视图将变为检查列表,以便用户可以删除多个文件。

我该怎么做?因为我以前从未“接触过”可扩展的列表视图。

【问题讨论】:

    标签: android file android-listview directory expandablelistview


    【解决方案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);
    

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

    【讨论】:

      【解决方案2】:

      如果我正确理解了您的问题,您可以在您的 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;
      }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-09
        相关资源
        最近更新 更多