Using ViewPager2 in an Existing Fragment - android

I have Created a Fragment for my bottom Tab bar, The tab bar has four fragments, In one of my fragments, I am in need of Slider to view testimonials of people one by one.
In this case, I have used the ViewPager2 functionality to work on the slider, I have all the steps to create Viewpager2 from this video
https://youtu.be/KwihiADN-0k
After following all the steps I have getting an error stating
java.lang.IllegalStateException: viewpager1 must not be null (viewpager1 is my id of Viewpager2 function)

You should define your viewpager1.adapter = viewpager1Adapter not in onCreateView(), but in onViewCreated() and your issue will be solved.

Related

ViewPager2 - fragment layout disappears after multiple swipes via tabs

I'm using ViewPager2 which is hosted inside a fragment. On the initial opening of ViewPager2 fragment host everything is loaded and displayed correctly, but when I swipe multiple times (via TabLayout) to the last fragment and go back to the first fragment, its layout is empty/white screen.
The adapter which I'm using is FragmentStateAdapter. Also, I set offscreenPageLimit to a constant value (which is in most cases less than the size of the fragments list in ViewPager2).
This is constructor of my custom FragmentStateAdapter:
pagerAdapter = MyCustomPagerAdapter(requireContext(), childFragmentManager, viewLifecycleOwner.lifecycle)
Since ViewPager2 is using RecyclerView internally, maybe there is some problem with recycling fragments when there are not visible (also considering offscreenPageLimit value).
Problem was in by viewBinding() method for getting binding object.
After using standard way of creating binding object everything is working OK.

Can i pass an Instance of a FragmentActivity into a Fragment transaction

I’m trying to do a tab layout, but before I’ve a navigation drawer that contains some menu. In one of those items I want to display a Tab activity or let’s say view. My problem is in the replace method we should pass a Fragment and in a tabbed view you can't do it with a Fragment you should have an activity or a FragmentActiviy. Now I’m trying to found a solution to this.
There is nothing that actually forbids using a ViewPager inside a Fragment, but I'd try to avoid going down that road. I fear for the UX, having both a navigation drawer and tab navigation is very confusing.
If you must do it nonetheless, you can find an example of using ViewPager on the official documentation page here, it is using Activity but the only main difference I see when implementing that in a Fragment is that the FragmentManager you use should be the one obtained calling getChildFragmentManager inside the main Fragment.

Mixing layout values each other when use same layout for different fragments in ViewPager

In my Android application there are two fragments called FragmentA and FragmentB. I added both fragments to a ViewPager using a FragmentAdapter So when I run the application I'm getting a unexpected layout (both fragment layouts are mixed together). Whenever I tried changing the currentItem of ViewPager by swiping for each time the layout is mixing each other.
My question: Is there any problem if we use same layout to different fragments in a ViewPager?
I found the answer.
There is no problem if you use getView() function of Fragment to refer the current root view of a fragment. So that you can get exact views in the Fragment. You can use like this(getView().findViewByid(...);
But there is a problem if you use activity reference to refer views in the fragment. Because all views in each fragment which is using same layout xml has same id. So if you change the value of a view in a fragment that will reflect in other fragments which are using the same layout in ViewPager. One more thing, think like this, when the activity created, all the fragments in the ViewPager of that activity will also be created and runs in background. So if you change the value of a view, android will return 1st view which is having the same id. Android knows only id of views. So always refer views of fragment using it root view (getView()).

Android changing setNavigationMode per fragment is crashing the app

I created the below project so you can see my exact code and what is going on:
https://github.com/CorradoDev/TabsTest/commit/8f054dab2371b791c4061ceb511413f720f65d67
Basically what I am trying to do is hide the tabs for some pages and show them in other pages.
Below is the code I am using to show the tabs in the onresume
if(getActivity().getActionBar().getNavigationMode()==ActionBar.NAVIGATION_MODE_STANDARD){
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
THen to hide the tabs I am doing the below on resume:
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
When I am on the first fragment(nothing in backstack). I can show and hide the tabs on hte second. It gives errors sometimes with changing tabs.
When I am on the second fragment in the backstack and I hide the third fragment. I see the second and third fragment both call the onrefresh but the third fragment does not show.
I am confused on what is going on and why this is not easier.
Below is the error I generally get
03-27 15:26:31.029: E/AndroidRuntime(5505): java.lang.IllegalStateException: Fragment already added: Fragment3{41f2e390 #2 id=0x1020002 fragment3}
I still would like to know why the above does not work. But my fix was to create another activity with the fragment and no tabs. That seems to work well. But I am interested if they did not intend you to change tabs and no tabs per fragment.
I had a similar situation - only that I used NAVIGATION_MODE_LIST instead of tabs. I run into similar issues when I called a fragment from another fragment e.g. click on a list item opening up the item details.
Now I call all fragments from the main activity which allows me to control the set up of the actionbar. Whenever the navigation list should disappear I just call NAVIGATION_MODE_STANDARD when the fragment is called and NAVIGATION_MODE_LIST for the other fragments.

A bug in ViewPager using it with other fragment

I need your help.
I have one Activity with two fragments: one fragment with simple TextView in LinearLayout and other fragment: ViewPager with 3 fragments in FragmentPagerAdapter.
I make transaction with replace action, but I have error from ViewPager: java.lang.IllegalStateException: Recursive entry to executePendingTransactions.
That's a source code that show this problem, maybe anybody knows how to fix it.
Um no. A ViewPager extends ViewGroup. How does that make it a Fragment?
Yes, you're right that nesting Fragments isn't supported. However, it works if you have a Fragment holding a ViewPager whose Adapter contains several Fragments. Trust me, it works. I'm using it in the current project I'm working on right now and I've even got nested ViewPagers without the horizontal touch events fighting for control, so essentially I have Fragments within a Fragment.
With revision 11 of the Android Support Library, you can now nest fragments within fragments to avoid the recursive exception. See this question How to add a Fragment inside a ViewPager using Nested Fragment (Android 4.2)
A ViewPager is basically a Fragment, and you can't put Fragments in Fragments. Period.

Categories

Resources