gpt4 book ai didi

android - 如何: gridview inside a fragment?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:16:54 24 4
gpt4 key购买 nike

我想创建一个类似 android market 的 gridview,我想用互联网数据库中的图像填充它。它需要与 androidv4.support 一起工作,因为我想运行 2.2 到 4.0。

有人说,在 pre-4.0 中不能创建 gridview,是这样吗?

然而,它仅在我使用 setListAdapter() 时有效,但当我更改为 gridview.setAdapter() 时,它每行仅显示一个图像,如 ListView >,它不再起作用了。

这是我的尝试:

这是 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);

}

这是 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;
}
}

如果有人有 sample ,它会很有帮助。谢谢

最佳答案

这应该可以正常工作,您不能在 ListFragment 中使用 gridView - 如果您要手动操作,只需使用普通的旧 Fragment无论如何管理网格

此外,检查 convertView 是否为 null 的目的是进行 View 回收 - 操作系统仅声明足够的 View 来填满屏幕,不再声明更多,因此如果您滚动,则它可以重用 View ,而不必扩充新 View 一。像这样更改您的 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;
}

此外,getView 不是 BaseAdapter 的函数,请尝试改用 ArrayAdapter。作为旁注,当你认为你正在覆盖一个基本函数时,总是使用 @Override - 这样如果你犯了错误,编译器就会给你一个错误

关于android - 如何: gridview inside a fragment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10285634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com