Android 如何:在片段中使用 gridview?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10285634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to: gridview inside a fragment?
提问by Marckaraujo
I want to create a gridview like android market, I want to populate this with images from a database on internet. it need to work with androidv4.support, since I want to run 2.2 until 4.0.
我想创建一个类似 android market 的 gridview,我想用互联网上的数据库中的图像填充它。它需要使用 androidv4.support,因为我想运行 2.2 直到 4.0。
Someone said, that isnt possible to create a gridview in pre-4.0, is it true?
有人说,在pre-4.0中创建gridview是不可能的,是真的吗?
However It only works when I put use setListAdapter()
, but it shows only one image per line, like a listview, when I change to gridview.setAdapter()
, it doenst work anymore.
但是,它仅在我使用 use 时才有效setListAdapter()
,但每行仅显示一个图像,例如列表视图,当我更改为 时gridview.setAdapter()
,它不再起作用。
Here is my try:
这是我的尝试:
This is the ListFragment class:
这是 ListFragment 类:
public static class ArrayListFragment extends ListFragment implements OnScrollListener{
ImageAdapter adapter = new ImageAdapter();
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
LayoutInflater gridInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = gridInflater.inflate(R.layout.imagelist, null);
GridView gridView = (GridView) v.findViewById(R.id.list);
ImageDownloader.Mode mode = ImageDownloader.Mode.CORRECT;
// ImageAdapter imageAdapter = new ImageAdapter();
adapter.getImageDownloader().setMode(mode);
setListAdapter(adapter);
// gridView.setAdapter(adapter);
getListView().setOnScrollListener(this);
}
This is ImageAdapter class:
这是 ImageAdapter 类:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private final ImageDownloader imageDownloader = new ImageDownloader();
public static int count = 10;
private final String[] URLS = {
"http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg",
"http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg",
"http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg",
"http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg",
"http://lh3.ggpht.com/_lLj6go_T1CQ/TCD8PW09KBI/AAAAAAAAQdc/AqmOJ7eg5ig/s144-c/Juvenile%20Gannet%20despute.jpg",
};
public int getCount() {
return count;
}
public String getItem(int position) {
return URLS[position];
}
public long getItemId(int position) {
return URLS[position].hashCode();
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = LayoutInflater.from(mContext).inflate(R.layout.image_text_view,null);
v.setLayoutParams(new GridView.LayoutParams(200,200));
ImageView imageview = (ImageView)v.findViewById(R.id.image);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setPadding(6, 6, 6, 6);
imageDownloader.download(URLS[position], imageview);
}
else {
v = convertView;
}
return v;
}
public ImageDownloader getImageDownloader() {
return imageDownloader;
}
}
It could help a lot if anyone have a sample. Thanks
如果有人有样品,它会很有帮助。谢谢
回答by JRaymond
This should work fine, you just can't use a gridView in a ListFragment
- just use a plain old Fragment
instead, if you're going to be manually managing the grid anyway
这应该可以正常工作,您只是不能在 a 中使用 gridView ListFragment
- 只需使用普通的旧Fragment
代替,如果您无论如何要手动管理网格
Also, the point of checking if convertView is null is to do view recycling - the OS only declares enough views to fill the screen and no more, so if you scroll then it can reuse the view instead of having to inflate a new one. Change up your getView() like so to take advantage:
此外,检查 convertView 是否为 null 的目的是进行视图回收 - 操作系统仅声明足够的视图来填充屏幕而不会再声明,因此如果您滚动,则它可以重用该视图而不必膨胀一个新视图。像这样改变你的 getView() 以利用:
public View getView(int position, View convertView, ViewGroup parent) {
View v;
if (convertView == null) {
v = LayoutInflater.from(mContext).inflate(R.layout.image_text_view,null);
v.setLayoutParams(new GridView.LayoutParams(200,200));
}
else {
v = convertView;
}
ImageView imageview = (ImageView)v.findViewById(R.id.image);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setPadding(6, 6, 6, 6);
imageDownloader.download(URLS[position], imageview);
return v;
}
Also, getView isn't a function of BaseAdapter, try switching to ArrayAdapter instead. As a side note, always use @Override
when you think you're overriding a base function - that way the compiler will give you an error if you make a mistake
此外,getView 不是 BaseAdapter 的功能,请尝试切换到 ArrayAdapter。作为旁注,@Override
当你认为你覆盖了一个基本函数时总是使用- 这样编译器会在你犯错时给你一个错误
回答by marczych
Using a GridView
in a Fragment
shouldn't be any different than using it in an Activity
. One glaring error I see with your code is that you are inflating a layout in onActivityCreated
and then promptly ignore it. Instead you should do all of your view initialization in onCreateView
which conveniently provides a LayoutInflater
for your use.
GridView
在 a 中使用 aFragment
应该与在Activity
. 我在您的代码中看到的一个明显错误是您正在膨胀布局onActivityCreated
,然后立即忽略它。相反,您应该进行所有的视图初始化,onCreateView
其中方便地提供一个LayoutInflater
供您使用。
As for its current behavior, it makes a lot of sense why it's acting how it is. I believe that ListFragment
inflates a layout that contains a ListView
if the programmer doesn't provide one (which you currently are not). The ImageAdapter
you are setting is then used to provide the View
s to the ListView
.
至于它目前的行为,它为什么会如此行事是很有意义的。我相信如果程序员不提供一个ListFragment
包含一个的布局ListView
(你目前没有),我相信它会膨胀。在ImageAdapter
您设置,然后用于提供View
s到的ListView
。
So move all of your code that is in onActivityCreated
to onCreateView
and it should work. You shouldn't need to override onActivityCreated
at all unless you need to do something with special with the Activity
when your Fragment
is attached to it.
因此,移动所有的代码是在onActivityCreated
给onCreateView
它应该工作。你不应该需要重写onActivityCreated
,除非你需要做的事情与特殊可言Activity
,当你Fragment
连接到它。
And as for using GridView
pre 4.0 - GridView
has been around since API level 1 so I'd bet that it's fine to use it for all Android API levels.
至于使用GridView
pre 4.0 -GridView
自 API 级别 1 以来一直存在,所以我敢打赌,可以将它用于所有 Android API 级别。