Skip to content Skip to sidebar Skip to footer

Recyclerview Not Displaying In Fragment

New to Android, I used the code from https://github.com/tutsplus/Android-CardViewRecyclerView which is working perfectly. But when I created Fragment out of it, it is displaying no

Solution 1:

You have not Override the Methods which are necessary for fragment. You should find some tutorials on Fragment first,rather using protected method onCreateView you should override its public method.

public class RecyclerViewFrag extends Fragment {

   private List<Person> persons;
   private RecyclerView rv;

     @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.recyclerview_activity, container, false);
    }
     @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        rv=(RecyclerView)view.findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);

        initializeData();
        initializeAdapter();
    }

    public interface OnFragmentInteractionListener {
      // TODO: Update argument type and name
     public void onFragmentInteraction(Uri uri);
    }

    private void initializeData(){
       persons = new ArrayList<>();
       persons.add(new Person("Emma Wilson", "23 years old", R.drawable.emma));
       persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.lavery));
       persons.add(new Person("Lillie Watts", "35 years old", R.drawable.lillie));
    }

    private void initializeAdapter(){
      RVAdapter adapter = new RVAdapter(persons);
      rv.setAdapter(adapter);
}
}

Post a Comment for "Recyclerview Not Displaying In Fragment"