Fragmento Recyclerview onCreateView, onViewCreated o onActivityCreated?

Vince VD:

¿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);
            }
        }));

}
ʍѳђ ઽ 9 Norte:

onCreateView()será la mejor elección, ya que está utilizando Fragment. La diferencia es onCreateView()es el Fragmentequivalente de onCreate()las actividades y carreras durante la Viewcreación, pero onViewCreated()se ejecuta después de que la Viewha creado.

Y onActivityCreated()las llamadas después de onCreate()método de las Activityultima como se puede ver aquí: https://stackoverflow.com/a/44582434/4409113

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=221084&siteId=1
Recomendado
Clasificación