1

Conc.: I am trying to View a RecyclerView inside PagerView

Problem: The RecyclerView is not being displayed

Also, I have ready RecyclerView in PagerView Question knowing that I have to have another fragment for the Recycler and I have it

NB :

  • PagerView is working well as it display a single TextView
  • RecyclerView was being displayed normally before the PageView

The Changes I made which after the changes the problem appears:

  • Moving the RecyclerView to a new Fragment Class

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

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

<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

new ExampleRecyclerViewFragment().fetchExamples(dataSnapshot.child("Examples"));

Thanks a lot

Community
  • 1
  • 1
Dasser Basyouni
  • 3,142
  • 5
  • 26
  • 50

1 Answers1

1

The problem is with the fact that you are calling the fetchExamples without the knowledge of whether your fragment has been inflated or not. You could use eventBus to send dataSnapshot object to your fragment and call the fetchExamples there. 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();
Ashik Vetrivelu
  • 1,021
  • 1
  • 9
  • 24
  • Thank you a lot for your answer and explanation, I think it will work but there is a small things "cannot resolve it " (sticky, ThreadMode, getDefault), I have no idea about eventBus so would you help me with that – Dasser Basyouni Feb 06 '17 at 11:49
  • 1
    Sure. EventBus is just a library for loose coupling fragment and activity. You would generally write an interface to send data from activity or main fragment to the consumer fragment. EventBus replaces that without the need to declaring the class. Sticky means that the consumer fragment need not be present while the data is being posted.ThreadMode just tells that the update takes place in Main/UI thread. – Ashik Vetrivelu Feb 06 '17 at 12:03
  • 1
    http://greenrobot.org/eventbus/ refer this for more info regarding events – Ashik Vetrivelu Feb 06 '17 at 12:03
  • Thank you a lot I will learn about it, but I mean the (sticky, ThreadMode, getDefault) cannot be resolved in running the code now – Dasser Basyouni Feb 06 '17 at 12:55
  • Oh!!!. You need to add EventBus dependency to your project. Add this line to your grade dependencies and sync the project `compile 'org.greenrobot:eventbus:3.0.0'` – Ashik Vetrivelu Feb 06 '17 at 13:03
  • Thank you again, that was from the wrong android studio auto add library android.support.test.espresso.core.deps.guava.eventbus – Dasser Basyouni Feb 06 '17 at 16:02
  • 1
    Guava has an eventbus, but I prefer green robot one. – Ashik Vetrivelu Feb 07 '17 at 08:25