RecyclerView:未附加适配器;跳过布局。

6

我一直在阅读stackoverflow上的不同答案以及这篇博客文章,并尝试实现它们的解决方案,但我仍然遇到以下错误:

RecyclerView﹕ No adapter attached; skipping layout

所以我在我的片段中的onCreateView中这样初始化我的recycler视图:

public class ProfileFragment extends Fragment {

private ModelUser user;

private View view;
private RecyclerView gridView;
private ProfilePhotoRecyclerViewAdapter adapter;
private List<ModelAttachment> photoList = new ArrayList<ModelAttachment>();

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

    view = inflater.inflate(R.layout.fragment_profile, container, false);

    initializeRecyclerView();

    //Get extra info for user
    QueryAPI query = new QueryAPI();
    query.userExtra(user, new QueryAPI.ApiResponse<ModelUser>() {
        @Override
        public void onCompletion(ModelUser result) {
            user = result;
                photoList.clear();
                photoList.addAll(0,user.getPhotos());

                adapter.notifyDataSetChanged();

            }
        });

     return view;

}

}

函数 initializeRecyclerView

void initializeRecyclerView() {
        gridView = (RecyclerView) view.findViewById(R.id.grid_view);
        StaggeredGridLayoutManager gridLayoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.HORIZONTAL );
        adapter = new ProfilePhotoRecyclerViewAdapter(getActivity(), photoList);
        gridView.setAdapter(adapter);
        gridView.setLayoutManager(gridLayoutManager);
        gridView.setHasFixedSize(true);
    }

片段中的布局:
 <android.support.v7.widget.RecyclerView
  android:id="@+id/grid_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

和一个项目的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/imageView"
    android:contentDescription="@string/image_description"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true" />

 </LinearLayout>

我应该做什么才能解决问题?
编辑:这是适配器:
public class ProfilePhotoRecyclerViewAdapter extends  RecyclerView.Adapter<ProfilePhotoRecyclerViewAdapter.ViewHolder> {

    private LayoutInflater inflater;
    private List<ModelAttachment> photos; //data
    private Activity listContext;
    private int listRowLayoutId;
    ImageLoader imageLoader = AppController.getInstance().getImageLoader();


    public ProfilePhotoRecyclerViewAdapter(Activity context, List<ModelAttachment> objs) {
        photos = objs;
        listContext = context;
        this.notifyDataSetChanged();
    }

    @Override
    public ProfilePhotoRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int i) {

        View itemView = LayoutInflater.from(listContext).inflate(R.layout.profile_photogrid_item, parent, false);

        return new ProfilePhotoRecyclerViewAdapter.ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(ProfilePhotoRecyclerViewAdapter.ViewHolder viewHolder, int i) {

        ModelAttachment photo = photos.get(i);
        viewHolder.imageView.setImageUrl(photo.getUrl(), imageLoader);
    }

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


    public static class ViewHolder extends RecyclerView.ViewHolder {

        ModelAttachment photo;
        public NetworkImageView imageView;

        public ViewHolder(View itemView) {
            super(itemView);
            this.imageView = (NetworkImageView) itemView.findViewById(R.id.imageView);
        }
    }
}
3个回答

20

我从来没有遇到过这个问题,对我来说很难复制。

这是因为你没有先将空适配器设置给 RecyclerView。你提供的博客并没有详细说明如何做到这一点。以下是我通常的写法。

我的适配器构造函数通常是这样的。

public ProfilePhotoRecyclerViewAdapter(Activity context) {
  photos = new ArrayList<>();
  listContext = context;
}

并为对象数组列表编写公共(setter)方法。

public void setPhotos(List< ModelAttachment> photoList) {
  photos.clear();
  photos.addAll(photoList);
  this.notifyItemRangeInserted(0, photos.size() - 1);
}

并且在您的片段中,您不再需要使用照片数组进行初始化。

adapter = new ProfilePhotoRecyclerViewAdapter(getActivity());

只需在API响应的onCompletion方法中调用适配器中的setter方法。

@Override
public void onCompletion(ModelUser result) {
  photoList.clear();
  photoList.addAll(0,user.getPhotos());

  adapter.setPhotos(photoList);
}

我认为这可能会解决问题。随时欢迎提问和讨论。

祝好,

SH


谢谢您的回答,不幸的是我尝试了,但仍然得到相同的错误 :/ - Kalianey
但是现在,我试着删除我放置在整个片段周围的滚动视图,它可以工作了。虽然我仍然可以在日志中看到错误,但我可以看到图片。不过我不确定原因... - Kalianey
等等...所以你的问题不仅仅是那个错误日志吗?你能否按照我建议的发布完整的代码编辑吗? - Swan
@KaliAney 实际上,我在晚上修复了我的两个初级开发人员的代码,就像这样。错误日志是由于recyclerview引起的。如果适配器或数据为空,他们会打印出错误日志。 - Swan
2
@SH. 你好。使用 photos.clear(); photos.addAll(photoList); 而不是 photos = photoList 的意义是什么? - Alexander Zar
显示剩余7条评论

1
确保您已将LayoutManager设置为RecyclerView。希望能有所帮助 :)
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(llm);
list.setAdapter( adapter );

0
你之所以会出现错误,是因为在数据源(photoList)仍为空时设置了适配器。并且通过调用setHasFixedSize(true)告诉您的回收站视图其大小不会改变,但是您正在更改查询的onCompletion回调中列表的大小。
因此,请删除setHasFixedSize(true),一旦填充列表,它就应该正常工作。
编辑:我错了,方法中的大小与数据无关,而与回收站视图本身的大小有关。

2
我已经删除了 setHasFixedSize(true) 但它仍然给我相同的错误 :/ - Kalianey
你能同时粘贴一下你的适配器代码吗?我认为在回调函数之前你已经初始化了photoList了吧? - m.stevanovic
1
这个答案是错误的。setHasFixedSize() 是关于每一行视图大小的设置,而不是适配器中项目数量的设置。请参考文档 - Lawrence Choy
@m.stevanovic 我已经在我的问题中添加了适配器代码。 - Kalianey
除了通知适配器构造函数中的更改(但我怀疑这不是问题所在),一切似乎都很完美。唯一剩下要尝试的就是在 onCompletion 回调中重新创建并交换适配器,然后看看会发生什么。 - m.stevanovic
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接