RecyclerView根本没有更新

时间:2016-04-14 22:55:43

标签: android android-recyclerview recycler-adapter

我的任务很简单,但我不明白为什么它不起作用。我需要将images放在GridView中。我决定使用RecyclerView。所以我有一些我放置它的地方:

  <LinearLayout
  ..>
  ...

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ebebeb">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view1"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</FrameLayout>
</LinearLayout>

在我的Activity我声明:

    images = new ArrayList<>();
    recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view1);

    adapter = new ImageGridAdapter(Place.this, images);
    manager = new GridLayoutManager(this, 3);
    recyclerView.addItemDecoration(new GridSpacingItemDecoration(3, 8, true));    //   for grid with equal spacing
    recyclerView.setLayoutManager(manager);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);

当我的所有viewsobjects被初始化时,使用Retrofit2我获取数据并尝试更新之前创建的适配器。

        @Override
        public void onResponse(Call<PlaceModel> call, Response<PlaceModel> response) {
            placeProperties = response.body().getData();
            images = placeProperties.getPhotos();
            System.out.println("size = " + images.size());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    updateUI(placeProperties);

                    images.clear();
                    images.addAll(placeProperties.getPhotos());
                    adapter.notifyItemChanged(0, images.size());
                    //just adapter.notifyDataSetChanged(); also doesn't give result;

                }
            });

        }

尽管如此,我的updateUI方法工作正常,并使用来自placeProperties对象的数据更新视图。还有image.size()>1

这是Adapter课程。 https://gist.github.com/burnix/aa27efe9586213852c108f3d79f3f69d

根据日志,我的onBindViewHolder方法无效。

我错过了什么?为什么我的RecyclerView没有显示任何图片?

P.S。我已经阅读了所有类似的问题,但这些问题并没有帮助。希望我只是累了,忽略了一些非常简单的事情......

1 个答案:

答案 0 :(得分:1)

它不起作用的原因是您正在重新分配RecylerView数据源的引用。

您需要做的就是删除此行:

images = placeProperties.getPhotos();