【问题标题】:Issue in loading new Images in gridview在gridview中加载新图像的问题
【发布时间】:2018-01-24 08:10:07
【问题描述】:

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

网址

安装应用时有 7 个 url,所以它加载了 7 个图像。但刷新或重新打开应用程序后无法加载第 8 张图片

{"结果":[{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/Desert.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/Jellyfish.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/child.png"},{"url": "http://smilestechno.000webhostapp.com/ImagesUpload/clamber-512.png"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/image:52.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/image:52.jpg"},{"url":"http://smilestechno.000webhostapp.com/ImagesUpload/Temp20181016_051043.jpg"},{"url":"@ 987654328@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;
}
}

图像对象

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];
    }
};
}

即使在刷新或重新打开应用后,它也不会加载第 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>

【问题讨论】:

标签: android image gridview android-volley


【解决方案1】:

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

stringRequest.setShouldCache(false);

这样我们就可以停止保存缓存了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    相关资源
    最近更新 更多