Recyclerview图像加载奇怪的行为

时间:2016-01-14 23:21:27

标签: java android android-recyclerview okhttp

我使用okhttp从http请求中获取数据并使用它来填充RecyclerView,数据只是我用Picasso加载的一个图像和我放在CardView中的一些文本。

  • 第一个问题是,有时当我打开应用程序时,只有当我从可见区域滚动行并向后滚动时才开始下载/显示图像,有时它会跳过一些行并将空格留空直到我再次向下滚动,如果我不这样做,我只看到空格

  • 第二个问题是滚动时似乎正在替换图像,当我滚动到一个新项目时,我可以看到来自不同行的旧图像一秒钟然后它被右边的替换图像。

我试图在任何地方寻找解决方案而没有任何修复它,继承了整个活动代码:

public class MainActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;
    private List<GalleryItem> mGalleryItems;
    GalleryAdapter adapter;
    private boolean loading = true;
    int pastVisiblesItems, visibleItemCount, totalItemCount;
    LinearLayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = (RecyclerView) findViewById(R.id.grid_view);
        mGalleryItems = new ArrayList<>();
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        getItems("http://www.reddit.com/r/aww.json");
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0) {
                    visibleItemCount = mLayoutManager.getChildCount();
                    totalItemCount = mLayoutManager.getItemCount();
                    pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
                    if (loading) {
                        if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                            loading = false;
                            Log.v("...", "Last Item Wow !");
                            getItems("https://www.reddit.com/r/aww.json?after=t3_40x6ke");
                        }
                    }
                }
            }
        });

        adapter = new GalleryAdapter(mGalleryItems, this);
        mRecyclerView.setAdapter(adapter);

    }


    @Override
    public void onDestroy(){
        super.onDestroy();

    }
    public void getItems(String url){
        OkHttpClient client = new OkHttpClient();
        File cacheDirectory = new File(MainActivity.this.getCacheDir(), "http");
        int cacheSize = 10 * 1024 * 1024;
        Cache cache = new Cache(cacheDirectory, cacheSize);
        client.setCache(cache);
        Request request = new Request.Builder()
                .url(url)
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                final String json = response.body().string();
                if (response.isSuccessful()) {
                        try {
                            mGalleryItems.addAll(parseItems(json));
                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.i("BEHS", e.getMessage());
                        }
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                adapter.notifyDataSetChanged();

                            }
                        });
                }
            }
        });

    }

    private List<GalleryItem> parseItems(String json) throws JSONException {
        JSONObject jsonMovie = new JSONObject(json);
        JSONObject firstData = jsonMovie.getJSONObject("data");
        JSONArray children = firstData.getJSONArray("children");

        List<GalleryItem> items = new ArrayList();

        for(int i = 2; i < children.length(); i++ ){
            JSONObject jsonObject = children.getJSONObject(i);
            JSONObject childrenObject = jsonObject.getJSONObject("data");
            GalleryItem item = new GalleryItem();
            item.setTitle(childrenObject.getString("title"));
            item.setUrl(childrenObject.getString("url"));
            item.setThumbnail(childrenObject.getString("thumbnail"));
            //if(!childrenObject.getString("url").endsWith(".jpg")){
         //       continue;
       //     }else{
                items.add(item);
     //       }
        }


        return items;
    }



    private class GalleryHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
        private ImageView mImageView;
        private TextView mTextView;
        private String mUrl;

        public GalleryHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            mImageView = (ImageView) itemView.findViewById(R.id.country_photo);
            mTextView = (TextView) itemView.findViewById(R.id.country_name);
        }


        public void bindImage(GalleryItem item ){
            Picasso.with(MainActivity.this).load(item.getThumbnail()).tag(MainActivity.this).into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    Palette pallete = Palette.from(bitmap).generate();
                    int color = pallete.getMutedColor(MainActivity.this.getResources().getColor(android.R.color.black));
                    mTextView.setBackgroundColor(color);
                    mImageView.setImageBitmap(bitmap);
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });
        }

        public void bindUrl(String url){
            mUrl = url;
        }

        @Override
        public void onClick(View view) {
            Intent i = new Intent(MainActivity.this, ItemActivity.class);
            i.putExtra("url", mUrl);
            Log.i(mUrl, mUrl);
            startActivity(i);

        }
    }

    private class GalleryAdapter extends RecyclerView.Adapter<GalleryHolder>{
        private List<GalleryItem> mImages;
        private Context mContext;

        public GalleryAdapter(List<GalleryItem> images, Context context){
            mContext = context;
            mImages = images;
        }

        @Override
        public GalleryHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            View view = inflater.inflate(R.layout.list_item, parent, false);
            return new GalleryHolder(view);

        }

        @Override
        public void onBindViewHolder(GalleryHolder holder, int position) {
            holder.mTextView.setText(mImages.get(position).getTitle());
            holder.bindImage(mImages.get(position));
            holder.bindUrl(mImages.get(position).getUrl());

        }
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return mImages.size();
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您似乎是异步图像加载陷阱的牺牲品。

问题#1:

您可以看到图像工作者仅在onBindViewHolder触发时触发。这意味着在绘制视图时会发生图像的实际下载/获取。如果没有关于GalleryItem中发生的事情的更多信息,我猜你的应用程序在后台时,图像字节数据被清除/垃圾收集 - 并且由于绑定发生在滚动期间,这就是为什么你没有看到任何图像。

或许可以查看图片缓存:http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

问题#2:

如果您不想查看旧图片,请将图片视图位图设置为null,作为onBindViewHolder中的第一项。

在伪代码中:

...
@Override
public void onBindViewHolder(GalleryHolder holder, int position) {
    holder.imageView.setBitmap(null);
}
...