0

First: I am fairly new to android. This project is probably above my pay grade so if I ask a wrong question, at least you understand.

I want to use a TabLayout with ViewPager2 with the views that each page will contain be made of complex fragments. Each page will have the same fragment definition. As far as I can tell at this point, ViewPager2 depends on RecyclerView. I have attempted to use a LinearLayout for each page and I kept getting a fault that referenced the RecyclerView.Adapter.

I create the recyclerView from within another fragment as:

recyclerView = (RecyclerView) mView.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
// use a linear layout manager
layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
List<String> input = new ArrayList<>();
for (int i = 0; i < 5; i++) {
    input.add("Test" + i);
}// define an adapter
mAdapter = new SalinasAdapter(input, this);
recyclerView.setAdapter(mAdapter);

and the SalinasAdapter's onCreateViewHolder

public SalinasAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // create a new view
    View v = null;
    FragmentManager fm = mFragment.getChildFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    // Add the fragment but do not put it in a container
    ft.add(new RowFragment(), Integer.toString(mIndex));
    ft.commit();
    ft.commitNow();

    // Find the fragment from the fragment manager
    Fragment foo = fm.findFragmentByTag(Integer.toString(mIndex));
    mIndex++;

    // Keep track of the index in an ArrayList<String> so they can be removed from the 
    // FragmentManager when needed.
    // ToDo

    // Make sure it is mine
    if (foo instanceof RowFragment){
        v = foo.getView();
    }

    // Add the view to a ViewHolder
    ViewHolder vh = new ViewHolder(v);

    return vh;
}

From this I assume that real views are passed to the ViewHolder and it can find all of the elements it needs to set the data correctly.

When done the application shows the five rows for a brief few seconds before it crashes.

I guess I have two questions: Is this the right way to look at this problem? Is there a way to debug, to catch the fault stack trace?

halfer
  • 19,824
  • 17
  • 99
  • 186
Nefarious
  • 458
  • 6
  • 21
  • Is there any reason for you to need to use Fragments here? Fragments have their own life cycle callbacks and the RecyclerView's job is to recycle views, that's definitely going to lead to trouble later on. – Jacob Collins Mar 10 '20 at 20:47
  • 2
    Trying to manage Fragment lifecycles from within a RecyclerView adapter is complex and somewhat painful. ViewPager2 does a lot to make it work correctly. For stack traces, see [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) for what to do once you have it. – Ryan M Mar 10 '20 at 20:50
  • @Jacob Collins: I need fragments in my tablayout pages. I have tried to use something other then RecyclerView but it always crashes. Is there somewhere it talks about using ViewPager2 without RecyclerView? – Nefarious Mar 10 '20 at 20:51
  • 1
    "using ViewPager2 without RecyclerView" - this isn't possible, ViewPager2 is entirely based on RecyclerView. This would be like trying to use `LinearLayout` without `ViewGroup`. – Ryan M Mar 10 '20 at 21:03

1 Answers1

0

(Posted the solution on behalf of the question author to move it to the answer space).

I fixed the problem by adding: fragmentManager.executePendingTransactions() instead of commitNow().

halfer
  • 19,824
  • 17
  • 99
  • 186