mercredi 13 mai 2015

Stop tabswipe horizontally by fingers

I am having three tabs which i have created using android.support.v4.view.ViewPager i just want to stop the swipe horizontally by fingers but it should be working when user taps on the name of the tab.

CalorieBSearchHeading.java

public class CalorieBSearchHeading extends FragmentActivity implements ActionBar.TabListener {

    private String[] tabs = { "Recent","Frequent","My Foods" };
    private String[] tabss = { "Recent","Frequent","My Drinks" };
    private ViewPager viewPager;
    private ActionBar actionBar;
    private TabsCalorieBPageAdapter mAdapter;
    private String foodtype;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_caloriebsearchheading);
        Bundle bundle = getIntent().getExtras();
        if(bundle != null){

            foodtype = bundle.getString("foodtype");

        }
        viewPager = (ViewPager) findViewById(R.id.pagercalorieb);
        actionBar = getActionBar();
        mAdapter = new TabsCalorieBPageAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
        actionBar.setStackedBackgroundDrawable(new ColorDrawable(Color.parseColor("#008080")));
        if(foodtype.equals("Drinks")){
            for (String tab_name : tabss) {
                actionBar.addTab(actionBar.newTab().setText(tab_name)
                        .setTabListener(this));
            }
        }
        else{
            for (String tab_name : tabs) {
                actionBar.addTab(actionBar.newTab().setText(tab_name)
                        .setTabListener(this));
            }
        }
        // Adding Tabs


        /**
         * on swiping the viewpager make respective tab selected
         * */
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {


    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {


    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            Intent intent = new Intent(this, CalorieMainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        default:
            return super.onOptionsItemSelected(item);

        }
    }

CalorieBSearchHeading.xml

<android.support.v4.view.ViewPager xmlns:android="http://ift.tt/nIICcg"
        android:id="@+id/pagercalorieb"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </android.support.v4.view.ViewPager>

TabOne.java

 public class CalorieBTabOneRecentFood extends Fragment{

//here i have done my work
    }

And Same for tab two and tab three. what code i should add to stop swiping by fingers.

enter image description here

Aucun commentaire:

Enregistrer un commentaire