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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…