Skip to content Skip to sidebar Skip to footer

Remove Swipe Action From Fixed Tab

I created an Android Project with ADT for Android 4+ versions and created a main Activity with 'Fixed Tabs + Swipe' (with the wizard)... but i don't need the swipe action, so, how

Solution 1:

  1. Replace the ViewPager in activity_main.xml with something else (like FrameLayout) and change its id to something sensible, like @+id/activity_root
  2. Remove everything related to ViewPager and SectionsPagerAdapter from MainActivity.
  3. Use the onTabSelected callback to switch fragments.

Something like this should work. You'll have to add logic to create and maintain your fragments.

publicclassMainActivityextendsActivityimplementsActionBar.TabListener {

    privateint mFragmentCount;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.finalActionBaractionBar= getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // For each of the sections in the app, add a tab to the action bar.
        mFragmentCount = 3;
        for (inti=0; i < mFragmentCount; i++) {
            // Create a tab with text Also specify this Activity object, which// implements the TabListener interface, as the callback (listener)// for when this tab is selected.
            actionBar.addTab(actionBar.newTab().setText("Tab " + i).setTabListener(this));
        }
    }

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        returntrue;
    }

    @OverridepublicvoidonTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // Switch fragments// use fragmentTransaction methods with R.id.activity_root for the container id// don't call commit(), it will be called for you
    }

    @OverridepublicvoidonTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

    @OverridepublicvoidonTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}
}

Layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

Solution 2:

I had the same problem. This is a quick way to disable the swiping.

In activity_main.xml chane ViewPager to the below:

<com.project.android.NoSwipeViewPager    
xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/pager"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity" />

Then create the below class in your project:

package com.project.android;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

publicclassNoSwipeViewPagerextendsViewPager {

  privateboolean enabled;

  publicNoSwipeViewPager(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.enabled = false;
  }

  @OverridepublicbooleanonTouchEvent(MotionEvent event) {
      if (this.enabled) {
          returnsuper.onTouchEvent(event);
      }

      returnfalse;
  }

  @OverridepublicbooleanonInterceptTouchEvent(MotionEvent event) {
      if (this.enabled) {
          returnsuper.onInterceptTouchEvent(event);
      }

      returnfalse;
  }

  publicvoidsetPagingEnabled(boolean enabled) {
      this.enabled = enabled;
  }
}

The this.enabled = false; in NoSwipeViewPager will disable the swiping.

Post a Comment for "Remove Swipe Action From Fixed Tab"