【问题标题】:RecyclerView﹕ No adapter attached; skipping layoutRecyclerView:没有附加适配器;跳过布局
【发布时间】:2015-08-19 06:45:03
【问题描述】:

我一直在 stackoverflow 和 blog post 上阅读不同的答案,并尝试实施他们的解决方案,但我仍然收到错误:

RecyclerView:没有附加适配器;跳过布局

所以我在片段中的onCreateView 中初始化我的回收器视图,如下所示:

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"/>

还有一个item的布局:

<?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);
        }
    }
}

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

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

    这是因为您没有先将空适配器设置为 RecyclerView。您提供的博客并未确切说明如何。这是我平时的写作方式。

    我的适配器构造函数通常如下所示。

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

    并为对象数组列表编写public(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 实际上,我刚刚在晚上修复了我的两个初级开发人员的代码。错误日志是因为 recyclerview。如果有空的适配器或数据,它们会打印出错误日志。
    • @SH。你好。用photos.clear(); photos.addAll(photoList);代替photos = photoList有什么意义
    【解决方案2】:

    确保您已将 LayoutManager 设置为 RecyclerView。希望它会有所帮助:)

    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    list.setLayoutManager(llm);
    list.setAdapter( adapter );
    

    【讨论】:

      【解决方案3】:

      您收到错误是因为您在数据源 (photoList) 仍为空时设置了适配器。 并且还告诉你的回收器视图它的大小不会改变(通过调用 setHasFixedSize(true))但是你正在改变查询的 onCompletion 回调中列表的大小。 所以,删除 setHasFixedSize(true),一旦你填充列表,它应该可以工作。

      编辑:我错了,方法中的大小与数据无关,而是与回收器视图本身的大小有关。

      【讨论】:

      • 我已删除 setHasFixedSize(true) 但它仍然给我同样的错误:/
      • 您也可以粘贴您的适配器代码吗?我想当然地认为你在回调之前的某个地方初始化了 photoList?
      • 这个答案是错误的。 setHasFixedSize() 是在讨论每行的视图大小,而不是适配器拥有的项目数。 Please refer to the docs
      • @m.stevanovic 我在我的问题中添加了适配器代码。
      • 一切似乎都很好(除了通知适配器构造函数中的更改,但我怀疑这是这里的问题)。唯一需要尝试的是在 onCompletion 回调中重新创建和swap 适配器,看看会发生什么。
      猜你喜欢
      • 2018-06-12
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 2019-04-07
      • 2021-11-04
      • 2020-01-09
      • 2017-12-01
      • 2021-06-19
      相关资源
      最近更新 更多