I've been using activity and firebase-ui in order to populate RecyclerView with data from Realtime Database. It works well in activity like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_transactions);
list = (RecyclerView) findViewById(R.id.list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
list.setLayoutManager(layoutManager);
list.setAdapter(new TransactionsAdapter(
DataManager.getTransactionsRef().orderByChild("date")
));
}
but then I moved this code to fragment with the same layout:
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
list.setLayoutManager(layoutManager);
list.setAdapter(new TransactionsAdapter(
DataManager.getTransactionsRef().orderByChild("date")
));
}
and it does not show any data. Currently I have both Activity and Fragment and it works in activity but it doesn't in Fragment.
Here is my dependencies:
compile 'com.google.firebase:firebase-core:10.2.1'
compile 'com.google.firebase:firebase-database:10.2.1'
compile 'com.google.firebase:firebase-auth:10.2.1'
compile 'com.firebaseui:firebase-ui-auth:1.2.0'
compile 'com.firebaseui:firebase-ui-database:1.2.0'
compile 'com.android.support:design:25.3.1'
@semenoh This question would be better for Stack Overflow, but I'll give you some clues here anyway.
Ok, so you know how in your AppCompatActivity you use setContentView(R.layout.something)? Well, in Fragments you have to deal with that stuff yourself. I would recommend Ctrl + Bing the super.onViewCreated(view, savedInstanceState); to see what it does. If you do that, you'll see that it returns null and does nothing:
@Nullable
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return null;
}
TLDR: you have to manually inflate your layout like so:
View rootView = inflater.inflate(R.layout.something, container, false);
// ...
rootView.findViewById(R.id.recycler_view).setAdapter(...)
return rootView;
Hope this helps! ๐
@SUPERCILEX thanks for the pointers!
@semenoh as mentioned, when the question is specific to your app (and not a bug in the library, which I am pretty sure this is not) it's better asked on StackOverflow.
Sorry I missed that part when creating an issue. Actually my code looks like this:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_transactions, container, false);
list = (RecyclerView) v.findViewById(R.id.list);
return v;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
list.setLayoutManager(layoutManager);
list.setAdapter(new TransactionsAdapter(
DataManager.getTransactionsRef().orderByChild("date")
));
}
So actually I'm inflating view and issue is not related to my app but to the library.
@semenoh if you believe it's a problem with the library please supply a MCVE.
Also I think using onViewCreated is not the correct choice here. That method is called immediately after onCreateView and there's no guarantee that getActivity() will be non-null.
You want to use Fragment#onActivityCreated
@semenoh Yeah, like @samtstern said, putting stuff in onViewCreated a little weird. I would just put everything in onCreateView and use the getContext() method for your layout manager.
Here are just a few pointers that might help you figure it out:
Hope this helps a little! ๐
@SUPERCILEX Thanks a lot, it really helped me a lot! And really sorry for rising this issue. It was my mistake in layout. 0dp height in ConstraintLayout was a root of problem.
@semenoh Glad you figured it out!!! ๐ Yeah, I still make mistakes with 0dp children in ConstraintLayout (and I converted my codebase way back in alpha2!). Basically, your child has to be constrained on both sides (top/bottom or left/right) _and_ the parent's size has to be stable.
OMG you guys dint know how much i love you at this moment , the fuking 0dp.
่ ๆไฝ
I am using Relative Layout Still the same error is there anyone can help?
In my case, I am using LinearLayout as a container for the fragment to display RecyclerView. If I set the height of RecyclerView or LinearLayout to 100dp its displays data. But when it is match_parent or wrap_content then it is not displaying anything. Please help me to solve this.
Most helpful comment
@semenoh This question would be better for Stack Overflow, but I'll give you some clues here anyway.
Ok, so you know how in your
AppCompatActivityyou usesetContentView(R.layout.something)? Well, in Fragments you have to deal with that stuff yourself. I would recommendCtrl + Bing thesuper.onViewCreated(view, savedInstanceState);to see what it does. If you do that, you'll see that it returns null and does nothing:TLDR: you have to manually inflate your layout like so:
Hope this helps! ๐