使用Picasso加载列表中的图像崩溃滚动应用程序

时间:2015-09-20 20:44:22

标签: android android-listview picasso

我一直在使用#!/bin/bash # Second subshell will get the PID of the first one through the pipe. # It will be able to kill the whole script by killing the first subshell. # Create a temporary named pipe (it's safe, conflicts will throw an error). pipe=$(mktemp -u) if ! mkfifo $pipe; then echo "ERROR: debug tracing pipe creation failed." >&2 exit 1 fi # Attach it to file descriptor 3. exec 3<>$pipe # Unlink the named pipe. rm $pipe (echo $BASHPID >&3; tee /dev/stderr) | (./script.sh; r=$?; kill $(head -n1 <&3); exit $r) | tee /dev/stderr exit ${PIPESTATUS[1]} 使用返回图片的网址填充Picasso。这些URL是从API调用返回的。

适配器

listview

我已经做了很多关于此的阅读,并且我尝试使用public class RecipeAdapter extends BaseAdapter implements ListAdapter { private final Activity activity; private final JSONArray jsonArray; public RecipeAdapter (Activity activity, JSONArray jsonArray){ assert activity !=null; assert jsonArray != null; this.jsonArray = jsonArray; this.activity = activity; } @Override public int getCount(){ if(null==jsonArray) return 0; else return jsonArray.length(); } @Override public JSONObject getItem(int position){ if(null==jsonArray) return null; else return jsonArray.optJSONObject(position); } @Override public long getItemId(int position){ return position; } @Override public View getView (int position, View v, ViewGroup parent){ if (v == null) { v = View.inflate(activity, R.layout.recipe_item, null); } CircularImageView icon = (CircularImageView)v.findViewById(R.id.recipeIcon); TextView title = (TextView) v.findViewById(R.id.recipeTitle); TextView supplier = (TextView) v.findViewById(R.id.supplier); JSONObject JSdata = getItem(position); if(null!=JSdata){ try { if (JSdata.has("title")) { title.setText(JSdata.getString("title")); } if (JSdata.has("publisher")) { supplier.setText(JSdata.getString("publisher")); } if (JSdata.has("image_url")) { String image = JSdata.getString("image_url"); String[] imagearray = image.split("\\."); String extension = imagearray[imagearray.length - 1]; String tag = new String(); if (extension.equals("jpg")) { if (URLUtil.isValidUrl(image)) { Picasso.with(activity) .load(image) .centerCrop() .tag(tag) .error(R.drawable.icon01) .resize(50, 50) .into(icon); } else { Picasso.with(activity) .load(R.drawable.icon01) .centerCrop() .resize(50, 50) .tag(tag) .into(icon); } } } } catch (JSONException e) { Toast.makeText(activity, "Error finding recipes", Toast.LENGTH_SHORT).show(); e.printStackTrace(); } } final ImageView fav = (ImageView)v.findViewById(R.id.favbutton); fav.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fav.setImageResource(R.drawable.ic_favorite_white_24dp); Toast.makeText(activity, "Added to favourites", Toast.LENGTH_SHORT).show(); } }); return v; } } 方法和滚动侦听器:

.tag()

我虽然这可能会解决问题,但是当我滚动大型列表时,它会导致应用程序崩溃。 logcat的:

  class Populate extends AsyncTask<String, String, JSONArray> {

    protected void onPreExecute() {
        progress = ProgressDialog.show(RecipeSearch.this, "Finding Recipes", "Searching....", true);
    }

    @Override
    protected JSONArray doInBackground(String... urls) {
        JSONParser recipeParse = new JSONParser();
        String rawJSON = recipeParse.getJSON(urls[0]);

        try {

            if (rawJSON != null) {
                JSONObject object = new JSONObject(rawJSON);
                JSONArray jArray = object.getJSONArray("recipes");
                return jArray;
            } else {
                JSONArray jArray = null;
                return jArray;
            }

        } catch(Exception e) {
            System.out.println(e);
            Toast.makeText(getApplicationContext(), "Error Searching for Recipes",
                    Toast.LENGTH_SHORT).show();

         JSONArray jArray = null;
            return jArray;
        }
        //do http request and add objects to array here.
        //need to do a custom adapter
       // return null;
    }

    @Override
    protected void onPostExecute(JSONArray jArray) {

        if (jArray != null) {
            final ListView recipes = (ListView) findViewById(R.id.recipeView);

            recipes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                    JSONObject selected = (JSONObject) (recipes.getItemAtPosition(position));

                    String url = null;
                    try {
                        url = selected.getString("source_url");
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_SHORT).show();
                    }
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);

                }
            });
           final String tag ="";
            recipes.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {


                    Picasso picasso = Picasso.with(getApplicationContext());
                    if (scrollState == SCROLL_STATE_IDLE ||
                            scrollState == SCROLL_STATE_TOUCH_SCROLL) {
                        picasso.resumeTag(tag);
                    } else {
                        picasso.pauseTag(tag);
                    }
                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                    Picasso picasso = Picasso.with(getApplicationContext());
                    picasso.pauseTag(tag);
                }
            });

            recipeAdapter = new RecipeAdapter(RecipeSearch.this, jArray);//jArray is your json array
            recipes.setAdapter(recipeAdapter);

        } else {
            Toast.makeText(getApplicationContext(), "No Recipes Found", Toast.LENGTH_SHORT).show();
            finish();
        }



        progress.dismiss();
    }
}

欢迎任何帮助或建议

1 个答案:

答案 0 :(得分:0)

我对CircularImageView,图片网址和毕加索有同样的问题。该 问题在于CircularImageView正在加载占位符图像然后是您请求的占位符图像。我在Picasso查询中使用noPlaceholder()解决了这个问题。

您应该可以通过添加noPlaceholder()来修复上述代码

if (URLUtil.isValidUrl(image)) {
    Picasso.with(activity)
        .load(image)
        .centerCrop()
        .tag(tag)
        .error(R.drawable.icon01)
        .resize(50, 50)
        .noPlaceholder()
        .into(icon);

} else {
     Picasso.with(activity)
         .load(R.drawable.icon01)
         .centerCrop()
         .resize(50, 50)
         .tag(tag)
         .noPlaceholder()
         .into(icon);
}

Picasso Github https://github.com/square/picasso/issues/931

详细说明了这个问题