使用滑动

时间:2015-05-19 18:46:48

标签: android android-fragments

我是学生,而我正在尝试为我的最终项目制作一个Android应用程序,我有这个代码,我想删除用手指滑动更改的标签,我只希望它通过单击选项卡进行导航。

  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));

    }

1 个答案:

答案 0 :(得分:0)

允许您在选项卡之间进行更改的组件Security_User,因此您只需要将其从您正在执行的活动中删除,这里是{{3你也可以使用viewpager,但是你需要从滑动中截取触摸事件,试试examplethis,它应该可以满足您的需求。

<强>更新

从你的代码:

viewpager

将此变量放入您的活动: Tab = (ViewPager) findViewById(R.id.pager); TabPagerAdapter tabAdapter = new TabPagerAdapter(getSupportFragmentManager()); //change this to : mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); mViewPager = (CustomViewPager) findViewById(R.id.pager);

CustomViewPager mViewPager;

实现CustomViewPager

// 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));
    }

确保更改布局文件以显示:

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

}

而不是:

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

PS:我没有编写代码,可能会有更小的错误