Initially the fragment returns first and second tab's corresponding listview. But returns empty fragment for third tab. When i select the first tab once again empty fragment is getting displayed. No problem with listadapter
. When i set the adapter in second tab its working fine. Problem is with Mainactivity and in Fragment
Mainactivity
public class SwipemainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{
private TextView displayTextView;
public static SwipemainActivity instance;
TabLayout tabLayout;
ViewPager viewPager;
Context context;
FragmentAdapterClass fragmentAdapter;
public static Context getInstance() {
return instance;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipemain);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
instance = this;
tabLayout = (TabLayout) findViewById(R.id.tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setSupportActionBar(toolbar);
tabLayout.addTab(tabLayout.newTab().setText("NEW"));
tabLayout.addTab(tabLayout.newTab().setText("CONFIRMED"));
tabLayout.addTab(tabLayout.newTab().setText("FINISHED"));
fragmentAdapter = new FragmentAdapterClass(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(fragmentAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab LayoutTab) {
viewPager.setCurrentItem(LayoutTab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab LayoutTab) {
}
#Override
public void onTabReselected(TabLayout.Tab LayoutTab) {
}
});
}
}
Fragment
public class FragmentAdapterClass extends FragmentStatePagerAdapter {
int TabCount;
public FragmentAdapterClass(FragmentManager fragmentManager, int CountTabs) {
super(fragmentManager);
this.TabCount = CountTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
case 2:
Tab3 tab3 = new Tab3();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return TabCount;
}
}
I never use this listener for changing the viewpager fragment. And use the setOffScreenPageLimit to tell the viewpager how many fragments needs to be saved in memory. This is how I always setup my fragments with tabbedlayout, and viewpager.
viewPager.setOffscreenPageLimit(AMOUNT_OF_FRAGMENTS);
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 maked a ViewPager with 3 Fragments:
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager(),getApplicationContext()));
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
});
}
private class CustomAdapter extends FragmentPagerAdapter {
private String fragments [] = {"Fragment 1","Fragment 2", "Fragment 3"};
public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext) {
super(supportFragmentManager);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
default:
return null;
}
}
#Override
public int getCount() {
return fragments.length;
}
#Override
public CharSequence getPageTitle(int position) {
return fragments[position];
}
}
}
I would have instead of the String Title (like Fragment 1,2...) a Image of the drawable Folder.
I'm not getting from your question that what actually you want to do! but what i assume is you want to set image instead of title of fragment. if yes then it might help you.
you can easily set icon using tabLayout object with getTabAt,setIcon() method.
tabLayout.getTabAt(i).setIcon();
you can also refer:
How to add page title and icon in android FragmentPagerAdapter
P.S if you're question is different free to ask,Thanks.
If you are using tablayout desing:
tabLayout.getTabAt(i).setIcon();
but come across a problem. When i run the app i don't see that my screen is swipes, and also i can't see tabs. Can you help
Here is my code:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tabLayout= (TabLayout) findViewById(R.id.tablayout);
viewPager= (ViewPager) findViewById(R.id.viewpager);
viewPagerAdapter=new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new Home(),"Home");
// viewPagerAdapter.addFragments(new Top(),"Top");
// viewPagerAdapter.addFragments(new Buttom(),"Buttom");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
}
I'm getting an error on commented lines.
Here is my Pager Adapters code:
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments= new ArrayList<>();
ArrayList<String> tabTitles = new ArrayList<>();
public void addFragments(Home fragments, String titles){
this.fragments.add(fragments);
this.tabTitles.add(titles);
}
public ViewPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragments.get(position);
}
#Override
public int getCount() {
return fragments.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
In my XML i've only Viewpager, Toolbarand TabLayout.
Please Help.
You need to change your type of parameter in addFragments
try this :
public void addFragments(Fragment fragments, String titles)
instead of
public void addFragments(Home fragments, String titles)
I hope this may help you
Probably is this:
Home fragments
Try change to this:
Fragment fragments
Method be like this:
public void addFragments(Fragment fragments, String titles){
this.fragments.add(fragments);
this.tabTitles.add(titles);
}
So you in the method you have Home fragments as paramter and when you call other fragments diferent than Home because parameter is Home you get error, to fix just do like I said change parameter to Fragment fragments. it's a small mistake.
Try this.
In your xml layout file.
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:tabSelectedTextColor="#android:color/white"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
In your mainactivity
tabLayout.addTab(tabLayout_ordering.newTab().setText("Tab 1"));
tabLayout.addTab(tabLayout_ordering.newTab().setText("Tab 2"));
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout_ordering));
tabLayout_ordering.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Then create a class for your pager adapter.
public class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm, int tabCount) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
FragmentA fragmentA = new FragmentA();
return fragmentFoods;
case 1:
FragmentDrinks fragmentA = new FragmentA();
return fragmentA;
default:
return null;
}
}
#Override
public int getCount() {
return 2;
}
}
I have a tablayout (tab1, tab2, tab3) with a ViewPager. I want to input something in tab1, and depends on the input, tab2 and tab3 will show data accordingly.
It seems every time the app start, tab1 (fragment) and tab2 (fragment) are loaded with correct data. I'm using static variable to pass data from tab1 to tab2. However, after I change some data in tab1, tab2 doesn't update itself base on the data from tab1, it shows the old data. Here are my codes
private ViewPager viewPager;
private PagerAdapter adapter;
private TabLayout tabLayout;
in my onCreate method within an activity.
tabLayout.addTab(tabLayout.newTab().setText("Input"));
tabLayout.addTab(tabLayout.newTab().setText("Detail"));
tabLayout.addTab(tabLayout.newTab().setText("Chart"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
viewPager = (ViewPager) findViewById(R.id.pager);
adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Here's my pageadapater class
public class PagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
private InputFragment inputFragment;
private DetailFragment detailFragment;
private ChartFragment chartFragment;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return POSITION_NONE;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
inputFragment = new InputFragment();
return inputFragment;
case 1:
detailFragment = new detailFragment();
return detailFragment;
case 2:
chartFragment = new ChartFragment();
return chartFragment;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
I am using ViewPager to create 3 Fragment on Activity1,
this is my viewPager adapter :
public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Fragment1 tab1 = new Fragment1();
return tab1;
case 1:
Fragment2 tab2 = new Fragment2();
return tab2;
case 2:
Fragment3 tab3 = new Fragment3();
return tab3;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
this is Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
TabLayout tab_layout = (TabLayout) findViewById(R.id.tab_layout);
tab_layout.addTab(tab_layout.newTab().setText("Frag1"));
tab_layout.addTab(tab_layout.newTab().setText("Frag2"));
tab_layout.addTab(tab_layout.newTab().setText("Frag3"));
final ViewPager view_pager = (ViewPager) findViewById(R.id.viewpager);
final ViewPagerAdapter adapter = new ViewPagerAdapter
(getSupportFragmentManager(), tab_layout.getTabCount());
view_pager.setAdapter(adapter);
view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));
tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
view_pager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
When Activity1 is open it show Fragment1 . But I want to show Fragment2 or Fragment3 instead .
How I can to do that?
Add this line after viewpager.setAdapter(adapter); :
viewPager.setCurrentItem(index);
Index is depending on you , how many fragments you have. tabLayout.getTabCount(); will return you tab count. But index is starting from 0.
If you have 3 Fragment and want to show third Fragment you must use viewPager.setCurrentItem(2);
Set current position of viewpager to your desired possition which is 2 or 3 in your case.
new Handler().post(new Runnable() {
#Override
public void run() {
mViewPager.setCurrentItem(desiredPos);
}
});
seee more
Android ViewPager setCurrentItem not working after onResume