Remove Swipe Action from Fixed Tab

我怕爱的太早我们不能终老 提交于 2019-12-24 12:20:05

问题


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 can i disable it in my app? It's possible?

Thanks a lot!


回答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" />



回答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.



来源:https://stackoverflow.com/questions/17621879/remove-swipe-action-from-fixed-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!