this is my pager adapter, i want to load 2 fragment by viewpager and tablayout but it displaying white screen only.
public class HomeFragmentAdapter extends FragmentPagerAdapter {
RelativeLayout headerLogo;
public HomeFragmentAdapter(FragmentManager fm, RelativeLayout headerLogo) {
super(fm);
this.headerLogo = headerLogo;
}
#Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
return new DoubleColumnMintsList();
case 1:
return new MyCircleMain();
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
}
this is inside activity:
viewPager = (ViewPager) view.findViewById(R.id.viewpager);
HomeFragmentAdapter adapter = new HomeFragmentAdapter(getFragmentManager(), headerLogo);
tabLayout = (TabLayout) view.findViewById(R.id.tabs);
viewPager.setOffscreenPageLimit(2);
viewPager.setAdapter(adapter);
setupTabs(tabLayout);
viewPager.setCurrentItem(0)
I was also facing same issue, it solved try to use this line:
HomeFragmentAdapter adapter = new HomeFragmentAdapter(getChildFragmentManager(), headerLogo);
Related
The idea is that I have a listener FAQFragment.OnQuestionSelectedListener that is attached to the main activity which holds a TabLayout. Now, I want every time I receive the event, to pass the event arguments to one of the fragments in the TabLayout. This is my code so far:
public class MainActivity extends AppCompatActivity implements FAQFragment.OnQuestionSelectedListener{
private SectionsPagerAdapter sectionsPagerAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
public void onQuestionSelected(String question) {
sectionsPagerAdapter.setFaq(question);
sectionsPagerAdapter.notifyDataSetChanged();
selectPage(1);
}
private void selectPage(int pageIndex) {
tabLayout.setScrollPosition(pageIndex,0f,true);
viewPager.setCurrentItem(pageIndex);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter{
private String faq;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
faq = null;
}
public void setFaq(String faq) {
this.faq = faq;
}
#Override
public Fragment getItem(int position) {
if (position == 0) return FAQFragment.newInstance();
else if (position == 1) {
ChatFragment chatFragment = ChatFragment.newInstance(faq);
faq = null;
return chatFragment;
}
else if (position == 2) return SettingsFragment.newInstance();
return null;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return "FAQ";
case 1: return "Morty";
case 2: return "Settings";
default: return null;
}
}
#Override
public int getCount() {
return 3;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupTabLayout();
}
private void setupTabLayout() {
// adapter
sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// viewpager
viewPager = findViewById(R.id.container);
viewPager.setAdapter(sectionsPagerAdapter);
// tablayout
tabLayout = findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
// load chat initially
viewPager.setCurrentItem(1);
// listeners
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager));
}
}
The problem with this code is that it doesn't go through the FragmentPagerAdapter's getItemMethods() when tabs are swapped through code. Now, I know that I could do this using fragmentManager() to swap the Fragments when I receive the event, but when I did that, the transition was not smooth (there is a small flickering that grinds my gears). So is there any way to implement a similar logic that would pass a new Fragment Instance every time a tab is selected through viewPager.setCurrentItem or tabLayout.getItem(1).select ?
You can get FAQFragment, ChatFragment or SettingsFragment from ViewPager as described here. Once you got the needed Fragment you can manipulate whatever you want.
i have a viewPager that contains 3 tabs fragments.
the first and the second fragments works fine but when i sweep to the third tab
i do not see him.
the third tab has shown only when the activity with the viewPager has reload(move to another activity and return).
my code:
private void setupViewPager(ViewPager viewPager) {
adapter = new PagerAdapter(getSupportFragmentManager());
adapter.addFragment(new Fragment1(), Constants.TAB_0);
adapter.addFragment(new Fragment2(), Constants.TAB_1);
adapter.addFragment(new Fragment3(), Constants.TAB_2);
viewPager.setAdapter(adapter);
viewPager.setOffscreenPageLimit(3);
}
pager adapter:
public class PagerAdapter extends FragmentPagerAdapter {
/*
* Arraylist used to contain the fragments
* and contain fragments title's
*/
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();
public PagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
//Adds the fragment and it's title. Called in MainActivity
public void addFragment(Fragment fragment, String title) {
fragmentList.add(fragment);
fragmentTitleList.add(title);
}
//Retrieves the title of the tab
#Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
Here is my adapter, on getItem I set the fragment object to the Fragment activity.
public class SectionPagerAdapter extends FragmentPagerAdapter {
Fragment fragment;
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0){
fragment = new FragmentOne();
}
else if (position == 1){
fragment = new FragmentTwo();
}
else if (position == 2){
fragment = new FragmentThree();
}
return fragment;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "name one";
case 1:
return "name two";
case 2:
return "name three";
}
return null;
}
}
Then in my main activity I call it like so:
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
SectionPagerAdapter sectionPagerAdapter = new SectionPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(sectionPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
Try replacing your code with above and let me know if it helps!
USE new PagerAdapter(getChildSupportFragmentManager());instead of getSupportFragmentManager()
Try this one !!
My view pager's Adapter is like:
public Pager(FragmentManager fm, int tabCount) {
super(fm);
this.tabCount = tabCount;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new frag1();
case 1:
return new frag2();
case 2:
return new frag1();
case 3:
return new frag2();
case 4:
return new frag1();
default:
return null;
}
}
#Override
public int getCount() {
return tabCount;
}
}
how i set the tablayout and viewpager in java class:
for (int i = 0; i < 5; i++) {
tabLayout.addTab(tabLayout.newTab());
}
Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.beginFakeDrag();
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
I am getting weird error look like this:
W/FragmentManager: moveToState: Fragment state for frag2{2428405 #1 id=0x7f0c006a} not updated inline; expected state 3 found 2
so how can i fix this issue?
I am getting answers from stack but it doesn't solve my issue,
so anyone have any knowledge about this, kindly help me!
Replace this line viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); with tabLayout.setTabsFromPagerAdapter(adapter);
Try connecting the tabLayout with the viewPager with tabLayout.setupWithViewPager(viewPager) instead of adding the tabs manually in the loop.
available_bus_fragment.xml
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
android:background="#d4d4d4"
app:tabMode="fixed" />
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e8e8e8"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
ViewPagerAdapter.java is adapter for ViewPager.
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
return String.valueOf(position);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment tab1 = new YourFragment1();
return tab1;
case 1:
Fragment tab2 = new YourFragment1();
return tab2;
case 2:
Fragment tab3 = new YourFragment1();
return tab3;
}
return null;
}}
This is your class where you set adapter of Viewpager.
public class MyFragment extends Fragment {
View rootView;
ViewPagerAdapter adapter;
TabLayout tabLayout;
ViewPager viewPager;
private int[] tabIcons = {
R.drawable.tabclock,
R.drawable.clock,
R.drawable.tab_rupee
};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView = getLayoutInflater(savedInstanceState).inflate(R.layout.available_bus_fragment, null);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
viewPager = (ViewPager) rootView.findViewById(R.id.viewpager);
adapter = new ViewPagerAdapter(getChildFragmentManager());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setTabsFromPagerAdapter(adapter);
setupTabIcons();
return rootView;
}
private void setupTabIcons() {
tabLayout.getTabAt(0).setIcon(tabIcons[0]).setTag("Departure").setText("Departure");
tabLayout.getTabAt(1).setIcon(tabIcons[1]).setTag("Duration").setText("Duration");
tabLayout.getTabAt(2).setIcon(tabIcons[2]).setTag("Price").setText("Price");
} }
I have a little problem
Current situation
I have a ViewPager with 2 Fragments inside.
viewPager = (ViewPager) findViewById(R.id.viewpager);
if (viewPager != null) {
setupViewPager(viewPager);
}
//////////////////////
private void setupViewPager(ViewPager viewPager) {
adapter = new PagerAdapter (getSupportFragmentManager());
adapter.addFragment(new Frag1(), "Frag1");
adapter.addFragment(new Frag2(), "Frag2");
viewPager.setAdapter(adapter);
}
And I put it in my TabLayout
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
It works very well but now I want to change the fragments inside my tabs when I press a button in my navigation drawer.
Here :
private void setupDrawerContent(final NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();
menuItem.setChecked(true);
switch (menuItem.getItemId()) {
case R.id.nav_menu1:
break;
case R.id.nav_menu2:
here for example
break;
case R.id.nav_menu3:
break;
}
return true;
}
});
}
How should I do this?
PS: I tried to add some notifyDataSetChanged() for the adapter.. invalidate() the tabLayout ... I removed the code because I did it wrong.
Here is my PagerAdapter
static class PagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragments = new ArrayList<>();
private final List<String> mFragmentTitles = new ArrayList<>();
public PagerAdapter(FragmentManager fm) {
super(fm);
}
public void addFragment(Fragment fragment, String title) {
mFragments.add(fragment);
mFragmentTitles.add(title);
}
#Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
#Override
public int getCount() {
return mFragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitles.get(position);
}
}
Solved with FragmentStatePagerAdapter instead of FragmentPagerAdapter.
I have a navigation drawer in which there is a ViewPager that extends a Fragment. When i click the item of drawer i open the viewpager in which there are three fragments. It works perfectly. but if i click again the same drawer item to open the viewpager another one time, the viewpager is empty.. I can see the tabs but not the fragments in there. This is the Viewpager:
public class ViewPagerManager extends Fragment {
public static ViewPagerManager instance = null;
Toolbar toolbar;
public static PagerSlidingTabStrip tabs;
public MyPagerAdapter adapter;
public ViewPager pager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_tabbed, container, false);
instance = this;
adapter = new MyPagerAdapter(getFragmentManager());
pager = (ViewPager)view.findViewById(R.id.pager);
tabs = (PagerSlidingTabStrip)view.findViewById(R.id.tabs);
pager.setAdapter(adapter);
tabs.setViewPager(pager);
pager.setOffscreenPageLimit(3);
adapter.notifyDataSetChanged();
pager.invalidate();
return view;
}
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "One", "Two", "Three" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// Top Rated fragment activity
return new FragmentOne();
case 1:
// Games fragment activity
return new FragmentTwo();
case 2:
// Games fragment activity
return new FragmentThree();
}
return null;
}
}
}
Is it normal? How can i solve? If could help i'm using PagerSlidingTabStrip library.
Use this code
adapter = new MyPagerAdapter(getChildFragmentManager());
instead of
adapter = new MyPagerAdapter(getFragmentManager());
Try to redraw the last selected page when you return to the fragment. I think the viewpager is not cached and you need to reselect the last item. You can override the onresume method.