gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:31:39 25 4
gpt4 key购买 nike

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

RecyclerView﹕ No adapter attached; skipping layout

所以我在 fragment 中的 onCreateView 中初始化我的回收器 View ,如下所示:

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

fragment 中的布局:

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

最佳答案

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

这是因为您没有先将空适配器设置为 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);
}

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

adapter = new ProfilePhotoRecyclerViewAdapter(getActivity());

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

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

adapter.setPhotos(photoList);
}

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

干杯,

上海

关于android - RecyclerView:没有附加适配器;跳过布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32088258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com