public class WorkFragment extends Fragment {
List<CardViewItem> items = new ArrayList<>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("FRAGMENT", "Work Fragment started");
TypedArray icons = getResources().obtainTypedArray(R.array.project_icons);
TypedArray names = getResources().obtainTypedArray(R.array.project_names);
TypedArray descs = getResources().obtainTypedArray(R.array.project_descs);
for (int i=0;i<icons.length();i++){
items.add(new CardViewItem(icons.getDrawable(i),
names.getString(i),
descs.getString(i)));
}
icons.recycle();
names.recycle();
descs.recycle();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("FRAGMENT", "Work Fragment onCreateView");
View rootView = inflater.inflate(R.layout.fragment_work, container, false);
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
LinearLayoutManager llm = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(new ItemAdapter(items));
return inflater.inflate(R.layout.fragment_work, container, false);
}
}
给我
E/RecyclerView: No adapter attached; skipping layout
我已经尝试了我找到的所有解决方案(设置一个空的适配器,使用单独的线程移动代码elswhere)但无济于事。这应该适用于正常活动,所以我想我可能做错了。
答案 0 :(得分:1)
问题主要是这一行
return inflater.inflate(R.layout.fragment_work, container, false);
你应该
return rootView
在第一次返回时,您正在从fragment_work.xml
开始,从正确设置RecyclerView
的视图层次结构中膨胀一个完全不同的视图层次结构。