I'm trying to use SmartTabLayout from here https://github.com/ogaclejapan/SmartTabLayout, and i have a drawer. When i'm clicking at item on drawer, i replace my fragment, to fragment with smarttablayout. For first time it's ok, smarttablayout have tabs with content, but when i click in drawer on the same item, content of tabs just disappear, and to show them back i have to scroll to the end, and scroll back, to show them. I don't have idea why.
Notes: If using fragment inside a ViewPager, Must be use Fragment#getChildFragmentManager().
https://github.com/ogaclejapan/SmartTabLayout#pageradapter-for-fragment-based-page
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getChildFragmentManager(), FragmentPagerItems.with(this.getA())
.add(R.string.item_pager_title_products, ProductListFragment.class)
.add(R.string.item_pager_title_shops, ShopListFragment.class)
.create());
FYI: Had same problem because I didn't read the manual... Original answer found here https://stackoverflow.com/a/25525714/1257369
Related
I am trying to implement a side-navigation bar (hamburger menu). I want to be able to select a new fragment to view from this menu, and then click buttons on that fragment to direct to another fragment (not in the hamburger menu list).
I was following a guide (https://www.youtube.com/watch?v=bjYstsO1PgI), but it doesn't show the second step of redirecting to the other fragment.
I keep getting a crash whenever I click a button that redirects to the other fragment, if it has been loaded by the hamburger menu.
The line I use is to render from the hamburger menu is:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FirstFragment()).commit();
This is called by the following event listener:
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
and I get this error whenever I press a button to go to the 'other' fragment.
Navigation action/destination com.example.testApp:id/action_FirstFragment_to_submitScreen cannot be found from the current destination Destination(com.example.testApp:id/submitScreen) label=fragment_submit_screen class=com.example.renamedTestApp.SubmitScreen
This error is not present when I use
<include layout="#layout/content_main"/>
to load the first fragment on app startup (the one from the hamburger menu).
Any tips? This question may be confusing so let me know if I should tidy up my explanation.
I believe the error may be in the fragment not being fully loaded perhaps? If that is the case, what would be a better line to use to switch on the fly.
provide some code
try this
FragmentManager fragmnt= getActivity().getSupportFragmentManager();
FragmentTransaction tr= fragmnt.beginTransaction();
tr.replace(R.id.YOUR,new Yourfragment());
//or tr.replace(R.id.content_frame, new Yourfragment());
tr.commit();
Current layout
The above is the app layout in Android app that I am working on. There is an activity, which has a BottomNavigationView. The navigation is managed using:
this.getSupportFragmentManager().beginTransaction().replace(R.id.content, fragment).commit();
Now, on this fragment, I have a TabLayout and a ViewPager as you can see in the image. The ViewPager listens to the TabLayout selector and it can be changed by either sliding the ViewPager or clicking on the tab. Works fine..
Once a tab is selected, I am loading another Fragment that has a RecyclerView on it. The contents in the RecyclerView is coming from a URL so its being updated on Post() as below:
CompletableFuture.runAsync(this::prepare);
recyclerView.post(() ->
{
adapter.list = newList;
adapter.notifyDataSetChanged();
progress.setVisibility(View.INVISIBLE);
});
Now the issue that I am facing:
Everything works fine for the first time when the Menu 1 in the layout image is clicked. The recycler view is able to call the service and load the content. Now if I switch the navigation to Menu 2 or any other and try to come back to Menu 1, nothing progresses from the ViewPager / TabLayout. Up to the TabLayout I can see Logs, and after that nothing. Basically setting the currentItem to the TabLayout is called but its not calling the create of the next Fragment and hence its not loading the RecyclerView again. The ViewPager is using FragmentStatePagerAdapter.
TabViewerAdapter extends FragmentStatePagerAdapter
The adapter is initialised using ChildFragmentAdapter as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 0, tabs.getTabCount());
I know behaviour "0" is deprecated so tried with "1" as well as below:
TabViewerAdapter adapter = new TabViewerAdapter(getChildFragmentManager(), 1, tabs.getTabCount());
Tried invalidating views, notifying adapter and many other.. No use.
I also noticed that when I switch app (while its running by tapping the phone square button to go to another app) and come back (resume) then its loading the recycler view again.
This means, I am missing something and tried the onResume method. But didn't work.
Am I doing something stupid or against design patterns or conventions? No idea what to do..
Please help, thank you.
I resolved this issue by doing the things below:
Used a SharedViewModel to handle the list
Used ListAdapter for RecyclerView
All good now.
This is what I'm hoping to achieve:
Step 1: Click on the Green Button to open View1.xml on the ViewPager. (Works)
Step 2: Click on the Orange Button to open SubView.xml on the ViewPager. (Doesn't work)
So, I have set the onClick event for the orange button the following:
public void openForm(View view) {
ViewGroup item = (ViewGroup)findViewById(R.id.vp_horizontal_ntb); //this is the view pager
View child = getLayoutInflater().inflate(R.layout.activity_post_form, null);
item.addView(child);
}
Results:
Nothing. Nothing happens.
If I switch viewpager for linearlayout it opens (and breaks tab navigation). What I wanna do is open it on the viewpager, so it looks and behaves as if it's on a tab, until it's removed by clicking on another tab.
I also thought about putting a linear layout there and make viewPager gone while the linearLayout is set to visible. But I feel like there's a better solution than this.
Hello everyone i have a view pager named "viewpager" and two fragments named "Add_Create" and "Add_Create_2".I am using Tab layout to show different tabs.Now in one of the tabs there is "Add_Create" fragment.In Add_Create fragment there is a button and on that button click i want to show Add_Create_2 fragment.Below is my code that i have tried
Add_Create_2 add= new Add_Create_2();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.viewpager,add);
transaction.addToBackStack(null);
transaction.commit();
Now this code is present in first fragment named "Add_Create".
The problem could be at the viewPager. What kind of element is this? If it is a ViewPager, you cannot load it a Fragment. You should to create on it maybe a RelativeLayout, and it will be your fragment container.
Instead of using getFragmentManager(), use getSupportFragmentManager(). Since you're calling it from a fragment, maybe getActivity().getSupportFragmentManager()
I am new to Android Espresso library. Trying to test click on RecyclerView item inside the ViewPager. I've searched the Internet but found nothing about ViewPager. So my question is: how to access RecyclerView inside a ViewPager using Espresso to perform click?
Let's suppose you want to tap on buttonClick, which is the id of a button inside your RecyclerView.
I use this code:
onView(allOf(withId(R.id.buttonToClick), isCompletelyDisplayed()))
.perform(click());
This gets the view with this id which is currently displayed and performs a click on it.
Hope this helps.
P.S.: Also, on espresso-contrib there are RecyclerViewActions that maybe you can use: actionOnHolderItem, actionOnItem and actionOnItemAtPosition. You can do something like:
onView(withId(R.id.recyclerView))
.perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));