[英]RecyclerView in PageView not showing list
Conc.: I am trying to View a RecyclerView inside PagerView浓度:我正在尝试在 PagerView 中查看 RecyclerView
Problem: The RecyclerView is not being displayed问题:未显示 RecyclerView
Also, I have ready RecyclerView in PagerView Question knowing that I have to have another fragment for the Recycler and I have it另外,我在 PagerView Question 中准备了RecyclerView知道我必须为Recycler准备另一个片段并且我拥有它
NB :注意:
The Changes I made which after the changes the problem appears:我所做的更改在更改后出现问题:
ExampleRecyclerViewFragment ExampleRecyclerViewFragment
public class ExampleRecyclerViewFragment extends Fragment {
public statlic ExampleAdapter adapter;
List<MainExampleObject> exampleList = new ArrayList<>();
public Example example = new Example();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_two, container, false);
RecyclerView exampleRecyclerView = (RecyclerView) rootView.findViewById(R.id.ExampleRecyclerView);
exampleList = new ArrayList<>();
adapter = new ExampleAdapter(exampleList);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setSmoothScrollbarEnabled(true);
exampleRecyclerView.setScrollbarFadingEnabled(true);
exampleRecyclerView.setLayoutManager(llm);
exampleRecyclerView.setAdapter(adapter);
return rootView;
}
public void fetchExamples(final DataSnapshot Examples) {
//noinspection StatementWithEmptyBody
if (Examples.hasChild("Method 2")) {
} else {
Examples.getRef().child("Method 1").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
example.setStepName(String.valueOf(dataSnapshot.getKey()));
for (DataSnapshot childSnapshot : dataSnapshot.child("Code").getChildren()) {
example.addCode(String.valueOf(childSnapshot.getValue()));
}
for (DataSnapshot childSnapshot : dataSnapshot.child("Explaination").getChildren()) {
example.addExplanation(String.valueOf(childSnapshot.getValue()));
}
example.addExample();
exampleList.add(example.getExampleObject());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
}
public ExampleRecyclerViewFragment() {
// Required empty public constructor
}
}
ViewPagerAdapter ViewPagerAdapter
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
@Override
public Fragment getItem(int position) {
return new ExampleRecyclerViewFragment();
}
@Override
public int getCount() {
return mFragmentTitleList.size();
}
void addFragment(String title) {
//mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
fragment_two.xml fragment_two.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ExampleRecyclerViewFragment"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/ExampleRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView"
/>
</LinearLayout>
I think that code will be the needed if anyone needs to see other class just let me know with a comment我认为如果有人需要查看其他课程,则需要该代码,请通过评论告诉我
Sorry for forgetting mentioning that fetchExamples()
is get called in the main class in a method called in onCreate抱歉忘记提到
fetchExamples()
是在主类的 onCreate 中调用的方法中调用的
new ExampleRecyclerViewFragment().fetchExamples(dataSnapshot.child("Examples"));
Thanks a lot非常感谢
The problem is with the fact that you are calling the fetchExamples without the knowledge of whether your fragment has been inflated or not.问题在于您在不知道您的片段是否已膨胀的情况下调用 fetchExamples。 You could use eventBus to send dataSnapshot object to your fragment and call the fetchExamples there.
您可以使用 eventBus 将 dataSnapshot 对象发送到您的片段并在那里调用 fetchExamples。 The code in your main class would be,
主类中的代码是,
EventBus.getDefault().postSticky(dataSnapshot.child("Examples"));
and in your Fragment do,在你的片段中做,
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(DataSnapshot examples) {
fetchExamples(examples);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.