如何在选项卡中添加滑动

时间:2014-02-01 09:31:18

标签: android android-support-library android-tabs

我正在使用支持库在我的应用中创建操作栏。我在动作栏中添加了动作完美的动作。现在我编辑下面的标签。但是要更改标签,我必须单击标签。我想在此代码中添加滑动。但对我来说很困难,因为我从一个链接中获取参考,只显示添加标签并在点击它们时更改它们。所以请有人帮我从屏幕添加滑动以更改标签。

代码 -

public class Types extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.types);
        setupTabs();        
    }


    private void setupTabs() {
        android.support.v7.app.ActionBar ab = getSupportActionBar();
        ab.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );
        Tab tab = ab.newTab()
        .setText( R.string.frag1).setTabListener(new MyTabListener(this, Type1.class.getName()));
        ab.addTab(tab);

        tab = ab.newTab()
        .setText( R.string.frag2).setTabListener(new MyTabListener( this, Type2.class.getName() ) );
        ab.addTab(tab);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_search:
        homeActivity();
        return true;

        default:
        return super.onOptionsItemSelected(item);   
        }
     }

     public void homeActivity() {
        Toast.makeText(this, "Home Option Selexted", Toast.LENGTH_SHORT).show();
     }

private class MyTabListener implements ActionBar.TabListener
{
    private Fragment mFragment;
    private final Activity mActivity;
    private final String mFragName;

    public MyTabListener( Activity activity, String fragName )
    {
        mActivity = activity;
        mFragName = fragName;
    }

    @Override
    public void onTabReselected( Tab tab, 
        FragmentTransaction ft )
    {    
    }

    @Override
    public void onTabSelected( Tab tab, 
        FragmentTransaction ft )
    {
        mFragment = Fragment.instantiate( mActivity, mFragName );
        ft.add( android.R.id.content, mFragment );
    }

    @Override
    public void onTabUnselected( Tab tab, FragmentTransaction ft )
    {
        ft.remove( mFragment );
        mFragment = null;
    }
}

2 个答案:

答案 0 :(得分:2)

创建ViewPagerAdapter类 -

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

    private final int PAGES = 4;

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case 0:
                return new Type1();
            case 1:
                return new Type2();
            case 2:
                return new Type3();
            case 3:
                return new Type4();
            default:
                throw new IllegalArgumentException("The item position should be less or equal to:" + PAGES);
        }
    }

    @Override
    public int getCount() {
        return PAGES;
    }
}

然后在你的Typle班 -

public class Types extends ActionBarActivity {

    private ViewPager viewPager;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pagerad);
        viewPager = (ViewPager) findViewById(R.id.pager);
        ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowHomeEnabled(true);
        viewPager.setOnPageChangeListener(onPageChangeListener);
        viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
        addActionBarTabs();
    }

    private ViewPager.SimpleOnPageChangeListener onPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
    super.onPageSelected(position);
    ab.setSelectedNavigationItem(position);
      }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_search:
        homeActivity();
        return true;

        default:
        return super.onOptionsItemSelected(item);   
        }
     }

     public void homeActivity() {
        Toast.makeText(this, "Home Option Selexted", Toast.LENGTH_SHORT).show();
     }

    private void addActionBarTabs() {
        ab = getSupportActionBar();
        String[] tabs = { "TYPE 1", "TYPE 2", "TYPE 3", "TYPE 4" };
        for (String tabTitle : tabs) {
            ActionBar.Tab tab = ab.newTab().setText(tabTitle).setTabListener(tabListener);
            ab.addTab(tab);
        }
        ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    }

    private ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            viewPager.setCurrentItem(tab.getPosition());
        }

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

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        }
    };

}   

最后创建pagerad xml -

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v4.view.ViewPager>

然后创建每个类型片段,如 -

public class Type1 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.type, container, false)
     return rootView;
     }
}

答案 1 :(得分:0)

您需要使用ViewPager。 从android教程实现viewpager时,您只需要关注SectionsPagerAdapter的getitem方法。在这里,您可以根据“视图”位置的位置选择正确的片段。

您的挑战是应用程序的滑动部分现在只是一个活动,现在每个视图必须作为片段实现。但是这种布局似乎与您当前的代码兼容(例如类似于onTabSelected方法)。

您的types.xml文件应该是ViewPager:

<android.support.v4.view.ViewPager 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="com.example.Types" />

接下来,在您的类型活动中声明一个ViewPager和SectionsPagerAdapter:

SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;

现在,在你OnCreate中添加:

// Create the adapter that will return a fragment for each swipe screen
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);

SectionsPagerAdapter实现为:

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        if (position == 2) {
            return new FragmentA();
        } else if (position == 1) {
            return new FragmentB();
        } else {
            // Pattern from you code...
            return Fragment.instantiate( mActivity, mFragName );
        }
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
        }
        return null;
    }
}

这就是全部:)