¿Debo inicializar mi recyclerview en onCreateView, onViewCreated o onActivityCreated?
¿Cuál es la diferencia entre los 3, buscaba explicaciones, pero algunas personas dicen utilizar onCreateView y algunos dicen utilizar onViewCreated o onActivityCreated Y sólo utilizar onCreateView para inflar el diseño?
Este es mi código
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_tab1, container, false);
recyclerViewSongs = rootView.findViewById(R.id.recyclerViewSongs);
initRecyclerView();
Log.e(TAG, "onCreateView called!");
return rootView;
}
private void initRecyclerView() {
Main.musicList = Main.songs.songs;
// Connects the song list to an adapter
// (Creates several Layouts from the song list)
allSongsAdapter = new AllSongsAdapter(getContext(), Main.musicList);
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
recyclerViewSongs.setLayoutManager(linearLayoutManager);
recyclerViewSongs.setHasFixedSize(true);
recyclerViewSongs.setAdapter(allSongsAdapter);
recyclerViewSongs.addOnItemTouchListener(new OnItemClickListeners(getContext(), new OnItemClickListeners.OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.O)
@Override
public void onItemClick(View view, int position) {
Toast.makeText(getContext(), "You Clicked position: " + position, Toast.LENGTH_SHORT).show();
if (! Main.songs.isInitialized())
return;
//Start playing the selected song.
playAudio(position);
}
}));
}
onCreateView()
será la mejor elección, ya que está utilizando Fragment
. La diferencia es onCreateView()
es el Fragment
equivalente de onCreate()
las actividades y carreras durante la View
creación, pero onViewCreated()
se ejecuta después de que la View
ha creado.
Y onActivityCreated()
las llamadas después de onCreate()
método de las Activity
ultima como se puede ver aquí: https://stackoverflow.com/a/44582434/4409113