I'm using a ViewPager
in my Activity which has multiple Fragments. in the onCreate
method of my Activity, I want to add multiple views to one of those fragments so that they can be seen at start. the problem is that i can not find a way to access that fragment. how to do that? I have tried to access the RecyclerView
of that fragment at this way:
public class CaafeFragment extends Fragment {
private RecyclerView recyclerView = null;
public CaafeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_caafe, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.caafeRecyclerView);
return view;
}
public RecyclerView getRecyclerView() {
return (RecyclerView) getView().findViewById(R.id.caafeRecyclerView);
}
}
but it returns null when i call getRecyclerView()
from the onCreate()
of my activity
// activity
public void onCreate()
{
.
.
.
CaafeFragment caafeFragment=new CaafeFragment();
caafeRecyclerView = caafeFragment.getRecyclerView(); // here it returns null
}