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
Related
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.
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
Right now, I have five fragments (tabs) in my app as the image shows. And they are all listed as members of the tab bar in the MainActivity.java
public class MainActivity extends AppCompatActivity implements InputTab.SendMessage, FollowingTab.SendMessage, FollowerTab.SendMessage, ProfileTab.SendMessage {
private SectionsPageAdapter pageAdapter;
ViewPager viewPager;
public static String currentUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new ProfileTab());
fragmentTransaction.commit();
pageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Sets up the ViewPager with the sections adapter
viewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager((viewPager));
}
// Adds fragments to SectionsPageAdapter and gives names for the corresponding tab
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new InputTab(), "Search");
adapter.addFragment(new ProfileTab(), "Profile");
adapter.addFragment(new GithubTab(), "Github Repos");
adapter.addFragment(new FollowerTab(), "Followers");
adapter.addFragment(new FollowingTab(), "Followings");
viewPager.setAdapter(adapter);
}
Since I implemented a search tab, I was wondering if it's possible to create another page(or fragment) that displays the result of the search, and when I click the result, I want to make it direct to the corresponding tabs (Profile for users, and Github Repos for repo search).
In short, how can I create a page that is not part of the tab and still display it when I press the 'search' button?
Implement your TabLayout and ViewPager inside a fragment instead of the activiry and replace that fragment with the results fragment.
I have a ViewPager to which I am adding 3 tab fragments dynamically:
private void addTabs(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new QuotesFragment(), "Quotes");
adapter.addFragment(new BooksFragment(), "Books");
adapter.addFragment(new ExercisesFragment(), "Exercises");
viewPager.setAdapter(adapter);
}
Here's the code of the fragment that is not working as expected:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
recyclerView = (RecyclerView) getActivity().findViewById(R.id.quotes_recycler);
layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
MasonryAdapter adapter = new MasonryAdapter(context, quotesList);
recyclerView.setAdapter(adapter);
}
The RecyclerView view holders form QuotesFragment() will not show up at parent Activity's start. They only show up when I have scrolled to the last tab (ExercisesFragment() in this case) and returned to the first one. Checking in Hierarchy Viewer it seems that at the app start the RecyclerView is initialized (the container takes necessary space on the screen), but its children are not.
What's odd, when I change the order in which I am adding tabs like so (note my problematic tab is now in the middle):
private void addTabs(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new ExercisesFragment(), "Exercises");
adapter.addFragment(new QuotesFragment(), "Quotes");
adapter.addFragment(new BookFragment(), "Books");
viewPager.setAdapter(adapter);
}
it does never load! I have tried various things, and there's even one question about exactly the same problem, but it does not help in my case. Could it have to do with Fragment/ViewPager lifecycle or implicit methods?
Any help appreciated.
I finally got it to work and it's dumb simple really. I was calling my method that loads quotes from JSON inside Fragment's onCreate() instead of onViewCreated(). When I moved it to onViewCreated() (after setting adapter and layout manager) it started working.
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.