Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
569 views
in Technique[技术] by (71.8m points)

android - RecyclerView﹕ No adapter attached; skipping layout

I have been reading the different answers here on stackoverflow and on this blog post and tried to implement their solutions but I am still getting the error:

RecyclerView﹕ No adapter attached; skipping layout

So I initialize my recycler view in onCreateView in my fragment like this:

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;

}

}

The function 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);
    }

The layout in the fragment:

 <android.support.v7.widget.RecyclerView
  android:id="@+id/grid_view"
  android:layout_width="match_parent"
  android:layout_height="match_parent"/>

And the layout of an 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>

What am I doing wrong?

EDIT: here is the adapter:

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);
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I never had this issue and it was really hard to replicate for me. ??

It is because you aren't setting empty adapter to RecyclerView first. The blog you give didn't exactly told how. Here is how I normally write.

My adapter constructor normally looks like this.

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

And write public (setter) method for the object array list.

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

And in your fragment, you don't have to initialise with photo array anymore.

adapter = new ProfilePhotoRecyclerViewAdapter(getActivity());

Just call the setter method from adapter inside your API response's onCompletion method.

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

  adapter.setPhotos(photoList);
}

I think this might solve. Feel free to ask and discuss.

Cheers,

SH


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...