在 Fragment 中使用 GridView 创建画廊?
Creating a Gallery with GridView in Fragment?
我正在尝试使用以下代码在名为 GalleryFrag 的 Fragment 中创建一个带有 gridview 的图库
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.io.File;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class GalleryFrag extends Fragment {
ArrayList<String> f = new ArrayList<>();
File[] files;
public GalleryFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
GridView gridView = (GridView) view.findViewById(R.id.gv);
gridView.setAdapter(new ImageAdapter(this.getContext()));
getImages();
return view;
}
public void getImages() {
File file = new File(android.os.Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES));
if (file.isDirectory()) {
files = file.listFiles();
for (int i = 0; i < files.length; i++) {
f.add(files[i].getAbsolutePath());
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
@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(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
//if it's not recycled, initialize some atributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(Integer.parseInt(f.get(position)));
return imageView;
}
}
}
但是当我 运行 应用程序时,它在包含图像的名为 Pictures 的文件夹中的 Gridview 中什么也没有显示。
当用户单击网格视图中的任何图像时,我想全屏显示图像。
也可以使用任何不同的方法。
请帮助我为我的应用程序创建图库。
非常欢迎回答。
代码更新
我用这段代码检查 file.exists :
public void getImages() {
File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)));
if (file.exists()){
Toast.makeText(this.getContext(), "Yep It's there", Toast.LENGTH_LONG).show();
files = file.listFiles();
if (files !=null) {
for (int i = 0; i < files.length; i++) {
//String[] FileNames= new String[files.length];
//FileNames[i]=files[i].getName();
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this.getContext(), android.R.layout.simple_list_item_1, FileNames);
//listView.setAdapter(arrayAdapter);
f.add(files[i].getAbsolutePath());
}
} else {
Toast.makeText(this.getContext(), "error", Toast.LENGTH_SHORT).show();
}
}
}
更新
我也需要在图库中添加子文件夹,并且需要从网格升级到 recyclerview。我需要在全屏 onclick 上显示图像,而不是在具有弹出图像查看器的同一片段中的其他活动上显示图像。
您在将值保存到 arraylist 之前给适配器充气,所以您的 arraylist 是空白的。应用此代码:-
public class GalleryFrag extends Fragment {
ArrayList<String> f = new ArrayList<>();
File[] files;
public GalleryFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
GridView gridView = (GridView) view.findViewById(R.id.gv);
getImages();
gridView.setAdapter(new ImageAdapter(this.getContext()),f);
return view;
}
public void getImages() {
File file = new File(android.os.Environment.getDataDirectory(), "injan");
if (file.isDirectory()) {
files = file.listFiles();
for (int i = 0; i < files.length; i++) {
f.add(files[i].getAbsolutePath());
}
}
}
您的适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> f;
public ImageAdapter(Context context, ArrayList<String> fData ) {
mContext = context;
f=fData ;
}
@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(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
//if it's not recycled, initialize some atributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(Integer.parseInt(f.get(position)));
return imageView;
}
}
}
it shows nothing in Gridview from the folder in Android/Data/injan
name/path 开头的文件夹不存在。
File file = new File(android.os.Environment.getDataDirectory(), "injan");
该目录“/data/injan”只能在有根设备上访问。那么你是怎么把图片放进去的呢?您的 arraylist 不包含元素,因此不显示任何内容。
imageView.setImageResource(Integer.parseInt(f.get(position)));
预期的文件名只是数字吗?它们是否等于资源标识符?很奇怪这是如何工作的。
虽然按照您的实现方式是可以的,但在执行列表之前设置适配器。
为了让您的应用能够 read/write 文件系统,您应该在清单文件中请求所需的权限。
对于 Android 6+,您需要添加代码让用户在 运行 时确认这些请求。
Google 运行 时间权限。
你今年的这个问题排名第 1254 位。
您可以使用这个库导入一个简单的画廊,而不需要太多代码:
图片库
https://github.com/lawloretienne/ImageGallery/blob/master/README.md
示例用法
Intent intent = new Intent(MainActivity.this, ImageGalleryActivity.class);
String[] images = getResources().getStringArray(R.array.unsplash_images);
Bundle bundle = new Bundle();
bundle.putStringArrayList(ImageGalleryActivity.KEY_IMAGES, new ArrayList<>(Arrays.asList(images)));
bundle.putString(ImageGalleryActivity.KEY_TITLE, "Unsplash Images");
intent.putExtras(bundle);
startActivity(intent);
Z画廊
https://github.com/mzelzoghbi/ZGallery
示例用法
ZGallery.with(this, /*your string arraylist of image urls*/)
.setToolbarTitleColor(ZColor.WHITE) // toolbar title color
.setGalleryBackgroundColor(ZColor.WHITE) // activity background color
.setToolbarColorResId(R.color.colorPrimary) // toolbar color
.setTitle("Zak Gallery") // toolbar title
.show();
我正在尝试使用以下代码在名为 GalleryFrag 的 Fragment 中创建一个带有 gridview 的图库
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.io.File;
import java.util.ArrayList;
/**
* A simple {@link Fragment} subclass.
*/
public class GalleryFrag extends Fragment {
ArrayList<String> f = new ArrayList<>();
File[] files;
public GalleryFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
GridView gridView = (GridView) view.findViewById(R.id.gv);
gridView.setAdapter(new ImageAdapter(this.getContext()));
getImages();
return view;
}
public void getImages() {
File file = new File(android.os.Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES));
if (file.isDirectory()) {
files = file.listFiles();
for (int i = 0; i < files.length; i++) {
f.add(files[i].getAbsolutePath());
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context context) {
mContext = context;
}
@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(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
//if it's not recycled, initialize some atributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(Integer.parseInt(f.get(position)));
return imageView;
}
}
}
但是当我 运行 应用程序时,它在包含图像的名为 Pictures 的文件夹中的 Gridview 中什么也没有显示。 当用户单击网格视图中的任何图像时,我想全屏显示图像。 也可以使用任何不同的方法。 请帮助我为我的应用程序创建图库。 非常欢迎回答。
代码更新 我用这段代码检查 file.exists :
public void getImages() {
File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)));
if (file.exists()){
Toast.makeText(this.getContext(), "Yep It's there", Toast.LENGTH_LONG).show();
files = file.listFiles();
if (files !=null) {
for (int i = 0; i < files.length; i++) {
//String[] FileNames= new String[files.length];
//FileNames[i]=files[i].getName();
//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this.getContext(), android.R.layout.simple_list_item_1, FileNames);
//listView.setAdapter(arrayAdapter);
f.add(files[i].getAbsolutePath());
}
} else {
Toast.makeText(this.getContext(), "error", Toast.LENGTH_SHORT).show();
}
}
}
更新 我也需要在图库中添加子文件夹,并且需要从网格升级到 recyclerview。我需要在全屏 onclick 上显示图像,而不是在具有弹出图像查看器的同一片段中的其他活动上显示图像。
您在将值保存到 arraylist 之前给适配器充气,所以您的 arraylist 是空白的。应用此代码:-
public class GalleryFrag extends Fragment {
ArrayList<String> f = new ArrayList<>();
File[] files;
public GalleryFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gallery, container, false);
GridView gridView = (GridView) view.findViewById(R.id.gv);
getImages();
gridView.setAdapter(new ImageAdapter(this.getContext()),f);
return view;
}
public void getImages() {
File file = new File(android.os.Environment.getDataDirectory(), "injan");
if (file.isDirectory()) {
files = file.listFiles();
for (int i = 0; i < files.length; i++) {
f.add(files[i].getAbsolutePath());
}
}
}
您的适配器
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> f;
public ImageAdapter(Context context, ArrayList<String> fData ) {
mContext = context;
f=fData ;
}
@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(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
//if it's not recycled, initialize some atributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new ViewGroup.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(Integer.parseInt(f.get(position)));
return imageView;
}
}
}
it shows nothing in Gridview from the folder in Android/Data/injan
name/path 开头的文件夹不存在。
File file = new File(android.os.Environment.getDataDirectory(), "injan");
该目录“/data/injan”只能在有根设备上访问。那么你是怎么把图片放进去的呢?您的 arraylist 不包含元素,因此不显示任何内容。
imageView.setImageResource(Integer.parseInt(f.get(position)));
预期的文件名只是数字吗?它们是否等于资源标识符?很奇怪这是如何工作的。
虽然按照您的实现方式是可以的,但在执行列表之前设置适配器。
为了让您的应用能够 read/write 文件系统,您应该在清单文件中请求所需的权限。
对于 Android 6+,您需要添加代码让用户在 运行 时确认这些请求。
Google 运行 时间权限。
你今年的这个问题排名第 1254 位。
您可以使用这个库导入一个简单的画廊,而不需要太多代码:
图片库
https://github.com/lawloretienne/ImageGallery/blob/master/README.md
示例用法
Intent intent = new Intent(MainActivity.this, ImageGalleryActivity.class);
String[] images = getResources().getStringArray(R.array.unsplash_images);
Bundle bundle = new Bundle();
bundle.putStringArrayList(ImageGalleryActivity.KEY_IMAGES, new ArrayList<>(Arrays.asList(images)));
bundle.putString(ImageGalleryActivity.KEY_TITLE, "Unsplash Images");
intent.putExtras(bundle);
startActivity(intent);
Z画廊
https://github.com/mzelzoghbi/ZGallery
示例用法
ZGallery.with(this, /*your string arraylist of image urls*/)
.setToolbarTitleColor(ZColor.WHITE) // toolbar title color
.setGalleryBackgroundColor(ZColor.WHITE) // activity background color
.setToolbarColorResId(R.color.colorPrimary) // toolbar color
.setTitle("Zak Gallery") // toolbar title
.show();