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
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
I am developing one android application and it has a tab layout with ViewPager setup. i have totally 8 fragments in the viewpager. Each Fragment holding an RecyclerView. The recyclerview row has one text and one checkbox.
Whenever the activity started it loads 8 fragments with recyclerview data. I have a button in the MainActivity, whenever the button is clicked i need to collect the checked rows from all 8 fragments and i have to send the same to the server.
i dont have any clue about the data retrieval but my UI design has been completed.
Looking in to example you followed, you can do something like:
1. Create global objects of fragments:
private OneFragment oneFragment;
private TwoFragment twoFragment;
private ThreeFragment threeFragment;
2. Initialize them in onCreate:
oneFragment = new OneFragment();
twoFragment = new TwoFragment();
threeFragment = new ThreeFragment();
3. Update setupViewPager method:
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(oneFragment, "ONE");
adapter.addFragment(new twoFragment, "TWO");
adapter.addFragment(new threeFragment, "THREE");
viewPager.setAdapter(adapter);
}
4. Create getSelectedValues mehthod in all fragments:
Create getSelectedValues in all fragments which will return
selected items.
5. Get selected values from onClick method in MainActivity:
if (oneFragment != null) {
oneFragment.getSelectedValues(); // here you'll get selected values of oneFragment
}
if (twoFragment != null) {
twoFragment.getSelectedValues(); // here you'll get selected values of twoFragment
}
if (threeFragment != null) {
threeFragment.getSelectedValues(); // here you'll get selected values of threeFragment
}
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.