android – “RecyclerView:没有附加适配器;跳过布局“for fragmentlerview in fragment
作者:互联网
参见英文答案 > recyclerview No adapter attached; skipping layout 29个
我收到了这个错误,但我不知道是什么导致它…也许与这个事实有关,这个事实是在片段中而不是在活动本身中初始化.编辑:包含StatsAdapter代码
public class StatsFragment extends Fragment {
private RecyclerView mRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
private RecyclerView.Adapter mAdapter;
private String[] myDataset = new String[]{"hello", "world", "yolo"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_stats, parent, false);
mRecyclerView = (RecyclerView) v.findViewById(R.id.cardList);
mRecyclerView.setHasFixedSize(true);
mAdapter = new StatsAdapter(myDataset);
mRecyclerView.setAdapter(mAdapter);
mLinearLayoutManager = new LinearLayoutManager(getActivity());
mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLinearLayoutManager);
return v;
}
}
public class StatsAdapter extends RecyclerView.Adapter<StatsAdapter.ViewHolder> {
private String[] mDataset;
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ViewHolder(View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.title);
}
}
public StatsAdapter(String[] myDataset) {
mDataset = myDataset;
}
@Override
public StatsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_stats, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
@Override
public int getItemCount() {
return mDataset.length;
}
}
解决方法:
在setAdapter之前移动setLayoutManager行
标签:android-adapter,android,android-fragments,android-recyclerview,android-cardview 来源: https://codeday.me/bug/20191006/1861823.html