1

I 'm a student and I 'm trying to make an android application for my final project, I have this code and I want to remove the tab changing with with finger swipe , I want it only to be navigated by clicking on the tab.

  package com.damcotech.football_manager;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;

public class MainActivity extends FragmentActivity {
    //tabbed variables
    private ViewPager Tab;
    private ActionBar actionBar;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TabPagerAdapter tabAdapter = new TabPagerAdapter(getSupportFragmentManager());

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

        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {

                        actionBar = getActionBar();
                        assert actionBar != null;
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        Tab.setAdapter(tabAdapter);

        //este metodo hace que se queden en memoria cargadas las 5 paginas
        Tab.setOffscreenPageLimit(4);

        actionBar = getActionBar();
        //Enable Tabs on Action Bar
        assert actionBar != null;
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {

            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                                        FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }

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

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

            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                                        FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }
        };
        //Add New Tab
        actionBar.addTab(actionBar.newTab().setText("Estadisticas").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Tacticas").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Anotaciones").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Reglamento").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Fichas").setTabListener(tabListener));

    }
Alejandro Cumpa
  • 2,118
  • 1
  • 24
  • 45
  • ¿Qué es lo que quieres? – Alejandro Cumpa May 19 '15 at 19:33
  • I want to eliminate the option to switch between tabs by sliding your finger over the display, One of the tabs have a drag & drop and when I want to move the image switches to the next window – Enrique Martinez gomez May 20 '15 at 22:50
  • 1
    ahoramismo tengo 5 tabs para las 5 ventanas que tiene la aplicacion, puedes cambiar de 1 a otra pinchando en en tab o deslizando en horizontal por la pantalla con el dedo. lo que quiero es deshabilitar la posibilidad de cambiar de ventana con el dedo, quiero que solo cambie cuando haga click en el tab porque en una de las pestañas tengo un arrastrar y soltar y cuando pincho la imagen para moverla me cambia a la otra ventana – Enrique Martinez gomez May 21 '15 at 11:36

1 Answers1

0

The componnet that allows you to change between tabs with swipe it's the viewpager, so you will just have to remove it from the activity you're doing, here is an example, also you can still have the viewpager, but you will need to intercept the touch event from the swipe, try this or this, it should fix fill your needs.

UPDATE:

From your code:

        Tab = (ViewPager) findViewById(R.id.pager);
TabPagerAdapter tabAdapter = new TabPagerAdapter(getSupportFragmentManager());
//change this to : 
 mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (CustomViewPager) findViewById(R.id.pager);

Put this variable to your activity: CustomViewPager mViewPager;

// Set up the CustomViewPager with the sections adapter.
    mViewPager = (CustomViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

And implement the CustomViewPager

public class CustomViewPager extends ViewPager {
    private boolean swipeable = true;

    public CustomViewPager(Context context) {
        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // Call this method in your motion events when you want to disable or enable
    // It should work as desired.
    public void setSwipeable(boolean swipeable) {
        this.swipeable = swipeable;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {
        return (this.swipeable) ? super.onInterceptTouchEvent(arg0) : false; 
    }

}

Make sure to change your layout file to show:

<com.your.package.CustomViewPager .. />

Instead of:

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

PS: I have not teset the code, it could have littler mistakes

Community
  • 1
  • 1
Alejandro Cumpa
  • 2,118
  • 1
  • 24
  • 45