如何在片段中按下背面清除图像缓存?

时间:2017-03-21 10:37:58

标签: android android-fragments caching

我有一个从MainActivity调用的片段。此Fragment获取图像URL的JSON文件并在屏幕上显示图像。 我使用Fresco库下载这些图像,然后弹出作为图像列表视图。图像由Fresco Library自动缓存。

我想从Fragment执行背压时删除所有缓存的图像。 当用户向下滚动列表时,图像立即被缓存,当用户在片段中执行backPress时,我想要解除所有图像的隐藏

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view  = inflater.inflate(R.layout.fragment_gallery, container, false);
    private List<gallery_adapter> gallery_adapterList = new ArrayList<>();
    ListView list = (ListView) view.findViewById(R.id.galleryList);

    gallery_adapterList.clear();
    if(isNetworkConnected()){
        new galleryImages().execute("http://www.myurl.json");
    }else
        Toast.makeText(context,"No Internet Connection!",Toast.LENGTH_SHORT).show();

    return view;
}






public class galleryImages extends AsyncTask<String, String, String> {

    HttpURLConnection connection;
    BufferedReader reader;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            String str = builder.toString();
            return str;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if(s!=null){


            try {
                JSONObject parent = new JSONObject(s);
                JSONArray images = parent.getJSONArray("images");

                if(images!=null){

                    for(int i=0; i<images.length();i++){
                        JSONObject child = images.getJSONObject(i);
                        gallery_adapterList.add(new gallery_adapter(child.getString("url"),child.getString("text")));
                    }
                    displayList();

                }else
                    Toast.makeText(context,"error",Toast.LENGTH_SHORT).show();

            } catch (JSONException e) {
                Toast.makeText(context, "Images not yet added. Try later",Toast.LENGTH_SHORT).show();
            }


        }else
            Toast.makeText(context, "Images not yet added. Try later",Toast.LENGTH_SHORT).show();
    }
}

显示列表方法

private void displayList() {
    ArrayAdapter<gallery_adapter> adapter = new galleryAdapterList();
    list.setAdapter(adapter);
}

public class galleryAdapterList extends ArrayAdapter<gallery_adapter> {

    galleryAdapterList() {
        super(context, R.layout.gallery_item, gallery_adapterList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.gallery_item, parent, false);
        }

        gallery_adapter current = gallery_adapterList.get(position);


        TextView mText=(TextView) convertView.findViewById(R.id.galleryText);
        mText.setText(current.getText());


        uri = Uri.parse(current.getUrl());
//code to remove images from cache
        ImagePipeline imagePipeline = Fresco.getImagePipeline();
        imagePipeline.evictFromCache(uri);     
//code ends here


        SimpleDraweeView mImage = (SimpleDraweeView) convertView.findViewById(R.id.galleryImage);
        mImage.setImageURI(uri);


        return convertView;
    }

}

2 个答案:

答案 0 :(得分:1)

我认为你是什么this但是如果我为什么要清除缓存呢?因为下次用户打开活动时他需要再次等待json响应

答案 1 :(得分:0)

以下是您可以选择的选项:

1.您可以在Fragment中缓存backpress事件并调用您的函数来清除缓存图像Handle Back Press

2.Track Fragment by it on onAttach(Context),意思是当你需要写FragmentManager.addOnBackStackChangedListener();时,如果你用addtoBackStack添加了你的片段,它就会被调用。