1

i want to integrate tabhoast with material design android so i write below code

public class ActivityPaymentModule extends AppCompatActivity implements MaterialTabListener {

private ViewPager pager;
private MaterialTabHost tabHost;
private ViewPagerAdapter pagerAdapter;
private Resources res;
public static int tabPosition = 0;

private AllMethods allMethods;

private String Titles[]={"Tab1","Tab2, "Tab3","Tab4"};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_payment_module);
    res = this.getResources();

    allMethods = new AllMethods(ActivityPaymentModule.this);

    Toolbar toolbar = (Toolbar) this.findViewById(R.id.toolbar);
    toolbar.setTitleTextColor(Color.WHITE);
    toolbar.setTitle("Demo");
    this.setSupportActionBar(toolbar);

    initControls();
    setPagerAdapter();
}

private void initControls() {
    tabHost = (MaterialTabHost)findViewById(R.id.materialTabHost);

    pager = (ViewPager) this.findViewById(R.id.pager);

    pagerAdapter =  new ViewPagerAdapter(getSupportFragmentManager());
}

private void setPagerAdapter() {
    pager.setAdapter(pagerAdapter);
    tabHost.setAccentColor(Color.WHITE);
    pager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            // when user do a swipe the selected tab change
            tabPosition = position;
            tabHost.setSelectedNavigationItem(position);
        }
    });

    for (int i = 0; i < pagerAdapter.getCount(); i++) {
        tabHost.addTab(
                tabHost.newTab()
                        .setText(Titles[i])
                        .setTabListener(this)
        );
    }
}

@Override
public void onTabSelected(MaterialTab materialTab) {
    pager.setCurrentItem(materialTab.getPosition());
}

@Override
public void onTabReselected(MaterialTab materialTab) {
}

@Override
public void onTabUnselected(MaterialTab materialTab) {

}

private class ViewPagerAdapter extends FragmentStatePagerAdapter {
    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }
    @Override
    public Fragment getItem(int position) {


        if (position == 0) {
            return new Fragment1();
        }else if (position == 1) {
            return new Fragment2();
        }else if(position == 2){
            return new Fragment3();
        }

        else {
            return new Fragment4();
        }
    }
    @Override
    public int getCount() {
        return 4;
    }
    @Override
    public CharSequence getPageTitle(int position) {
        return Titles[position];
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.income_menu, menu);
    menu.findItem(R.id.action_dasbboard).setVisible(false);
    menu.findItem(R.id.action_feedback).setVisible(false);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case R.id.terms:

            Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://worldofrental.com/terms-of-service/"));
            startActivity(browserIntent2);
            return true;
        case R.id.policy:

            Intent browserIntent3 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://worldofrental.com/privacy-policy/"));
            startActivity(browserIntent3);
        case R.id.action_dasbboard:

            return true;

        case R.id.action_help:

            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://worldofrental.com/help-center/"));
            startActivity(browserIntent);
            return true;

    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    this.finish();
}

}

EDIT

I face 2 issues: (1) when first time load my screen at that time Fragment1() and Fragment2() both are called. (2)When i swipe fragment at that time fragment replace position not maintain like when i swipe Fragment1() from right to left at that time Fragment3() and Fragment2() both called

When i run above code at first time Fragment1() and Fragment2() both fragment called. and when i swipe it fragment position also not maintain by replacing so any idea how can i solve this ? your all suggestions are appreciable.

  • what is not maintained when you swipe? Can you elaborate, your question is not very clear. Thanks – Kaveesh Kanwal Sep 30 '15 at 04:16
  • Kaveesh Kanwal: I face 2 issues: (1) when first time load my screen at that time Fragment1() and Fragment2() both are called. (2)When i swipe fragment at that time fragment replace position not maintain like when i swipe Fragment1() from right to left at that time Fragment3() and Fragment2() botj called –  Sep 30 '15 at 04:48

0 Answers0