在gridview中加载新图像时出现问题

时间:2018-01-24 08:10:08

标签: android image gridview android-volley

我制作的应用程序可以在gridview中加载来自服务器的图像。我使用Volley进行Http请求,滑行下载图像。我可以在安装app并第一次打开时加载图像,但是当添加新图像时,它无法加载新图像。

网址

安装应用时有7个网址,因此它加载了7张图片。但刷新或重新打开应用后,无法加载第8张图片

  

{"导致":[{" URL":" http://smilestechno.000webhostapp.com/ImagesUpload/Desert.jpg"},{" URL":& #34; http://smilestechno.000webhostapp.com/ImagesUpload/Jellyfish.jpg"},{" URL":" http://smilestechno.000webhostapp.com/ImagesUpload/child.png"},{" URL":& #34; http://smilestechno.000webhostapp.com/ImagesUpload/clamber-512.png"},{" URL":" http://smilestechno.000webhostapp.com/ImagesUpload/image:52.jpg"},{" URL":& #34; http://smilestechno.000webhostapp.com/ImagesUpload/image:52.jpg"},{" URL":" http://smilestechno.000webhostapp.com/ImagesUpload/Temp20181016_051043.jpg"},{" URL":& #34; http://smilestechno.000webhostapp.com/ImagesUpload/My Children20182917_022900.jpg"},{" url":" http://smilestechno.000webhostapp.com/ImagesUpload/MyChildren1412414122.jpg"}]}

图片细分,其中显示gridview

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
  //  return inflater.inflate(R.layout.fragment_images, container, false);
    View v = inflater.inflate(R.layout.fragment_images, container, false);
    FloatingActionButton btnCamera = (FloatingActionButton) v.findViewById(R.id.btnCamera);
    FloatingActionButton btnFolder = (FloatingActionButton) v.findViewById(R.id.btnFolder);
    refreshMainGrid = (SwipeRefreshLayout) v.findViewById(R.id.refreshManGrid);

    imgGrid = (GridView) v.findViewById(R.id.imgGrid);
    getData();
    imgGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Intent intent = new Intent(getContext(), SingleImage.class);
            intent = intent.putExtra("id", (ImageObject) myGridAdapter.getItem(position));
            startActivity(intent);
        }
    });

    refreshMainGrid.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            refreshData();
        }
    });

    return v;

}

private void getData(){
    RequestQueue queue = Volley.newRequestQueue(getContext());
    StringRequest stringRequest =  new StringRequest(Request.Method.GET, IMAGE_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.optJSONArray("result");
                if (jsonArray != null && jsonArray.length() > 0) {
                    ArrayList<ImageObject> imageObjects = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonitem = jsonArray.optJSONObject(i);
                        imageObjects.add(new ImageObject(jsonitem));
                    }
                    myGridAdapter = new MyGridAdapter(getContext(), imageObjects);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imgGrid.setAdapter(myGridAdapter);
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(6000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(stringRequest);
}
private void refreshData(){
    RequestQueue queue = Volley.newRequestQueue(getContext());
    StringRequest stringRequest =  new StringRequest(Request.Method.GET, IMAGE_URL, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.optJSONArray("result");
                if (jsonArray != null && jsonArray.length() > 0) {
                    ArrayList<ImageObject> imageObjects = new ArrayList<>();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonitem = jsonArray.optJSONObject(i);
                        imageObjects.add(new ImageObject(jsonitem));
                    }
                    myGridAdapter = new MyGridAdapter(getContext(), imageObjects);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            imgGrid.setAdapter(myGridAdapter);
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    stringRequest.setRetryPolicy(new DefaultRetryPolicy(6000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    queue.add(stringRequest);
    refreshMainGrid.setRefreshing(false);
}

MyGridAdapter

public class MyGridAdapter extends BaseAdapter {
private Context context;
private ArrayList<ImageObject> imageObjects;

private LayoutInflater mLayoutInflate;


public MyGridAdapter(Context context, ArrayList<ImageObject> imageObjects) {
    this.context = context;
    this.imageObjects = imageObjects;

    this.mLayoutInflate = LayoutInflater.from(context);
}

public int getCount() {
    if (imageObjects != null) return imageObjects.size();
    return 0;
}

@Override
public Object getItem(int position) {
    if (imageObjects != null && imageObjects.size() > position)
        return imageObjects.get(position);

    return null;
}

@Override
public long getItemId(int position) {
    if (imageObjects != null && imageObjects.size() > position)
        return imageObjects.get(position).getId();
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {

        viewHolder = new ViewHolder();

        convertView = mLayoutInflate.inflate(R.layout.imageitem, parent,
                false);
        viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    ImageObject imageObject = (ImageObject) getItem(position);
    if (imageObject != null) {
        Glide
                .with(context)
                .load(imageObject.getImageUrl())
                .centerCrop()
                .crossFade()
                .into(viewHolder.imageView);
    }

    return convertView;
}
}

ImageObject

public class ImageObject implements Parcelable{
private int id;
private String imageUrl;

public ImageObject(Parcel in){
    id = in.readInt();
    imageUrl = in.readString();

}

public  ImageObject(JSONObject jsonObject){
    if(jsonObject == null) return;
    this.id = jsonObject.optInt("id");
    this.imageUrl = jsonObject.optString("url");
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeString(imageUrl);
}
public static final ImageObject.Creator<ImageObject> CREATOR = new ImageObject.Creator<ImageObject>(){

    @Override
    public ImageObject createFromParcel(Parcel in) {
        return new ImageObject(in);
    }

    @Override
    public ImageObject[] newArray(int size) {
        return new ImageObject[size];
    }
};
}

enter image description here

即使刷新或重新打开应用程序后,它也不会加载第8张图像。 还有一个问题是它从底部加载图像而不是从顶部项目加载图像。

图片xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.smiles.mychildren.Images"
android:orientation="vertical">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/refreshManGrid"
    android:layout_marginTop="110dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

      <GridView
        android:id="@+id/imgGrid"
        android:numColumns="auto_fit"
          android:columnWidth="135dp"
          android:layout_marginTop="110dp"
          android:clipToPadding="false"
          android:padding="0dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
          android:layout_weight="2"
          android:verticalSpacing="0dp"
          android:horizontalSpacing="0dp"
          android:stretchMode="none"
          android:stackFromBottom="false">
    </GridView>
</android.support.v4.widget.SwipeRefreshLayout>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 问题在于Volley缓存。

stringRequest.setShouldCache(false);

通过这个我们可以停止保存缓存。