RecyclerView﹕No adapter attached; skipping layout
But when I click on the tab that opens the fragment, after the app is already open, everything works normally. I believe the problem was to have passed on to the onActivityCreated, but if you put it in OncreateView, the app does not even open.
fragment:
package br.com.igoroliv.youtubecanal.fragments;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
import br.com.igoroliv.youtubecanal.*;
import br.com.igoroliv.youtubecanal.API.Modelo.PlaylistItem;
import br.com.igoroliv.youtubecanal.Recycle.Adapter;
/**
* Created by igord on 23/06/2017.
*/
public class fragmentlistavideos extends Fragment {
private static final String TAG = "Log";
private TextView txtteste;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private List<PlaylistItem> mylistadevideo;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmentlistavideos, container, false);
return v;
}
@Override
public void onActivityCreated (@Nullable Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
try {
mRecyclerView = (RecyclerView) getActivity().findViewById(R.id.mRecicleview);
Log.d(TAG, "pegou o recycle do layout");
} catch (Exception e) {
Log.d(TAG, "Erro ao pegar o recycle do layout " + e);
}
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
mRecyclerView.setHasFixedSize(true);
Log.d(TAG, "definiu setHasFixedSize como true");
// defininado o LinearLayoutManager
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(llm);
Log.d(TAG, "definiu LinearLayoutManager");
mylistadevideo = ((MainActivity) getActivity()).getlistavideos();
Log.d(TAG, "Salvou a lista no mylistadevideo");
if (mylistadevideo == null) {
Log.d(TAG, "Lista nula");
} else {
mAdapter = new Adapter(mylistadevideo);
Log.d(TAG, "Definiu o Adapter");
}
// specify an adapter (see also next example)
mRecyclerView.setAdapter(mAdapter);
Log.d(TAG, "Setou o Adapter");
}
}
Main Activity with click on the tabs:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//click nas abas
BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {
if (tabId == R.id.tab_videos) {
//infla o fragment dentro do id ( contentContainer ) do framelayout
getSupportFragmentManager().beginTransaction()
.add(R.id.contentContainer, new fragmentlistavideos())
.replace(R.id.contentContainer,new fragmentlistavideos())
.commit();
}else if (tabId == R.id.tab_chat){
//infla o fragment dentro do id ( contentContainer ) do framelayout
getSupportFragmentManager().beginTransaction()
.add(R.id.contentContainer, new fragmentchat())
.replace(R.id.contentContainer,new fragmentchat())
.commit();
}
}
});