很抱歉有这个问题,但我开始学习Firebase UI,连续两天我都在片段中填充RecyclerView。我试图在onViewCreated和onCreateView方法中移动我的代码,但我认为这不是问题所在,在日志中它只抛出了一个错误:
2020-02-18 15:08:26.730 4467-4467/com.social.voteup E/RecyclerView: No adapter attached; skipping layout
仔细检查所有链接到xml文件的内容。这是我的代码:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
return view;
}
论视图创建方法
mRecyclerView=view.findViewById(R.id.posts_recyclerview(;mSwipeRefreshLayout=view.findViewById(R.id.refreshLayout(;
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
setupAdapter();
// Refresh Action on Swipe Refresh Layout
mSwipeRefreshLayout.setOnRefreshListener(() -> mAdapter.refresh());
我在片段中使用适配器,如下所示:
private void setupAdapter() {
// Init Paging Configuration
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(2)
.setPageSize(10)
.build();
// Init Adapter Configuration
FirestorePagingOptions<Post> options = new FirestorePagingOptions.Builder<Post>()
.setLifecycleOwner(this)
.setQuery(mQuery, config, Post.class)
.build();
// Instantiate Paging Adapter
mAdapter = new FirestorePagingAdapter<Post, PostViewHolder>(options) {
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = getLayoutInflater().inflate(R.layout.item_post, parent, false);
return new PostViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull PostViewHolder viewHolder, int i, @NonNull Post post) {
// Bind to ViewHolder
viewHolder.bind(post);
}
@Override
protected void onError(@NonNull Exception e) {
super.onError(e);
Log.e("MainActivity", Objects.requireNonNull(e.getMessage()));
}
protected void onLoadingStateChanged(@NonNull LoadingState state) {
switch (state) {
case LOADING_INITIAL:
case LOADING_MORE:
mSwipeRefreshLayout.setRefreshing(true);
break;
case LOADED:
mSwipeRefreshLayout.setRefreshing(false);
break;
case ERROR:
mSwipeRefreshLayout.setRefreshing(false);
break;
case FINISHED:
mSwipeRefreshLayout.setRefreshing(false);
break;
}
}
};
// Finally Set the Adapter to mRecyclerView
mRecyclerView.setAdapter(mAdapter);
}
以下是pastebin上的完整源文件:https://pastebin.com/YkzKRuUD
在On-View-Created方法中添加这行代码。
您已经创建了一个适配器,但尚未将适配器连接到回收器查看
mRecyclerView = view.findViewById(R.id.posts_recyclerview);
mSwipeRefreshLayout = view.findViewById(R.id.refreshLayout);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
mRecyclerView.setAdapter(new setupAdapter());
// Refresh Action on Swipe Refresh Layout
mSwipeRefreshLayout.setOnRefreshListener(() -> mAdapter.refresh());