This question already has answers here:
How to use TabLayout with ViewPager2 in Android
(12 answers)
Closed 2 years ago.
I know view pager and tab layout, but i want to display a tab on my screen, and each tab make up of a fragment. How can i do? I have try few approach but none of them work for me.
You actually want to use TabLayout and ViewPager2.
private TabLayout tabLayout;
private AppBarLayout appBarLayout;
private ViewPager viewPager;
tabLayout=(TabLayout)findViewById(R.id.tablayout);
viewPager=(ViewPager)findViewById(R.id.viewpager);
//create viewpageradaper class object
ViewPagerAdapter adapter=new ViewPagerAdapter(getSupportFragmentManager());
//adding fragments using adapter object
adapter.AddFragment(new FirstFragment(), "Home");
adapter.AddFragment(new SecondFragment(), "Friends");
adapter.AddFragment(new ThirdFragment(), "Group");
//set adapter into viewpager
viewPager.setAdapter(adapter);
//set viewpager into tablayout
tabLayout.setupWithViewPager(viewPager);
Here's the git repo
Sometimes you might get an error with ViewPager2 for auto import import androidx.viewpager.widget.ViewPager;
You have changed it to ViewPager2 also.
Related
I want to access Views that are in Fragment's children that are put in ViewPager. The idea is to toggle (disable/enable) spinners when button is pressed. Spinners are placed in separate fragments that are displayed in ViewPager controlled by TabLayout:
// Snippet of code from Fragment A
final TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
final ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
Fragment childFragment = new Fragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.viewPager, childFragment).commit();
ViewPagerAdapter adapter = new ViewPagerAdapter(childFragment.getFragmentManager());
adapter.AddFragment(new fragment_unositelj(), "FragmentB");
adapter.AddFragment(new fragment_voditelj(), "FragmentC");
adapter.AddFragment(new fragment_pratitelj(), "FragmentD");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
FragmentB = viewPager.findViewById(R.id.FragmentB);
FragmentC = viewPager.findViewById(R.id.FragmentC);
FragmentD = viewPager.findViewById(R.id.FragmentD);
I don't know how to achieve to set Spinner disabled / enabled in all fragment children's spinner. I tried to set public variable in MainActivity and to control Spinners attribute based on that, but I am not sure in which lifecycle part to place it because fragment B is inflated initialy, and others are when TabItem clicked. I hope I could describe my problem properly. If anything more is needed please let me know. Thanks!
I found workaround for this problem, I am not sure if it is correct way, but for now it works for me. In FragmentB,FragmentC,FragmentD i added
#Nullable
#Override
public View getView() {
if (((MainActivity) getActivity()).getStatus() == 1) {
mySpinner.setEnabled( true );
} else
mySpinner.setEnabled( false );
return super.getView();
}
//(MainActivity) getActivity()).getStatus() - method that returns state if it shold be enabled
I have an android app in which I have 3 tabs in a tablayout. I simply want the first tab to open a new activity and not a fragment, because I'll have new tabs in this new activity. What's the best solution for that?
I have tried to desing with a linear layout a "fake tab" but the problem is it's never gonna have the same size as the other tabs.
Is there a way to do that like this?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = findViewById(R.id.pager);
tabLayout = findViewById(R.id.tabLayout);
adapter = new TabAdapter(getSupportFragmentManager());
adapter.addFragment(new ProfileActivity(), "Profile");
adapter.addFragment(new SwipeFragment(), "Meet");
adapter.addFragment(new FriendsFragment(), "Friends");
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
}
this addFragment method does not accept an Activity so that line is in error. Someone has any idea on how can I achieve this?
Anyway in which I can make a tab call an activity instead of a fragment
This question already has answers here:
Android ViewPager Refresh fragment
(2 answers)
Closed 4 years ago.
I implemented TabView with ViewPager for my Application. The issue is that when I swipe view from the first tab to the second tab the view which related to first tab remaining same. I need to clear all TextViews once I leaving that view.
What I found that when I click the first tab it loads two Fragments
I solve that?
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
adapter.addFragment(new VoiceFragment(), "Voice");
adapter.addFragment(new DataFragment(), "Data");
adapter.addFragment(new IddFragment(), "Idd");
viewPager.setAdapter(adapter);
switch (openedActivity) {
case "Voice":
pageId = 0;
break;
case "Data":
pageId = 1;
break;
case "Idd":
pageId = 2;
break;
}
viewPager.setCurrentItem(pageId);
}
You need to set offscreenpage limit of your viewpager to 1.
viewpager.setOffscreenPageLimit(1);
Hope this helps
total novice,but bit familiar with static tabs addition to view pager.But here I stuck in dynamic tabs which I am able to show but not the fragments of those dynamically received tabs.How would I add fragments to those dynamically received tabs from server.Pardon me spelling and could be ambiguous question
Follow this link example and change the following method.
Visit https://www.androidhive.info/2015/09/android-material-design-working-with-tabs/
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
for(int i=0;i<NUM_OF_FRAGMENT;i++){
adapter.addFragment(new YourFragment(), "Title"+i);
}
viewPager.setAdapter(adapter);
}
You need only one fragment for creating dynamic tab.
I am using android.support.v7.widget.Toolbar with ViewPager like this :
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabanim_tabs);
tabLayout.setupWithViewPager(viewPager);
the tab is displaying with text of the ViewPager when I add the dummy Fragment to it
adapter.addFrag(new Fragment(getResources().getColor(R.color.material_light)), "XYZ");
i want the image to display instead of a text.
i did it by using this
tabLayout.getTabAt(0).setIcon(R.drawable.brands);
tabLayout.getTabAt(0).setText("");
tabLayout.getTabAt(1).setIcon(R.drawable.food);
tabLayout.getTabAt(1).setText("");
tabLayout.getTabAt(2).setIcon(R.drawable.cinema);
tabLayout.getTabAt(2).setText("");