Good evening, I can not understand why the fragment does not display recyclerview. if you need code

public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.CategoriesViewHolder> { private ArrayList<Category> categoryList; private onCategoriesPositionClick onCategoriesPositionClick; public CategoriesAdapter (ArrayList<Category>categoriesList, onCategoriesPositionClick onCategoriesPositionClick){ this.categoryList = categoriesList; this.onCategoriesPositionClick = onCategoriesPositionClick; } @Override public CategoriesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_categories, null); return new CategoriesViewHolder (view); } @Override public void onBindViewHolder(CategoriesViewHolder holder, int position) { holder.category.setText(categoryList.get(position).getCategory()); holder.image_ic.setImageResource(categoryList.get(position).getImage_ic()); } @Override public int getItemCount() { return categoryList.size(); } class CategoriesViewHolder extends RecyclerView.ViewHolder { TextView category; ImageView image_ic; LinearLayout linearlayout; public CategoriesViewHolder(View itemView) { super(itemView); category = (TextView)itemView.findViewById(R.id.Category); image_ic = (ImageView)itemView.findViewById(R.id.Image_ic); linearlayout = (LinearLayout) itemView.findViewById(R.id.Category_linearLayout); linearlayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { onCategoriesPositionClick.OnCategoriesClick(getAdapterPosition()); } }); } } } public class FragmentCategories extends Fragment { // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, eg ARG_ITEM_NUMBER private static final String ARG_Category = "param1"; // TODO: Rename and change types of parameters private String mParam1; private String mParam2; RecyclerView.LayoutManager mLayoutManager; private RecyclerView recyclerView; private CategoriesAdapter categoriesAdapter; ArrayList<Category>categoryList = new ArrayList<>(); private onCategoryPositionSelectedListener mListener; public FragmentCategories() { // Required empty public constructor } /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @return A new instance of fragment FragmentCategories. */ // TODO: Rename and change types and number of parameters public static FragmentCategories newInstance(ArrayList<Category> categoryList) { FragmentCategories fragmentCategories = new FragmentCategories(); Bundle args = new Bundle(); args.putSerializable(ARG_Category,categoryList); fragmentCategories.setArguments(args); return fragmentCategories; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); if (getArguments() != null) categoryList = (ArrayList<Category>) getArguments().getSerializable(ARG_Category); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_categories, container, false); recyclerView = view.findViewById(R.id.rv_categories); recyclerView.setHasFixedSize(true); categoriesAdapter = new CategoriesAdapter(categoryList, new onCategoriesPositionClick() { @Override public void OnCategoriesClick(int position) { mListener.onCategoryPositionClick(position); } }); LinearLayoutManager llm = new LinearLayoutManager(getActivity()); recyclerView.setLayoutManager(llm); recyclerView.setAdapter(categoriesAdapter); return view; } @Override public void onAttach(Context context) { super.onAttach(context); if (context instanceof onCategoryPositionSelectedListener) { mListener = (onCategoryPositionSelectedListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public interface onCategoryPositionSelectedListener { // TODO: Update argument type and name void onCategoryPositionClick(int position); } } 

1 answer 1

You incorrectly inflate the markup in onCreateViewHolder

 View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_categories, parent, false); 

Why is that?

Now you pass in as a container null . Therefore, your View not attached to it. The onCreateViewHolder comes to onCreateViewHolder , to it and you need to connect your View

  • thanks, I definitely didn’t notice, but it still doesn’t work ( - Lena
  • @ lena, try removing setHasFixedSize - Flippy
  • removed, did not help ( - Lena
  • @ lena, updated the answer - Flippy
  • @ lena, hmm .. Try in newInstance just save the data to the fragmentCategories.categoryList = categoryList variable fragmentCategories.categoryList = categoryList - Flippy