首页 > 解决方案 > 在网格视图中显示图像和文件夹?

问题描述

我正在使用此代码从特定文件夹加载网格视图中的图像:

public class GalleryClass extends AsyncTask<Void,Void,Void> {
    public static ArrayList<String> f = new ArrayList<>();

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        GalleryFrag.gridView.setAdapter(new ImageAdapter(GalleryFrag.context));
    }

    @Override
    protected Void doInBackground(Void... voids) {
        File dir = new File(String.valueOf(Environment.getExternalStoragePublicDirectory("MyFolder")));
        File[] FileList = dir.listFiles();
        if (FileList !=null) {
            for (File aFileList : FileList) {
                f.add(aFileList.getAbsolutePath());
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        f.clear();
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        ImageAdapter(Context context1) {
            mContext = context1;
        }

        @Override
        public int getCount() {
            return f.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new ViewGroup.LayoutParams(230, 230));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(0, 0, 0, 0);
            } else {
                imageView = (ImageView) convertView;
            }
            Glide.with(GalleryFrag.context).load("file://" + f.get(position))
                    .apply(new RequestOptions()
                            .placeholder(R.drawable.ic_photo)
                            .error(R.drawable.ic_error))
                    .into(imageView);
            return imageView;
        }
    }
}

这是 Gallery.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
    android:background="@color/cardcolor"
tools:context=".GalleryFrag">

<!-- TODO: Update blank fragment layout -->

    <GridView
        android:id="@+id/gv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="1dp"
        android:stretchMode="columnWidth"/>

</RelativeLayout> 

当没有子文件夹时,我的文件夹中的所有图像都显示正常,否则它会显示ic_error占位符。

但是我也需要在主文件夹中添加子文件夹MyFolder,当单击文件夹时,它的所有图像也应该显示在主文件夹中GridView

标签: androidandroid-gallery

解决方案


推荐阅读