我收到RecyclerView: No adapter attached; skipping layout
错误,而我的ViewPager没有显示RecyclerView列表。我在片段的onCreate方法中创建了一个新的适配器对象,这应该是正确的方法。我在这里做错了什么?
片段文件:
public class HomeCurrentFragment extends Fragment {
private RecyclerView recView;
private EventAdapter eventAdapter;
public HomeCurrentFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
eventAdapter = new EventAdapter(DummyData.getListData());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_current, container, false);
// Get the recview for id
recView = (RecyclerView) view.findViewById(R.id.rec_list);
// Set the layout to linear (as to grid)
recView.setLayoutManager(new LinearLayoutManager(getActivity()));
recView.setAdapter(eventAdapter);
// Inflate the layout for this fragment
return view;
}
}
RecycleAdapter文件:
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.EventHolder> {
private ArrayList<EventItem> listData;
public static class EventHolder extends RecyclerView.ViewHolder {
private TextView title;
private ImageView icon;
private View container;
public EventHolder(View itemView) {
super(itemView);
this.title = (TextView) itemView.findViewById(R.id.lbl_item_text);
this.icon = (ImageView) itemView.findViewById(R.id.im_item_icon);
this.container = itemView.findViewById(R.id.cont_item_root);
}
}
public EventAdapter(ArrayList<EventItem> listData) {
this.listData = listData;
}
@Override
public EventHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.event_item, parent, false);
return new EventHolder(view);
}
@Override
public void onBindViewHolder(EventHolder holder, int position) {
EventItem item = listData.get(position);
holder.title.setText(item.getTitle());
holder.icon.setImageResource(item.getImageResId());
}
@Override
public int getItemCount() {
return listData.size();
}
}
答案 0 :(得分:0)
更改:
public EventHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_item, parent, false);
return new EventHolder(view);
}
致:
public EventAdapter.EventHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.event_item, parent, false);
return new EventHolder(view);
}
我不确定为什么,希望有人可以在评论中解释:)