图像随机变化
Images randomly change
我有以下问题:
我有一个显示很多 ImageView
的 GridView
,如果我开始滚动,项目会随机开始更改,因此错误的图像会出现在错误的位置,并且它们每次都会从位置到位置。
ImageAdapter
package com.zippyscore.pr0gramm.Adapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.zippyscore.pr0gramm.MainActivity;
import com.zippyscore.pr0gramm.Util.Constants;
import java.io.InputStream;
/**
* Created by Dominik on 16.01.2015.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return MainActivity.mainPagePostLoader.getPostList().size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
convertView = new ImageView(mContext);
vh = new ViewHolder();
vh.imageView = (ImageView) convertView;
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
new DownloadImageTask(vh.imageView).execute(MainActivity.mainPagePostLoader.getPosts().get(MainActivity.mainPagePostLoader.getPostList().get(position)).getThumb());
return convertView;
}
protected class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = "http://" + Constants.THUMB_DOMAIN + urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Pr0gramm", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
private class ViewHolder{
public ViewHolder(){}
public ImageView imageView;
//public TextView textView;
//Add any views you want to use in getView here
}
}
MainPagePostLoader
(应该无关紧要)
package com.zippyscore.pr0gramm.Network;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.AsyncTask;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.zippyscore.pr0gramm.CustomViews.ThumbImageView;
import com.zippyscore.pr0gramm.MainActivity;
import com.zippyscore.pr0gramm.Post.MainPagePost;
import com.zippyscore.pr0gramm.Util.GridRow;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by Dominik on 13.01.2015.
*/
public class MainPagePostLoader{
int chunksLoaded;
int lastPromoted;
private HashMap<String, MainPagePost> posts = new HashMap<String, MainPagePost>();
private List<String> postList = new ArrayList<String>();
private NetworkHelper networkHelper;
public MainPagePostLoader(){
chunksLoaded = 0;
lastPromoted = 0;
networkHelper = new NetworkHelper();
}
public HashMap<String, MainPagePost> getPosts(){
return this.posts;
}
public List<String> getPostList(){
return this.postList;
}
public void loadNextChunk(){
if(chunksLoaded==0){
String serverResponse = networkHelper.connect("http://pr0gramm.com/api/items/get?flags=1&promoted=1"); //TODO ADD FLAG SUPPORT
if(serverResponse!=null){
try {
JSONObject serverResponseJSON = new JSONObject(serverResponse);
if(serverResponseJSON.has("items")) {
JSONArray postsJSON = serverResponseJSON.getJSONArray("items");
for(int i = 0; i < postsJSON.length();i++){
JSONObject post = postsJSON.getJSONObject(i);
posts.put(String.valueOf(post.getInt("id")),new MainPagePost(post.getInt("id"),post.getInt("promoted"),post.getInt("up"), post.getInt("down"), post.getLong("created"), post.getString("image"),post.getInt("flags"),post.getInt("mark"), post.getString("source"),post.getString("thumb"),post.getString("user")));
postList.add(String.valueOf(post.getInt("id")));
MainActivity.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
MainActivity.imageAdapter.notifyDataSetChanged();
}
});
//addThumb(i);
}
}
chunksLoaded++;
} catch (JSONException e) {
Log.e("Pr0gramm","JSON Parse error : " + e.getMessage() + "|" + e.getCause());
}
}else{
Log.e("Pr0gramm", "Chunk loading error");
}
}else{
try {
String serverResponse = networkHelper.connect("http://pr0gramm.com/api/items/get?older=" + String.valueOf(posts.get(postList.size() - 1).getID()));
JSONObject serverResponseJSON = new JSONObject(serverResponse);
if(serverResponseJSON.has("items")){
JSONArray postsJSON = serverResponseJSON.getJSONArray("items");
for(int i = 0; i < postsJSON.length();i++){
JSONObject post = postsJSON.getJSONObject(i);
posts.put(String.valueOf(post.getInt("id")),new MainPagePost(post.getInt("id"),post.getInt("promoted"),post.getInt("up"), post.getInt("down"), post.getLong("created"), post.getString("image"),post.getInt("flags"),post.getInt("mark"), post.getString("source"),post.getString("thumb"),post.getString("user")));
postList.add(String.valueOf(post.getInt("id")));
MainActivity.imageAdapter.notifyDataSetChanged();
//addThumb(i + (postList.size()-1));
}
}
chunksLoaded++;
}catch(JSONException e){
Log.e("Pr0gramm", "JSON Parse error : " + e.getMessage() + "|" + e.getCause());
}
}
}
/*
private void addThumb(int id){
int currentRow = MainActivity.gridRows.size() - 1;
Point size = new Point();
MainActivity.defaultDisplay.getSize(size);
if(currentRow!=-1) {
if (MainActivity.gridRows.get(currentRow).getItemCount() < MainActivity.maxItemsPerRow) {
final ThumbImageView thumbImageView = new ThumbImageView(MainActivity.context, id);
thumbImageView.setLayoutParams(new RelativeLayout.LayoutParams(size.x / MainActivity.maxItemsPerRow, size.x / MainActivity.maxItemsPerRow));
thumbImageView.setX(MainActivity.gridRows.get(currentRow).getItemCount() * size.x / MainActivity.maxItemsPerRow);
thumbImageView.setY(currentRow * size.x / MainActivity.maxItemsPerRow);
MainActivity.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
MainActivity.rootLayout.addView(thumbImageView);
}
});
MainActivity.gridRows.get(currentRow).addItem();
} else {
MainActivity.gridRows.add(new GridRow());
addThumb(id);
}
}else{
MainActivity.gridRows.add(new GridRow());
addThumb(id);
}
} */
}
考虑为此使用 Square Picasso 库,而不是自己动手:
我有以下问题:
我有一个显示很多 ImageView
的 GridView
,如果我开始滚动,项目会随机开始更改,因此错误的图像会出现在错误的位置,并且它们每次都会从位置到位置。
ImageAdapter
package com.zippyscore.pr0gramm.Adapter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.zippyscore.pr0gramm.MainActivity;
import com.zippyscore.pr0gramm.Util.Constants;
import java.io.InputStream;
/**
* Created by Dominik on 16.01.2015.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return MainActivity.mainPagePostLoader.getPostList().size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh;
if (convertView == null) {
convertView = new ImageView(mContext);
vh = new ViewHolder();
vh.imageView = (ImageView) convertView;
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
new DownloadImageTask(vh.imageView).execute(MainActivity.mainPagePostLoader.getPosts().get(MainActivity.mainPagePostLoader.getPostList().get(position)).getThumb());
return convertView;
}
protected class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = "http://" + Constants.THUMB_DOMAIN + urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Pr0gramm", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
private class ViewHolder{
public ViewHolder(){}
public ImageView imageView;
//public TextView textView;
//Add any views you want to use in getView here
}
}
MainPagePostLoader
(应该无关紧要)
package com.zippyscore.pr0gramm.Network;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.AsyncTask;
import android.util.Log;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.zippyscore.pr0gramm.CustomViews.ThumbImageView;
import com.zippyscore.pr0gramm.MainActivity;
import com.zippyscore.pr0gramm.Post.MainPagePost;
import com.zippyscore.pr0gramm.Util.GridRow;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* Created by Dominik on 13.01.2015.
*/
public class MainPagePostLoader{
int chunksLoaded;
int lastPromoted;
private HashMap<String, MainPagePost> posts = new HashMap<String, MainPagePost>();
private List<String> postList = new ArrayList<String>();
private NetworkHelper networkHelper;
public MainPagePostLoader(){
chunksLoaded = 0;
lastPromoted = 0;
networkHelper = new NetworkHelper();
}
public HashMap<String, MainPagePost> getPosts(){
return this.posts;
}
public List<String> getPostList(){
return this.postList;
}
public void loadNextChunk(){
if(chunksLoaded==0){
String serverResponse = networkHelper.connect("http://pr0gramm.com/api/items/get?flags=1&promoted=1"); //TODO ADD FLAG SUPPORT
if(serverResponse!=null){
try {
JSONObject serverResponseJSON = new JSONObject(serverResponse);
if(serverResponseJSON.has("items")) {
JSONArray postsJSON = serverResponseJSON.getJSONArray("items");
for(int i = 0; i < postsJSON.length();i++){
JSONObject post = postsJSON.getJSONObject(i);
posts.put(String.valueOf(post.getInt("id")),new MainPagePost(post.getInt("id"),post.getInt("promoted"),post.getInt("up"), post.getInt("down"), post.getLong("created"), post.getString("image"),post.getInt("flags"),post.getInt("mark"), post.getString("source"),post.getString("thumb"),post.getString("user")));
postList.add(String.valueOf(post.getInt("id")));
MainActivity.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
MainActivity.imageAdapter.notifyDataSetChanged();
}
});
//addThumb(i);
}
}
chunksLoaded++;
} catch (JSONException e) {
Log.e("Pr0gramm","JSON Parse error : " + e.getMessage() + "|" + e.getCause());
}
}else{
Log.e("Pr0gramm", "Chunk loading error");
}
}else{
try {
String serverResponse = networkHelper.connect("http://pr0gramm.com/api/items/get?older=" + String.valueOf(posts.get(postList.size() - 1).getID()));
JSONObject serverResponseJSON = new JSONObject(serverResponse);
if(serverResponseJSON.has("items")){
JSONArray postsJSON = serverResponseJSON.getJSONArray("items");
for(int i = 0; i < postsJSON.length();i++){
JSONObject post = postsJSON.getJSONObject(i);
posts.put(String.valueOf(post.getInt("id")),new MainPagePost(post.getInt("id"),post.getInt("promoted"),post.getInt("up"), post.getInt("down"), post.getLong("created"), post.getString("image"),post.getInt("flags"),post.getInt("mark"), post.getString("source"),post.getString("thumb"),post.getString("user")));
postList.add(String.valueOf(post.getInt("id")));
MainActivity.imageAdapter.notifyDataSetChanged();
//addThumb(i + (postList.size()-1));
}
}
chunksLoaded++;
}catch(JSONException e){
Log.e("Pr0gramm", "JSON Parse error : " + e.getMessage() + "|" + e.getCause());
}
}
}
/*
private void addThumb(int id){
int currentRow = MainActivity.gridRows.size() - 1;
Point size = new Point();
MainActivity.defaultDisplay.getSize(size);
if(currentRow!=-1) {
if (MainActivity.gridRows.get(currentRow).getItemCount() < MainActivity.maxItemsPerRow) {
final ThumbImageView thumbImageView = new ThumbImageView(MainActivity.context, id);
thumbImageView.setLayoutParams(new RelativeLayout.LayoutParams(size.x / MainActivity.maxItemsPerRow, size.x / MainActivity.maxItemsPerRow));
thumbImageView.setX(MainActivity.gridRows.get(currentRow).getItemCount() * size.x / MainActivity.maxItemsPerRow);
thumbImageView.setY(currentRow * size.x / MainActivity.maxItemsPerRow);
MainActivity.activity.runOnUiThread(new Runnable() {
@Override
public void run() {
MainActivity.rootLayout.addView(thumbImageView);
}
});
MainActivity.gridRows.get(currentRow).addItem();
} else {
MainActivity.gridRows.add(new GridRow());
addThumb(id);
}
}else{
MainActivity.gridRows.add(new GridRow());
addThumb(id);
}
} */
}
考虑为此使用 Square Picasso 库,而不是自己动手: