禁用标签页

时间:2015-05-14 06:46:02

标签: java android android-fragments android-activity

我有三个使用android.support.v4.view.ViewPager创建的标签我只想停止手指水平滑动,但是当用户点击标签名称时它应该正常工作。

  

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://schemas.android.com/apk/res/android"
        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
    }

对于标签2和标签3也是如此。 我应该添加什么代码来停止用手指滑动。

2 个答案:

答案 0 :(得分:1)

首先,您必须创建自定义视图寻呼机。前

public class CustomViewPager extends ViewPager {
    private boolean enabled;

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

    @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;
    }
}

现在在您的布局中使用此viewpager,如。

    <yourclasspackage.CustomViewPager
        android:id="@+id/viewPager"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
      />

现在在课堂上设置pageenable。

viewPager.setPagingEnabled(false);

答案 1 :(得分:1)

  

尝试在片段活动中设置限制。

 viewPager.setOffscreenPageLimit(Number);