android - 在网格视图中显示图像和文件夹?
问题描述
我正在使用此代码从特定文件夹加载网格视图中的图像:
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
。
解决方案
推荐阅读
- kubernetes - 应用程序之间的 ArgoCD 同步波
- c# - 当 Screen.orientaion 改变时 Unity 框架丢弃
- python - 如何从 pandas.value_counts() 返回元素
- android - VideoView 无法播放视频文件并显示 mediiaplayer 错误
- python - 如何在 pandas Dataframe 中创建多级标题?
- regex - 排除“|” 从正则表达式
- reactjs - 在 React 中将多个数据从子级传递给父级
- javascript - 为谷歌时间线图表着色一种特定颜色,并使其他颜色为“默认”各种颜色
- python - 在 django 3.1 中将图像添加到数据库时显示此错误
- reactjs - ReactJS Hook - 如何重新渲染其他组件