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.

public class MainActivity extends Activity implements ActionBar.TabListener {

    private int mFragmentCount;

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

        // Set up the action bar.
        final ActionBar actionBar = 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 (int i = 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));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(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
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

    @Override
    public void onTabReselected(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;

public class NoSwipeViewPager extends ViewPager {

  private boolean enabled;

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

  @Override
  public boolean onTouchEvent(MotionEvent event) {
      if (this.enabled) {
          return super.onTouchEvent(event);
      }

      return false;
  }

  @Override
  public boolean onInterceptTouchEvent(MotionEvent event) {
      if (this.enabled) {
          return super.onInterceptTouchEvent(event);
      }

      return false;
  }

  public void setPagingEnabled(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"