我一直在阅读stackoverflow和this answer上的不同答案,并尝试实施他们的解决方案,但我仍然收到错误:
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"/>
项目的布局:
<?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);
}
}
}
答案 0 :(得分:14)
我从未遇到过这个问题,而且我很难为我复制。
这是因为您不首先将空适配器设置为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
答案 1 :(得分:0)
您收到错误是因为您在数据源(photoList)仍为空时设置了适配器。 并且还告诉您的Recycler视图它的大小不会改变(通过调用setHasFixedSize(true)),但是您要在查询的onCompletion回调中更改列表的大小。
因此,删除setHasFixedSize(true),填充列表后它应该可以工作。
编辑:我错了,方法中的大小与数据无关,但与回收者视图本身的大小无关。
答案 2 :(得分:0)
确保您已将LayoutManager
设置为RecyclerView
。希望它会有所帮助:)
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);
list.setLayoutManager(llm);
list.setAdapter( adapter );