我有一个Android应用程序,我需要从服务器检索图像,并且必须将它们显示到带有标题的网格视图中。所以请任何人都可以帮助我如何执行此任务机器人。从服务器的网格视图中加载图像
0
A
回答
1
这可以通过以下步骤来完成:
- 创建AsyncTaskLoader(这里是一个很好的教程http://www.androiddesignpatterns.com/2012/08/implementing-loaders.html)。在那里你可以从服务器加载你的图像。
- 例如,通过扩展
BaseAdapter
,创建自定义适配器来存储图像和创建视图。自己实现所需的功能。 - 在此适配器内部创建一个负责添加新数据的函数。一旦完成,请不要忘记拨打电话
notifyDataSetChanged()
- 将此适配器连接到您的
GridView
- 将加载的图像推送到您的适配器。
EDIT好的,这是构思的基本证明:
活动(装载机可以在onCreate
方法来启动):
public class MyActivity extends Activity implements LoaderManager.LoaderCallbacks<ArrayList<Drawable>> {
//other stuff
@Override
public Loader<ArrayList<Drawable>> onCreateLoader(int id, Bundle args) {
return new ImageLoader(this);
}
@Override
public void onLoadFinished(Loader<ArrayList<Drawable>> loader, ArrayList<Drawable> data) {
myAdapter.pushData(data);
}
@Override
public void onLoaderReset(Loader<ArrayList<Drawable>> loader) {
}
}
装载机:
public class ImageLoader extends AsyncTaskLoader<ArrayList<Drawable>> {
public ImageLoader(Context context) {
super(context);
}
@Override
public ArrayList<Drawable> loadInBackground() {
//load the stuff
return data;
}
@Override
protected void onStartLoading() {
if (data != null) {
deliverResult(data);
}
if (takeContentChanged() || data == null) {
forceLoad();
}
}
}
适配器:
public class MyAdapter extends BaseAdapter {
ArrayList<Drawable> data = new ArrayList<>();
//other functions
public void pushData(ArrayList<Drawable> data){
this.data = data;
notifyDataSetChanged();
}
}
2
如果您正在使用URL加载图像从服务器,那么你可以使用毕加索。
对于使用Picasso
- >
1)首先在built.gradle添加compile 'com.squareup.picasso:picasso:2.5.2'
中的依赖关系。
2)在网格适配器java文件中添加导入"com.squareup.picasso.Picasso;"
。
3)现在将"Picasso.with(context).load(pImage[position]).into(imageView);"
添加到适配器文件(从服务器数据加载网格)中。
compileSdkVersion 23
buildToolsVersion "23.0.2"
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'
}
现在适配器文件在getView方法(注po
是全局变量):
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
po = position;
View gridView;
if (convertView == null) {
gridView = inflater.inflate(R.layout.dashboard_inner, null);// set image based on selected text
} else {
gridView = (View) convertView;
}
imageView = (ImageView) gridView.findViewById(R.id.grid_item_image);
TextView textView1 = (TextView)gridView.findViewById(R.id.grid_item_text1);
textView1.setText(pName[position]);
TextView textView2 = (TextView) gridView.findViewById(R.id.grid_item_text2);
textView2.setText(pPrice[position]);
textView2.setPaintFlags(textView2.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
TextView textView3 = (TextView) gridView.findViewById(R.id.grid_item_text3);
textView3.setText(pSprice[position]);
TextView textView4 = (TextView) gridView.findViewById(R.id.grid_item_text4);
textView4.setText(pOffer[position]);
Picasso.with(context).load(pImage[position]).into(imageView);
return gridView;
}
相关问题
- 1. 从网络服务器加载图像
- 2. 从服务器和列表视图中加载图像
- 3. 从服务器获取图像到网格视图,获取java.lang.NullPointerException
- 4. 从远程服务器加载图像
- 5. 从远程服务器加载图像?
- 6. 如何从服务器使用图像ID加载图像?
- 7. 二进制图像从服务器加载到图像与JavaScript
- 8. 不能将图像从SD卡加载到网格视图
- 9. 与毕加索的网格视图无法加载图像
- 10. Android - 从服务器下载图像并加载到ImageView中
- 11. AS3使用加载程序从服务器加载图像
- 12. 如何从图库中加载图像视图中的图像?
- 13. 从表格视图加载动画图像控制器
- 14. 文本和图像视图分别在android的网格视图中加载
- 15. 图片加载从java服务器到android列表视图
- 16. 代理服务器不加载图像
- 17. 如何在ActionScript3中平滑从服务器加载的图像
- 18. 自动从服务器下载图像
- 19. 从服务器下载图像
- 20. 从服务器下载Android图像?
- 21. 如何加速从服务器的图像加载 - Android
- 22. 从列表视图适配器中加载URL中的图像
- 23. 从不同局域网中的远程服务器加载图像
- 24. 网格视图中图像的图像视图
- 25. 从网址加载图像
- 26. 从网站加载图像
- 27. 从列表中的服务器重复列表视图图像
- 28. 在网格视图中显示图像保存在服务器上
- 29. 从网格视图获取图像
- 30. 从资源中挑选图像并加载到网络视图
http://sunil-android.blogspot.in/2013/09/lazy-loading-image- download-from.html使用此链接..使用gridview代替listview –